MCPcopy Create free account
hub / github.com/coder/coder / TestLogSender_MaxQueuedLogs

Function TestLogSender_MaxQueuedLogs

codersdk/agentsdk/logs_internal_test.go:327–385  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

325}
326
327func TestLogSender_MaxQueuedLogs(t *testing.T) {
328 t.Parallel()
329 testCtx := testutil.Context(t, testutil.WaitShort)
330 ctx, cancel := context.WithCancel(testCtx)
331 logger := testutil.Logger(t)
332 fDest := newFakeLogDest()
333 uut := NewLogSender(logger)
334
335 t0 := dbtime.Now()
336 ls1 := uuid.UUID{0x11}
337 n := 4
338 hugeLog := make([]byte, maxBytesQueued/n)
339 for i := range hugeLog {
340 hugeLog[i] = 'q'
341 }
342 var logs []Log
343 for i := 0; i < n; i++ {
344 logs = append(logs, Log{
345 CreatedAt: t0,
346 Output: string(hugeLog),
347 Level: codersdk.LogLevelInfo,
348 })
349 }
350 uut.Enqueue(ls1, logs...)
351
352 // we're now right at the limit of output
353 require.Equal(t, maxBytesQueued, uut.outputLen)
354
355 // adding more logs should not error...
356 ls2 := uuid.UUID{0x22}
357 uut.Enqueue(ls2, logs...)
358
359 loopErr := make(chan error, 1)
360 go func() {
361 err := uut.SendLoop(ctx, fDest)
362 loopErr <- err
363 }()
364
365 // It should still queue up one log from source #2, so that we would exceed the database
366 // limit. These come over a total of 3 updates, because due to overhead, the n logs from source
367 // #1 come in 2 updates, plus 1 update for source #2.
368 logsBySource := make(map[uuid.UUID]int)
369 for i := 0; i < 3; i++ {
370 req := testutil.TryReceive(ctx, t, fDest.reqs)
371 require.NotNil(t, req)
372 srcID, err := uuid.FromBytes(req.LogSourceId)
373 require.NoError(t, err)
374 logsBySource[srcID] += len(req.Logs)
375 testutil.RequireSend(ctx, t, fDest.resps, &proto.BatchCreateLogsResponse{})
376 }
377 require.Equal(t, map[uuid.UUID]int{
378 ls1: n,
379 ls2: 1,
380 }, logsBySource)
381
382 cancel()
383 err := testutil.TryReceive(testCtx, t, loopErr)
384 require.ErrorIs(t, err, context.Canceled)

Callers

nothing calls this directly

Calls 10

EnqueueMethod · 0.95
SendLoopMethod · 0.95
ContextFunction · 0.92
LoggerFunction · 0.92
NowFunction · 0.92
TryReceiveFunction · 0.92
RequireSendFunction · 0.92
newFakeLogDestFunction · 0.85
NewLogSenderFunction · 0.85
EqualMethod · 0.45

Tested by

no test coverage detected