MCPcopy
hub / github.com/grafana/dskit / TestModuleService_InterruptedFastStartup

Function TestModuleService_InterruptedFastStartup

modules/modules_test.go:354–393  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

352}
353
354func TestModuleService_InterruptedFastStartup(t *testing.T) {
355 finishStarting := make(chan struct{})
356 subserviceStarted := make(chan struct{})
357 subserviceStopped := false
358
359 subService := services.NewBasicService(func(_ context.Context) error {
360 // We want to control the execution of this function via the test code,
361 // so ignore the passed context because it will be cancelled shortly after entering this function.
362 close(subserviceStarted)
363 <-finishStarting
364 return nil
365 }, func(serviceContext context.Context) error {
366 <-serviceContext.Done()
367 return nil
368 }, func(error) error {
369 subserviceStopped = true
370 return nil
371 })
372
373 noDepsFunc := func(string) map[string]services.Service { return nil }
374 moduleSvc := NewModuleService("A", log.NewNopLogger(), subService, noDepsFunc, noDepsFunc)
375
376 ctx, cancel := context.WithCancel(context.Background())
377 go func() {
378 <-subserviceStarted
379 cancel()
380 close(finishStarting)
381 }()
382
383 // Start service using context that's going to be cancelled
384 require.NoError(t, moduleSvc.StartAsync(ctx))
385 // We get context cancelled error from service context cancellation.
386 err := moduleSvc.AwaitRunning(context.Background())
387 require.ErrorIs(t, err, context.Canceled)
388 require.ErrorContains(t, err, "starting module A")
389
390 require.True(t, subserviceStopped)
391 require.Equal(t, services.Failed, moduleSvc.State())
392 require.Equal(t, services.Terminated, subService.State())
393}
394
395func TestModuleService_InterruptedSlowStartup(t *testing.T) {
396 subserviceStarted := make(chan struct{})

Callers

nothing calls this directly

Calls 8

StateMethod · 0.95
NewBasicServiceFunction · 0.92
NewModuleServiceFunction · 0.85
DoneMethod · 0.65
StartAsyncMethod · 0.65
AwaitRunningMethod · 0.65
StateMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected