try all possible handler transitions takes around 200ms and is supposed to force race conditions
(t *testing.T)
| 92 | // try all possible handler transitions |
| 93 | // takes around 200ms and is supposed to force race conditions |
| 94 | func TestTransitionThreadsWhileDoingRequests(t *testing.T) { |
| 95 | t.Cleanup(Shutdown) |
| 96 | |
| 97 | var ( |
| 98 | isDone atomic.Bool |
| 99 | wg sync.WaitGroup |
| 100 | transitionsWG sync.WaitGroup |
| 101 | ) |
| 102 | |
| 103 | numThreads := 10 |
| 104 | numRequestsPerThread := 100 |
| 105 | worker1Path := filepath.Join(testDataPath, "transition-worker-1.php") |
| 106 | worker1Name := "worker-1" |
| 107 | worker2Path := filepath.Join(testDataPath, "transition-worker-2.php") |
| 108 | worker2Name := "worker-2" |
| 109 | |
| 110 | assert.NoError(t, Init( |
| 111 | WithNumThreads(numThreads), |
| 112 | WithWorkers(worker1Name, worker1Path, 1, |
| 113 | WithWorkerEnv(map[string]string{"ENV1": "foo"}), |
| 114 | WithWorkerWatchMode([]string{}), |
| 115 | WithWorkerMaxFailures(0), |
| 116 | ), |
| 117 | WithWorkers(worker2Name, worker2Path, 1, |
| 118 | WithWorkerEnv(map[string]string{"ENV1": "foo"}), |
| 119 | WithWorkerWatchMode([]string{}), |
| 120 | WithWorkerMaxFailures(0), |
| 121 | ), |
| 122 | )) |
| 123 | |
| 124 | // try all possible permutations of transition, transition every ms |
| 125 | transitions := allPossibleTransitions(worker1Path, worker2Path) |
| 126 | transitionsWG.Add(numThreads) |
| 127 | for i := range numThreads { |
| 128 | go func(thread *phpThread, start int) { |
| 129 | defer transitionsWG.Done() |
| 130 | for { |
| 131 | for j := start; j < len(transitions); j++ { |
| 132 | if isDone.Load() { |
| 133 | return |
| 134 | } |
| 135 | transitions[j](thread) |
| 136 | time.Sleep(time.Millisecond) |
| 137 | } |
| 138 | start = 0 |
| 139 | } |
| 140 | }(phpThreads[i], i) |
| 141 | } |
| 142 | |
| 143 | // randomly do requests to the 3 endpoints |
| 144 | wg.Add(numThreads) |
| 145 | for i := range numThreads { |
| 146 | go func(i int) { |
| 147 | for range numRequestsPerThread { |
| 148 | switch rand.IntN(3) { |
| 149 | case 0: |
| 150 | assertRequestBody(t, "http://localhost/transition-worker-1.php", "Hello from worker 1") |
| 151 | case 1: |
nothing calls this directly
no test coverage detected