@@ -89,6 +89,9 @@ func TestReplicaSetEvents(t *testing.T) {
89
89
require .Equal (t ,"Kubernetes" ,source .DisplayName )
90
90
require .Equal (t ,"/icon/k8s.png" ,source .Icon )
91
91
92
+ // Advance clock to trigger log flush
93
+ cMock .Advance (time .Second )
94
+
92
95
logs := testutil .RequireRecvCtx (ctx ,t ,api .logs )
93
96
require .Len (t ,logs ,1 )
94
97
require .Contains (t ,logs [0 ].Output ,"Created replicaset" )
@@ -110,13 +113,19 @@ func TestReplicaSetEvents(t *testing.T) {
110
113
_ ,err = client .CoreV1 ().Events (namespace ).Create (ctx ,event , v1.CreateOptions {})
111
114
require .NoError (t ,err )
112
115
116
+ // Advance clock to trigger log flush
117
+ cMock .Advance (time .Second )
118
+
113
119
logs = testutil .RequireRecvCtx (ctx ,t ,api .logs )
114
120
require .Len (t ,logs ,1 )
115
121
require .Contains (t ,logs [0 ].Output ,event .Message )
116
122
117
123
err = client .AppsV1 ().ReplicaSets (namespace ).Delete (ctx ,rs .Name , v1.DeleteOptions {})
118
124
require .NoError (t ,err )
119
125
126
+ // Advance clock to trigger log flush
127
+ cMock .Advance (time .Second )
128
+
120
129
logs = testutil .RequireRecvCtx (ctx ,t ,api .logs )
121
130
require .Len (t ,logs ,1 )
122
131
require .Contains (t ,logs [0 ].Output ,"Deleted replicaset" )
@@ -182,6 +191,9 @@ func TestPodEvents(t *testing.T) {
182
191
require .Equal (t ,"Kubernetes" ,source .DisplayName )
183
192
require .Equal (t ,"/icon/k8s.png" ,source .Icon )
184
193
194
+ // Advance clock to trigger log flush
195
+ cMock .Advance (time .Second )
196
+
185
197
logs := testutil .RequireRecvCtx (ctx ,t ,api .logs )
186
198
require .Len (t ,logs ,1 )
187
199
require .Contains (t ,logs [0 ].Output ,"Created pod" )
@@ -203,13 +215,19 @@ func TestPodEvents(t *testing.T) {
203
215
_ ,err = client .CoreV1 ().Events (namespace ).Create (ctx ,event , v1.CreateOptions {})
204
216
require .NoError (t ,err )
205
217
218
+ // Advance clock to trigger log flush
219
+ cMock .Advance (time .Second )
220
+
206
221
logs = testutil .RequireRecvCtx (ctx ,t ,api .logs )
207
222
require .Len (t ,logs ,1 )
208
223
require .Contains (t ,logs [0 ].Output ,event .Message )
209
224
210
225
err = client .CoreV1 ().Pods (namespace ).Delete (ctx ,pod .Name , v1.DeleteOptions {})
211
226
require .NoError (t ,err )
212
227
228
+ // Advance clock to trigger log flush
229
+ cMock .Advance (time .Second )
230
+
213
231
logs = testutil .RequireRecvCtx (ctx ,t ,api .logs )
214
232
require .Len (t ,logs ,1 )
215
233
require .Contains (t ,logs [0 ].Output ,"Deleted pod" )
@@ -283,14 +301,14 @@ func Test_tokenCache(t *testing.T) {
283
301
}
284
302
285
303
func Test_logQueuer (t * testing.T ) {
286
- t .Run ("Timeout " ,func (t * testing.T ) {
304
+ t .Run ("Basic " ,func (t * testing.T ) {
287
305
api := newFakeAgentAPI (t )
288
306
agentURL ,err := url .Parse (api .server .URL )
289
307
require .NoError (t ,err )
290
- clock := quartz .NewMock ( t )
291
- ttl := time .Second
308
+ clock := quartz .NewReal () // Use real clock for simplicity
309
+ ttl := 100 * time .Millisecond // Short TTL for faster test
292
310
293
- ch := make (chan agentLog )
311
+ ch := make (chan agentLog , 10 ) // Buffered channel to prevent blocking
294
312
lq := & logQueuer {
295
313
logger :slogtest .Make (t ,nil ),
296
314
clock :clock ,
@@ -307,6 +325,7 @@ func Test_logQueuer(t *testing.T) {
307
325
defer cancel ()
308
326
go lq .work (ctx )
309
327
328
+ // Send first log
310
329
ch <- agentLog {
311
330
name :"mypod" ,
312
331
token :"0b42fa72-7f1a-4b59-800d-69d67f56ed8b" ,
@@ -318,11 +337,14 @@ func Test_logQueuer(t *testing.T) {
318
337
},
319
338
}
320
339
321
- //it should send both a log sourcerequest and the log
340
+ //Wait for log sourceto be created
322
341
_ = testutil .RequireRecvCtx (ctx ,t ,api .logSource )
342
+
343
+ // Wait for logs to be sent (ticker fires every second)
323
344
logs := testutil .RequireRecvCtx (ctx ,t ,api .logs )
324
345
require .Len (t ,logs ,1 )
325
346
347
+ // Send second log
326
348
ch <- agentLog {
327
349
name :"mypod" ,
328
350
token :"0b42fa72-7f1a-4b59-800d-69d67f56ed8b" ,
@@ -334,13 +356,18 @@ func Test_logQueuer(t *testing.T) {
334
356
},
335
357
}
336
358
337
- //duplicate logs should not trigger a log source
359
+ //Wait for second batch of logs
338
360
logs = testutil .RequireRecvCtx (ctx ,t ,api .logs )
339
361
require .Len (t ,logs ,1 )
340
362
341
- clock .Advance (ttl )
342
- // wait for the client to disconnect
343
- _ = testutil .RequireRecvCtx (ctx ,t ,api .disconnect )
363
+ // Test cleanup by waiting for TTL
364
+ time .Sleep (ttl + 50 * time .Millisecond )
365
+
366
+ // Verify that the logger was cleaned up
367
+ lq .mu .RLock ()
368
+ loggerCount := len (lq .loggers )
369
+ lq .mu .RUnlock ()
370
+ require .Equal (t ,0 ,loggerCount ,"Logger should be cleaned up after TTL" )
344
371
})
345
372
}
346
373