BaseStartStop tests that need specific internal implementation (like ones we can add to sampleService) to be able to verify.
(t *testing.T)
| 218 | // BaseStartStop tests that need specific internal implementation (like ones we |
| 219 | // can add to sampleService) to be able to verify. |
| 220 | func TestSampleService(t *testing.T) { |
| 221 | t.Parallel() |
| 222 | |
| 223 | ctx := context.Background() |
| 224 | |
| 225 | type testBundle struct{} |
| 226 | |
| 227 | setup := func(t *testing.T) (*sampleService, *testBundle) { //nolint:unparam |
| 228 | t.Helper() |
| 229 | |
| 230 | return &sampleService{}, &testBundle{} |
| 231 | } |
| 232 | |
| 233 | t.Run("StartedChannel", func(t *testing.T) { |
| 234 | t.Parallel() |
| 235 | |
| 236 | service, _ := setup(t) |
| 237 | |
| 238 | require.NoError(t, service.Start(ctx)) |
| 239 | t.Cleanup(service.Stop) |
| 240 | |
| 241 | riversharedtest.WaitOrTimeout(t, service.Started()) |
| 242 | require.True(t, service.state) |
| 243 | }) |
| 244 | |
| 245 | t.Run("StartError", func(t *testing.T) { |
| 246 | t.Parallel() |
| 247 | |
| 248 | service, _ := setup(t) |
| 249 | service.startErr = errors.New("error on start") |
| 250 | |
| 251 | require.ErrorIs(t, service.Start(ctx), service.startErr) |
| 252 | |
| 253 | riversharedtest.WaitOrTimeout(t, service.Started()) // start channel also closed on erroneous start |
| 254 | riversharedtest.WaitOrTimeout(t, service.Stopped()) |
| 255 | }) |
| 256 | |
| 257 | t.Run("StartErrorThenSuccessfulRestart", func(t *testing.T) { |
| 258 | t.Parallel() |
| 259 | |
| 260 | service, _ := setup(t) |
| 261 | service.startErr = errors.New("error on start") |
| 262 | |
| 263 | // First start fails with our simulated error. |
| 264 | require.ErrorIs(t, service.Start(ctx), service.startErr) |
| 265 | |
| 266 | riversharedtest.WaitOrTimeout(t, service.Started()) |
| 267 | riversharedtest.WaitOrTimeout(t, service.Stopped()) |
| 268 | |
| 269 | // Clear error so the next start succeeds. |
| 270 | service.startErr = nil |
| 271 | |
| 272 | // Second start should succeed despite the prior failure. Without the |
| 273 | // reset-on-failed-start logic in StartInit, isRunning would still be |
| 274 | // true and StartInit would return shouldStart=false, causing Start to |
| 275 | // return nil without actually starting the service. |
| 276 | require.NoError(t, service.Start(ctx)) |
| 277 | t.Cleanup(service.Stop) |
nothing calls this directly
no test coverage detected
searching dependent graphs…