@@ -174,7 +174,7 @@ func TestLogSender_SkipHugeLog(t *testing.T) {
174
174
175
175
t0 := dbtime .Now ()
176
176
ls1 := uuid.UUID {0x11 }
177
- hugeLog := make ([]byte , logOutputMaxBytes + 1 )
177
+ hugeLog := make ([]byte , maxBytesPerBatch + 1 )
178
178
for i := range hugeLog {
179
179
hugeLog [i ] = 'q'
180
180
}
@@ -244,14 +244,14 @@ func TestLogSender_Batch(t *testing.T) {
244
244
gotLogs += len (req .Logs )
245
245
wire , err := protobuf .Marshal (req )
246
246
require .NoError (t , err )
247
- require .Less (t , len (wire ), logOutputMaxBytes , "wire should not exceed 1MiB" )
247
+ require .Less (t , len (wire ), maxBytesPerBatch , "wire should not exceed 1MiB" )
248
248
testutil .RequireSendCtx (ctx , t , fDest .resps , & proto.BatchCreateLogsResponse {})
249
249
req = testutil .RequireRecvCtx (ctx , t , fDest .reqs )
250
250
require .NotNil (t , req )
251
251
gotLogs += len (req .Logs )
252
252
wire , err = protobuf .Marshal (req )
253
253
require .NoError (t , err )
254
- require .Less (t , len (wire ), logOutputMaxBytes , "wire should not exceed 1MiB" )
254
+ require .Less (t , len (wire ), maxBytesPerBatch , "wire should not exceed 1MiB" )
255
255
require .Equal (t , 60000 , gotLogs )
256
256
testutil .RequireSendCtx (ctx , t , fDest .resps , & proto.BatchCreateLogsResponse {})
257
257
@@ -260,6 +260,68 @@ func TestLogSender_Batch(t *testing.T) {
260
260
require .NoError (t , err )
261
261
}
262
262
263
+ func TestLogSender_MaxQueuedLogs (t * testing.T ) {
264
+ t .Parallel ()
265
+ testCtx := testutil .Context (t , testutil .WaitShort )
266
+ ctx , cancel := context .WithCancel (testCtx )
267
+ logger := slogtest .Make (t , nil ).Leveled (slog .LevelDebug )
268
+ fDest := newFakeLogDest ()
269
+ uut := newLogSender (logger )
270
+
271
+ t0 := dbtime .Now ()
272
+ ls1 := uuid.UUID {0x11 }
273
+ n := 4
274
+ hugeLog := make ([]byte , maxBytesQueued / n )
275
+ for i := range hugeLog {
276
+ hugeLog [i ] = 'q'
277
+ }
278
+ var logs []agentsdk.Log
279
+ for i := 0 ; i < n ; i ++ {
280
+ logs = append (logs , agentsdk.Log {
281
+ CreatedAt : t0 ,
282
+ Output : string (hugeLog ),
283
+ Level : codersdk .LogLevelInfo ,
284
+ })
285
+ }
286
+ err := uut .enqueue (ls1 , logs ... )
287
+ require .NoError (t , err )
288
+
289
+ // we're now right at the limit of output
290
+ require .Equal (t , maxBytesQueued , uut .outputLen )
291
+
292
+ // adding more logs should error...
293
+ ls2 := uuid.UUID {0x22 }
294
+ err = uut .enqueue (ls2 , logs ... )
295
+ require .ErrorIs (t , err , MaxQueueExceededError )
296
+
297
+ loopErr := make (chan error , 1 )
298
+ go func () {
299
+ err := uut .sendLoop (ctx , fDest )
300
+ loopErr <- err
301
+ }()
302
+
303
+ // ...but, it should still queue up one log from source #2, so that we would exceed the database
304
+ // limit. These come over a total of 3 updates, because due to overhead, the n logs from source
305
+ // #1 come in 2 updates, plus 1 update for source #2.
306
+ logsBySource := make (map [uuid.UUID ]int )
307
+ for i := 0 ; i < 3 ; i ++ {
308
+ req := testutil .RequireRecvCtx (ctx , t , fDest .reqs )
309
+ require .NotNil (t , req )
310
+ srcID , err := uuid .FromBytes (req .LogSourceId )
311
+ require .NoError (t , err )
312
+ logsBySource [srcID ] += len (req .Logs )
313
+ testutil .RequireSendCtx (ctx , t , fDest .resps , & proto.BatchCreateLogsResponse {})
314
+ }
315
+ require .Equal (t , map [uuid.UUID ]int {
316
+ ls1 : n ,
317
+ ls2 : 1 ,
318
+ }, logsBySource )
319
+
320
+ cancel ()
321
+ err = testutil .RequireRecvCtx (testCtx , t , loopErr )
322
+ require .NoError (t , err )
323
+ }
324
+
263
325
type fakeLogDest struct {
264
326
reqs chan * proto.BatchCreateLogsRequest
265
327
resps chan * proto.BatchCreateLogsResponse
0 commit comments