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

Function TestLogSender_Mainline

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

Source from the content-addressed store, hash-verified

18)
19
20func TestLogSender_Mainline(t *testing.T) {
21 t.Parallel()
22 testCtx := testutil.Context(t, testutil.WaitShort)
23 ctx, cancel := context.WithCancel(testCtx)
24 logger := testutil.Logger(t)
25 fDest := newFakeLogDest()
26 uut := NewLogSender(logger)
27
28 t0 := dbtime.Now()
29
30 ls1 := uuid.UUID{0x11}
31 uut.Enqueue(ls1, Log{
32 CreatedAt: t0,
33 Output: "test log 0, src 1",
34 Level: codersdk.LogLevelInfo,
35 })
36
37 ls2 := uuid.UUID{0x22}
38 uut.Enqueue(ls2,
39 Log{
40 CreatedAt: t0,
41 Output: "test log 0, src 2",
42 Level: codersdk.LogLevelError,
43 },
44 Log{
45 CreatedAt: t0,
46 Output: "test log 1, src 2",
47 Level: codersdk.LogLevelWarn,
48 },
49 )
50
51 loopErr := make(chan error, 1)
52 go func() {
53 err := uut.SendLoop(ctx, fDest)
54 loopErr <- err
55 }()
56
57 empty := make(chan error, 1)
58 go func() {
59 err := uut.WaitUntilEmpty(ctx)
60 empty <- err
61 }()
62
63 // since neither source has even been flushed, it should immediately Flush
64 // both, although the order is not controlled
65 var logReqs []*proto.BatchCreateLogsRequest
66 logReqs = append(logReqs, testutil.TryReceive(ctx, t, fDest.reqs))
67 testutil.RequireSend(ctx, t, fDest.resps, &proto.BatchCreateLogsResponse{})
68 logReqs = append(logReqs, testutil.TryReceive(ctx, t, fDest.reqs))
69 testutil.RequireSend(ctx, t, fDest.resps, &proto.BatchCreateLogsResponse{})
70 for _, req := range logReqs {
71 require.NotNil(t, req)
72 srcID, err := uuid.FromBytes(req.LogSourceId)
73 require.NoError(t, err)
74 switch srcID {
75 case ls1:
76 require.Len(t, req.Logs, 1)
77 require.Equal(t, "test log 0, src 1", req.Logs[0].GetOutput())

Callers

nothing calls this directly

Calls 15

EnqueueMethod · 0.95
SendLoopMethod · 0.95
WaitUntilEmptyMethod · 0.95
FlushMethod · 0.95
ContextFunction · 0.92
LoggerFunction · 0.92
NowFunction · 0.92
TryReceiveFunction · 0.92
RequireSendFunction · 0.92
newFakeLogDestFunction · 0.85
NewLogSenderFunction · 0.85
FatalMethod · 0.80

Tested by

no test coverage detected