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