Skip to content

Commit

Permalink
[modules]: add test for ordered shutdown
Browse files Browse the repository at this point in the history
Signed-off-by: Zach Leslie <[email protected]>
  • Loading branch information
zalegrala committed Dec 2, 2022
1 parent 45ea666 commit be5f4bb
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions modules/modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ import (

func mockInitFunc() (services.Service, error) { return services.NewIdleService(nil, nil), nil }

func mockStoppingFunc(id string, stopChan chan string) services.StoppingFn {
return func(_ error) error {
fmt.Printf("Stopping %s", id)
stopChan <- id
return nil
}
}

func mockInitFuncFail() (services.Service, error) { return nil, errors.New("Error") }

func TestDependencies(t *testing.T) {
Expand Down Expand Up @@ -277,13 +285,15 @@ func TestManager_inverseDependenciesForModule(t *testing.T) {
func TestModuleWaitsForAllDependencies(t *testing.T) {
var serviceA services.Service

stopChanOrder := make(chan string, 2)

initA := func() (services.Service, error) {
serviceA = services.NewIdleService(func(serviceContext context.Context) error {
// Slow-starting service. Delay is here to verify that service for C is not started before this service
// has finished starting.
time.Sleep(1 * time.Second)
return nil
}, nil)
}, mockStoppingFunc("a", stopChanOrder))

return serviceA, nil
}
Expand All @@ -295,7 +305,7 @@ func TestModuleWaitsForAllDependencies(t *testing.T) {
return fmt.Errorf("serviceA has invalid state: %v", s)
}
return nil
}, nil), nil
}, mockStoppingFunc("c", stopChanOrder)), nil
}

m := NewManager(log.NewNopLogger())
Expand All @@ -320,6 +330,12 @@ func TestModuleWaitsForAllDependencies(t *testing.T) {
require.NoError(t, err)
assert.NoError(t, services.StartManagerAndAwaitHealthy(context.Background(), servManager))
assert.NoError(t, services.StopManagerAndAwaitStopped(context.Background(), servManager))

expectedStopOrder := []string{"c", "a"}

for i, stopID := range <-stopChanOrder {
assert.Equal(t, expectedStopOrder[i], string(stopID))
}
}

func getStopDependenciesForModule(module string, services map[string]services.Service) []string {
Expand Down

0 comments on commit be5f4bb

Please sign in to comment.