@@ -19,7 +19,7 @@ import (
19
19
"github.com/coder/coder/v2/testutil"
20
20
)
21
21
22
- func TestLogSender (t * testing.T ) {
22
+ func TestLogSender_Mainline (t * testing.T ) {
23
23
t .Parallel ()
24
24
testCtx := testutil .Context (t ,testutil .WaitShort )
25
25
ctx ,cancel := context .WithCancel (testCtx )
@@ -62,7 +62,9 @@ func TestLogSender(t *testing.T) {
62
62
// both, although the order is not controlled
63
63
var logReqs []* proto.BatchCreateLogsRequest
64
64
logReqs = append (logReqs ,testutil .RequireRecvCtx (ctx ,t ,fDest .reqs ))
65
+ testutil .RequireSendCtx (ctx ,t ,fDest .resps ,& proto.BatchCreateLogsResponse {})
65
66
logReqs = append (logReqs ,testutil .RequireRecvCtx (ctx ,t ,fDest .reqs ))
67
+ testutil .RequireSendCtx (ctx ,t ,fDest .resps ,& proto.BatchCreateLogsResponse {})
66
68
for _ ,req := range logReqs {
67
69
require .NotNil (t ,req )
68
70
srcID ,err := uuid .FromBytes (req .LogSourceId )
@@ -97,6 +99,7 @@ func TestLogSender(t *testing.T) {
97
99
require .NoError (t ,err )
98
100
99
101
req := testutil .RequireRecvCtx (ctx ,t ,fDest .reqs )
102
+ testutil .RequireSendCtx (ctx ,t ,fDest .resps ,& proto.BatchCreateLogsResponse {})
100
103
// give ourselves a 25% buffer if we're right on the cusp of a tick
101
104
require .LessOrEqual (t ,time .Since (t1 ),flushInterval * 5 / 4 )
102
105
require .NotNil (t ,req )
@@ -118,8 +121,53 @@ func TestLogSender(t *testing.T) {
118
121
require .NoError (t ,err )
119
122
}
120
123
124
+ func TestLogSender_LogLimitExceeded (t * testing.T ) {
125
+ t .Parallel ()
126
+ ctx := testutil .Context (t ,testutil .WaitShort )
127
+ logger := slogtest .Make (t ,nil ).Leveled (slog .LevelDebug )
128
+ fDest := newFakeLogDest ()
129
+ uut := newLogSender (logger )
130
+
131
+ t0 := dbtime .Now ()
132
+
133
+ ls1 := uuid.UUID {0x11 }
134
+ err := uut .enqueue (ls1 , agentsdk.Log {
135
+ CreatedAt :t0 ,
136
+ Output :"test log 0, src 1" ,
137
+ Level :codersdk .LogLevelInfo ,
138
+ })
139
+ require .NoError (t ,err )
140
+
141
+ loopErr := make (chan error ,1 )
142
+ go func () {
143
+ err := uut .sendLoop (ctx ,fDest )
144
+ loopErr <- err
145
+ }()
146
+
147
+ req := testutil .RequireRecvCtx (ctx ,t ,fDest .reqs )
148
+ require .NotNil (t ,req )
149
+ testutil .RequireSendCtx (ctx ,t ,fDest .resps ,
150
+ & proto.BatchCreateLogsResponse {LogLimitExceeded :true })
151
+
152
+ err = testutil .RequireRecvCtx (ctx ,t ,loopErr )
153
+ require .NoError (t ,err )
154
+
155
+ // we can still enqueue more logs after sendLoop returns, but they don't
156
+ // actually get enqueued
157
+ err = uut .enqueue (ls1 , agentsdk.Log {
158
+ CreatedAt :t0 ,
159
+ Output :"test log 2, src 1" ,
160
+ Level :codersdk .LogLevelTrace ,
161
+ })
162
+ require .NoError (t ,err )
163
+ uut .L .Lock ()
164
+ defer uut .L .Unlock ()
165
+ require .Len (t ,uut .queues ,0 )
166
+ }
167
+
121
168
type fakeLogDest struct {
122
- reqs chan * proto.BatchCreateLogsRequest
169
+ reqs chan * proto.BatchCreateLogsRequest
170
+ resps chan * proto.BatchCreateLogsResponse
123
171
}
124
172
125
173
func (f fakeLogDest )BatchCreateLogs (ctx context.Context ,req * proto.BatchCreateLogsRequest ) (* proto.BatchCreateLogsResponse ,error ) {
@@ -130,12 +178,18 @@ func (f fakeLogDest) BatchCreateLogs(ctx context.Context, req *proto.BatchCreate
130
178
case <- ctx .Done ():
131
179
return nil ,ctx .Err ()
132
180
case f .reqs <- req :
133
- return & proto.BatchCreateLogsResponse {},nil
181
+ select {
182
+ case <- ctx .Done ():
183
+ return nil ,ctx .Err ()
184
+ case resp := <- f .resps :
185
+ return resp ,nil
186
+ }
134
187
}
135
188
}
136
189
137
190
func newFakeLogDest ()* fakeLogDest {
138
191
return & fakeLogDest {
139
- reqs :make (chan * proto.BatchCreateLogsRequest ),
192
+ reqs :make (chan * proto.BatchCreateLogsRequest ),
193
+ resps :make (chan * proto.BatchCreateLogsResponse ),
140
194
}
141
195
}