Skip to content

Commit 10c4591

Browse files
committed
fix data race in telegram tests
Wait for tg.Run() goroutine to complete before test returns to prevent race condition when next test modifies global apiPollInterval variable.
1 parent bbbea87 commit 10c4591

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

provider/telegram_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,16 @@ func TestTelegram_ProcessUpdateFlow(t *testing.T) {
309309
assert.Equal(t, "my_auth_bot", resp.Bot)
310310

311311
ctx, cancel := context.WithCancel(context.Background())
312-
defer cancel()
313-
go tg.Run(ctx)
312+
runDone := make(chan struct{})
313+
go func() {
314+
_ = tg.Run(ctx)
315+
close(runDone)
316+
}()
314317
assert.Eventually(t, func() bool {
315318
return tg.ProcessUpdate(ctx, "").Error() == "Run goroutine should not be used with ProcessUpdate"
316319
}, time.Millisecond*100, time.Millisecond*10, "ProcessUpdate should not work same time as Run")
320+
cancel()
321+
<-runDone
317322
}
318323

319324
func TestTelegram_TokenVerification(t *testing.T) {

v2/provider/telegram_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,16 @@ func TestTelegram_ProcessUpdateFlow(t *testing.T) {
309309
assert.Equal(t, "my_auth_bot", resp.Bot)
310310

311311
ctx, cancel := context.WithCancel(context.Background())
312-
defer cancel()
313-
go tg.Run(ctx)
312+
runDone := make(chan struct{})
313+
go func() {
314+
_ = tg.Run(ctx)
315+
close(runDone)
316+
}()
314317
assert.Eventually(t, func() bool {
315318
return tg.ProcessUpdate(ctx, "").Error() == "Run goroutine should not be used with ProcessUpdate"
316319
}, time.Millisecond*100, time.Millisecond*10, "ProcessUpdate should not work same time as Run")
320+
cancel()
321+
<-runDone
317322
}
318323

319324
func TestTelegram_TokenVerification(t *testing.T) {

0 commit comments

Comments
 (0)