diff --git a/phpmainthread_test.go b/phpmainthread_test.go index d274991863..54722b0963 100644 --- a/phpmainthread_test.go +++ b/phpmainthread_test.go @@ -95,8 +95,9 @@ func TestTransitionThreadsWhileDoingRequests(t *testing.T) { t.Cleanup(Shutdown) var ( - isDone atomic.Bool - wg sync.WaitGroup + isDone atomic.Bool + wg sync.WaitGroup + transitionsWG sync.WaitGroup ) numThreads := 10 @@ -122,8 +123,10 @@ func TestTransitionThreadsWhileDoingRequests(t *testing.T) { // try all possible permutations of transition, transition every ms transitions := allPossibleTransitions(worker1Path, worker2Path) + transitionsWG.Add(numThreads) for i := range numThreads { go func(thread *phpThread, start int) { + defer transitionsWG.Done() for { for j := start; j < len(transitions); j++ { if isDone.Load() { @@ -158,6 +161,9 @@ func TestTransitionThreadsWhileDoingRequests(t *testing.T) { // we are finished as soon as all 1000 requests are done wg.Wait() isDone.Store(true) + // wait for transition goroutines to exit before Shutdown to avoid them + // racing with a subsequent test's initPHPThreads via mainThread.state + transitionsWG.Wait() } func TestFinishBootingAWorkerScript(t *testing.T) {