(t *testing.T)
| 148 | } |
| 149 | |
| 150 | func TestConnectAndWait(t *testing.T) { |
| 151 | t.Run("calls cancel func on EOF", func(t *testing.T) { |
| 152 | srv, err := NewPluginServer(nil) |
| 153 | assert.NilError(t, err, "failed to setup server") |
| 154 | defer srv.Close() |
| 155 | |
| 156 | done := make(chan struct{}) |
| 157 | t.Setenv(EnvKey, srv.Addr().String()) |
| 158 | cancelFunc := func() { |
| 159 | done <- struct{}{} |
| 160 | } |
| 161 | ConnectAndWait(cancelFunc) |
| 162 | |
| 163 | select { |
| 164 | case <-done: |
| 165 | t.Fatal("unexpectedly done") |
| 166 | default: |
| 167 | } |
| 168 | |
| 169 | srv.Close() |
| 170 | |
| 171 | select { |
| 172 | case <-done: |
| 173 | case <-time.After(10 * time.Millisecond): |
| 174 | t.Fatal("cancel function not closed after 10ms") |
| 175 | } |
| 176 | }) |
| 177 | |
| 178 | // TODO: this test cannot be executed with `t.Parallel()`, due to |
| 179 | // relying on goroutine numbers to ensure correct behaviour |
| 180 | t.Run("connect goroutine exits after EOF", func(t *testing.T) { |
| 181 | runtime.LockOSThread() |
| 182 | defer runtime.UnlockOSThread() |
| 183 | srv, err := NewPluginServer(nil) |
| 184 | assert.NilError(t, err, "failed to setup server") |
| 185 | |
| 186 | defer srv.Close() |
| 187 | |
| 188 | t.Setenv(EnvKey, srv.Addr().String()) |
| 189 | |
| 190 | runtime.Gosched() |
| 191 | numGoroutines := runtime.NumGoroutine() |
| 192 | |
| 193 | ConnectAndWait(func() {}) |
| 194 | |
| 195 | runtime.Gosched() |
| 196 | poll.WaitOn(t, func(t poll.LogT) poll.Result { |
| 197 | // +1 goroutine for the poll.WaitOn |
| 198 | // +1 goroutine for the connect goroutine |
| 199 | if runtime.NumGoroutine() < numGoroutines+1+1 { |
| 200 | return poll.Continue("waiting for connect goroutine to spawn") |
| 201 | } |
| 202 | return poll.Success() |
| 203 | }, poll.WithDelay(1*time.Millisecond), poll.WithTimeout(500*time.Millisecond)) |
| 204 | |
| 205 | srv.Close() |
| 206 | |
| 207 | runtime.Gosched() |
nothing calls this directly
no test coverage detected
searching dependent graphs…