(t *testing.T, tc testCase)
| 186 | } |
| 187 | |
| 188 | func runTestCase(t *testing.T, tc testCase) { |
| 189 | s := newServ(servConf{ |
| 190 | startSleep: time.Second, |
| 191 | startRetVal: tc.startRetVal, |
| 192 | startReturnContextErr: tc.startReturnContextErr, |
| 193 | runSleep: time.Second, |
| 194 | runRetVal: tc.runRetVal, |
| 195 | runReturnContextErr: tc.runReturnContextErr, |
| 196 | stopRetVal: tc.stopRetVal, |
| 197 | }) |
| 198 | |
| 199 | sl := newServiceListener() |
| 200 | require.NoError(t, sl.StartAsync(context.Background())) |
| 201 | require.NoError(t, sl.AwaitRunning(context.Background())) |
| 202 | |
| 203 | s.AddListener(sl) |
| 204 | |
| 205 | require.Equal(t, New, s.State()) |
| 206 | |
| 207 | ctx, servCancel := context.WithCancel(context.Background()) |
| 208 | defer servCancel() // make sure to call cancel at least once |
| 209 | |
| 210 | require.NoError(t, s.StartAsync(ctx), "StartAsync") |
| 211 | require.Error(t, s.StartAsync(ctx), "second StartAsync") // must always return error |
| 212 | if tc.cancelAfterStartAsync { |
| 213 | servCancel() |
| 214 | } |
| 215 | if tc.stopAfterStartAsync { |
| 216 | s.StopAsync() |
| 217 | } |
| 218 | |
| 219 | awaitCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 220 | defer cancel() |
| 221 | |
| 222 | awaitRunningErr := s.AwaitRunning(awaitCtx) |
| 223 | |
| 224 | if len(tc.awaitRunningError) == 0 { |
| 225 | require.NoError(t, awaitRunningErr, "AwaitRunning") |
| 226 | } else { |
| 227 | require.Contains(t, tc.awaitRunningError, awaitRunningErr, "AwaitRunning") |
| 228 | } |
| 229 | |
| 230 | if tc.cancelAfterAwaitRunning { |
| 231 | servCancel() |
| 232 | } |
| 233 | if tc.stopAfterAwaitRunning { |
| 234 | s.StopAsync() |
| 235 | } |
| 236 | |
| 237 | awaitCtx, cancel = context.WithTimeout(context.Background(), 5*time.Second) |
| 238 | defer cancel() |
| 239 | |
| 240 | require.Equal(t, tc.awaitTerminatedError, s.AwaitTerminated(awaitCtx), "AwaitTerminated") |
| 241 | require.Equal(t, tc.failureCase, s.FailureCase(), "FailureCase") |
| 242 | |
| 243 | // get log, and compare against expected |
| 244 | // we can only get log once listener is finished, otherwise we risk race conditions |
| 245 |
no test coverage detected