Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[modules]: add test for ordered shutdown #240

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions modules/modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ 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 {
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 +284,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 +304,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 +329,14 @@ 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"}

close(stopChanOrder)

for i, stopID := range <-stopChanOrder {
zalegrala marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, expectedStopOrder[i], string(stopID))
}
}

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