@@ -25,7 +25,8 @@ const (
25
25
// --- Helper Functions ---
26
26
27
27
// testLogger creates a logger for tests, optionally capturing output.
28
- func testLogger (t * testing.T , capture bool ) (* slog.Logger , * bytes.Buffer ) { //nolint:thelper
28
+ func testLogger (t * testing.T , capture bool ) (* slog.Logger , * bytes.Buffer ) {
29
+ t .Helper ()
29
30
var buf bytes.Buffer
30
31
w := io .Discard
31
32
if capture {
@@ -40,14 +41,16 @@ func testLogger(t *testing.T, capture bool) (*slog.Logger, *bytes.Buffer) { //no
40
41
}
41
42
42
43
// readWorkerConfig safely reads the current config from the worker.
43
- func readWorkerConfig (w * Worker ) WorkerConfig {
44
+ func readWorkerConfig (t * testing.T , w * Worker ) WorkerConfig {
45
+ t .Helper ()
44
46
w .mu .Lock ()
45
47
defer w .mu .Unlock ()
46
48
return w .config // Return a copy
47
49
}
48
50
49
51
// readWorkerName safely reads the current name from the worker.
50
- func readWorkerName (w * Worker ) string {
52
+ func readWorkerName (t * testing.T , w * Worker ) string {
53
+ t .Helper ()
51
54
w .mu .Lock ()
52
55
defer w .mu .Unlock ()
53
56
return w .name
@@ -218,7 +221,7 @@ func TestWorker_Run_ContextCancel(t *testing.T) {
218
221
// TestWorker_ReloadWithConfig tests applying valid and invalid configs via ReloadWithConfig.
219
222
func TestWorker_ReloadWithConfig (t * testing.T ) {
220
223
t .Parallel ()
221
- logger , logBuf := testLogger (t , true ) // Capture logs
224
+ logger , _ := testLogger (t , false )
222
225
223
226
originalConfig := WorkerConfig {
224
227
Interval : 100 * time .Millisecond ,
@@ -242,8 +245,10 @@ func TestWorker_ReloadWithConfig(t *testing.T) {
242
245
243
246
go func () {
244
247
defer wg .Done ()
245
- // Ignore error here, checked elsewhere
246
- _ = worker .Run (ctx ) //nolint:errcheck
248
+ err = worker .Run (ctx )
249
+ if err != nil {
250
+ t .Errorf ("Worker run failed: %v" , err )
251
+ }
247
252
}()
248
253
249
254
// Wait for worker to start running (at least one tick)
@@ -261,61 +266,47 @@ func TestWorker_ReloadWithConfig(t *testing.T) {
261
266
262
267
// Wait for the configuration to be applied using Eventually
263
268
require .Eventually (t , func () bool {
264
- current := readWorkerConfig (worker )
265
- name := readWorkerName (worker )
269
+ current := readWorkerConfig (t , worker )
270
+ name := readWorkerName (t , worker )
266
271
return current .Interval == newConfig .Interval && current .JobName == newConfig .JobName &&
267
272
name == newConfig .JobName
268
273
}, 1 * time .Second , pollInterval , "Worker config was not updated after valid ReloadWithConfig" )
269
- t .Logf ("Worker config updated successfully to: %+v" , readWorkerConfig (worker ))
274
+ t .Logf ("Worker config updated successfully to: %+v" , readWorkerConfig (t , worker ))
270
275
271
276
// --- Test Invalid Config Type ---
272
- configAfterValidReload := readWorkerConfig (worker ) // Store state before invalid reload
277
+ configAfterValidReload := readWorkerConfig (t , worker ) // Store state before invalid reload
273
278
t .Log ("Reloading with invalid type 'string'" )
274
279
worker .ReloadWithConfig ("invalid type" ) // Pass a non-WorkerConfig type
275
280
276
281
// Wait a short time and assert config hasn't changed
277
- time .Sleep (50 * time .Millisecond ) // Allow select loop to process if needed
278
- currentConfig := readWorkerConfig (worker )
282
+ assert .Eventually (t , func () bool {
283
+ return worker .tickCount .Load () > 0
284
+ }, 1 * time .Second , pollInterval , "Worker did not start ticking" )
285
+
286
+ currentConfig := readWorkerConfig (t , worker )
279
287
assert .Equal (
280
288
t ,
281
289
configAfterValidReload ,
282
290
currentConfig ,
283
291
"Config should not change after invalid type reload" ,
284
292
)
285
- assert .Contains (
286
- t ,
287
- logBuf .String (),
288
- "Invalid config type received" ,
289
- "Should log error for invalid type" ,
290
- )
291
- logBuf .Reset () // Clear buffer for next check
292
293
293
294
// --- Test Invalid Config Values ---
294
295
invalidValueConfig := WorkerConfig {Interval : 0 , JobName : "wont-apply" }
295
296
t .Logf ("Reloading with invalid value config: %+v" , invalidValueConfig )
296
297
worker .ReloadWithConfig (invalidValueConfig )
297
298
298
299
// Wait and assert config hasn't changed
299
- time .Sleep (50 * time .Millisecond )
300
- currentConfig = readWorkerConfig (worker )
300
+ assert .Eventually (t , func () bool {
301
+ return worker .tickCount .Load () > 0
302
+ }, 1 * time .Second , pollInterval , "Worker did not start ticking" )
303
+ currentConfig = readWorkerConfig (t , worker )
301
304
assert .Equal (
302
305
t ,
303
306
configAfterValidReload ,
304
307
currentConfig ,
305
308
"Config should not change after invalid value reload" ,
306
309
)
307
- assert .Contains (
308
- t ,
309
- logBuf .String (),
310
- "Invalid configuration received in ReloadWithConfig" ,
311
- "Should log error for invalid value" ,
312
- )
313
- assert .Contains (
314
- t ,
315
- logBuf .String (),
316
- "interval must be positive" ,
317
- "Should log specific validation error" ,
318
- )
319
310
}
320
311
321
312
// TestWorker_Execution_Timing tests that the worker ticks according to the configured interval,
@@ -411,7 +402,7 @@ func TestWorker_Execution_Timing(t *testing.T) {
411
402
412
403
// Ensure config is applied before measuring again
413
404
require .Eventually (t , func () bool {
414
- return readWorkerConfig (worker ).Interval == reloadedInterval
405
+ return readWorkerConfig (t , worker ).Interval == reloadedInterval
415
406
}, 1 * time .Second , pollInterval , "Config interval did not update after reload" )
416
407
t .Log ("Config interval updated." )
417
408
@@ -481,7 +472,7 @@ func TestWorker_ReloadWithConfig_Concurrency(t *testing.T) {
481
472
finalConfig := WorkerConfig {} // Store the config from the last reload goroutine
482
473
483
474
// Launch concurrent reloads
484
- for i := 0 ; i < numReloads ; i ++ {
475
+ for i := range numReloads {
485
476
go func (index int ) {
486
477
defer reloadWg .Done ()
487
478
// Vary interval and name slightly for each reload
@@ -494,8 +485,6 @@ func TestWorker_ReloadWithConfig_Concurrency(t *testing.T) {
494
485
finalConfig = cfg
495
486
}
496
487
worker .ReloadWithConfig (cfg )
497
- // Add a small random sleep to increase inter-leaving chances
498
- // time.Sleep(time.Duration(rand.Intn(5)) * time.Millisecond)
499
488
}(i )
500
489
}
501
490
@@ -509,7 +498,7 @@ func TestWorker_ReloadWithConfig_Concurrency(t *testing.T) {
509
498
time .Sleep (2 * time .Second )
510
499
511
500
t .Logf ("Final config details - Expected: %v, Current: %v" ,
512
- finalConfig .Interval , readWorkerConfig (worker ).Interval )
501
+ finalConfig .Interval , readWorkerConfig (t , worker ).Interval )
513
502
514
503
// Because ReloadWithConfig replaces the config in the channel if full,
515
504
// we expect the *last* successfully queued config to eventually be applied.
@@ -524,7 +513,7 @@ func TestWorker_ReloadWithConfig_Concurrency(t *testing.T) {
524
513
525
514
t .Logf (
526
515
"Final applied config interval: %v (expected: %v)" ,
527
- readWorkerConfig (worker ).Interval ,
516
+ readWorkerConfig (t , worker ).Interval ,
528
517
finalConfig .Interval ,
529
518
)
530
519
// Check tick count is still advancing (worker didn't get stuck)
0 commit comments