-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The core change is moving the context out of the `ServiceRunner` struct to be a local variable, and using a channel to notify about shutdown events. Add more synchronization between Run and the moment service started to avoid mis-identifying not running (yet) service as successfully finished. Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com> Co-authored-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
- Loading branch information
Showing
10 changed files
with
1,693 additions
and
1,536 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package system | ||
|
||
import ( | ||
"github.com/siderolabs/talos/internal/app/machined/pkg/runtime" | ||
"github.com/siderolabs/talos/pkg/conditions" | ||
) | ||
|
||
func NewServices(runtime runtime.Runtime) *singleton { //nolint:revive | ||
return newServices(runtime) | ||
} | ||
|
||
func WaitForServiceWithInstance(instance *singleton, event StateEvent, service string) conditions.Condition { | ||
return waitForService(instance, event, service) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package system_test | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/siderolabs/talos/internal/app/machined/pkg/runtime" | ||
"github.com/siderolabs/talos/internal/app/machined/pkg/system" | ||
"github.com/siderolabs/talos/internal/app/machined/pkg/system/events" | ||
"github.com/siderolabs/talos/internal/app/machined/pkg/system/runner" | ||
"github.com/siderolabs/talos/internal/app/machined/pkg/system/runner/goroutine" | ||
"github.com/siderolabs/talos/pkg/conditions" | ||
) | ||
|
||
type TestCondition struct{} | ||
|
||
func (TestCondition) String() string { | ||
return "test-condition" | ||
} | ||
|
||
func (TestCondition) Wait(ctx context.Context) error { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case <-time.After(10 * time.Millisecond): | ||
return nil | ||
} | ||
} | ||
|
||
type TestService struct{} | ||
|
||
func (TestService) ID(runtime.Runtime) string { | ||
return "test-service" | ||
} | ||
|
||
func (TestService) PreFunc(ctx context.Context, r runtime.Runtime) error { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func (TestService) Runner(r runtime.Runtime) (runner.Runner, error) { | ||
return goroutine.NewRunner(r, "test-service", func(ctx context.Context, r runtime.Runtime, logOutput io.Writer) error { | ||
<-ctx.Done() | ||
|
||
return nil | ||
}), nil | ||
} | ||
|
||
func (TestService) PostFunc(runtime.Runtime, events.ServiceState) error { | ||
return nil | ||
} | ||
|
||
func (TestService) Condition(runtime.Runtime) conditions.Condition { | ||
return TestCondition{} | ||
} | ||
|
||
func (TestService) DependsOn(runtime.Runtime) []string { | ||
return nil | ||
} | ||
|
||
func TestRestartService(t *testing.T) { | ||
deadline, ok := t.Deadline() | ||
if !ok { | ||
deadline = time.Now().Add(15 * time.Second) | ||
} | ||
|
||
ctx, cancel := context.WithDeadline(context.Background(), deadline) | ||
defer cancel() | ||
|
||
services := system.NewServices(nil) | ||
|
||
services.Load(TestService{}) | ||
|
||
for i := 0; i < 100; i++ { | ||
require.NoError(t, services.Start("test-service")) | ||
|
||
require.NoError(t, system.WaitForServiceWithInstance(services, system.StateEventUp, "test-service").Wait(ctx)) | ||
|
||
require.NoError(t, services.Stop(ctx, "test-service")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.