Skip to content

Commit 56c3417

Browse files
test(cmd): replace time.Sleep with channel synchronization in concurrent update test
- Add scheduledStarted and scheduledDone channels to coordinate goroutine execution - Replace fixed time.Sleep with close(scheduledStarted) and wait on scheduledDone - Ensure full API update is rejected while scheduled update holds the lock
1 parent a612e89 commit 56c3417

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

cmd/root_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ func TestConcurrentScheduledAndFullAPIUpdate(t *testing.T) {
356356
updateLock := make(chan bool, 1)
357357
updateLock <- true
358358

359+
scheduledStarted := make(chan struct{})
360+
scheduledDone := make(chan struct{})
361+
359362
updateFn := func(_ context.Context, _, _ []string) *metrics.Metric {
360363
t.Error("API update function should not be called when lock is held for full updates")
361364

@@ -367,11 +370,16 @@ func TestConcurrentScheduledAndFullAPIUpdate(t *testing.T) {
367370
go func() {
368371
v := <-updateLock
369372

370-
time.Sleep(50 * time.Millisecond)
373+
close(scheduledStarted)
374+
375+
// Hold the lock until the API request has been asserted, then release.
376+
<-scheduledDone
371377

372378
updateLock <- v
373379
}()
374380

381+
<-scheduledStarted
382+
375383
testApp := fiber.New(fiber.Config{})
376384
testApp.Post(handler.Path, handler.Handle)
377385

@@ -390,6 +398,8 @@ func TestConcurrentScheduledAndFullAPIUpdate(t *testing.T) {
390398

391399
assert.Equal(t, http.StatusTooManyRequests, resp.StatusCode,
392400
"full API update should be rejected with 429 while scheduled update holds the lock")
401+
402+
close(scheduledDone)
393403
}
394404

395405
func TestHandleAsync(t *testing.T) {

0 commit comments

Comments
 (0)