Skip to content

Commit

Permalink
Stop tests gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Oct 19, 2022
1 parent 50fdced commit df6bb13
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/ferretdb/main.go
Expand Up @@ -210,6 +210,7 @@ func run() {
logger := setupLogger(uuid)

ctx, stop := notifyAppTermination(context.Background())

go func() {
<-ctx.Done()
logger.Info("Stopping...")
Expand Down
1 change: 1 addition & 0 deletions cmd/ferretdb/main_unix.go
Expand Up @@ -23,6 +23,7 @@ import (
"golang.org/x/sys/unix"
)

// notifyAppTermination installs a signal handler that cancels the context.
func notifyAppTermination(parent context.Context) (context.Context, context.CancelFunc) {
return signal.NotifyContext(parent, unix.SIGTERM, unix.SIGINT)
}
1 change: 1 addition & 0 deletions cmd/ferretdb/main_windows.go
Expand Up @@ -22,6 +22,7 @@ import (
"golang.org/x/sys/windows"
)

// notifyAppTermination installs a signal handler that cancels the context.
func notifyAppTermination(parent context.Context) (context.Context, context.CancelFunc) {
return signal.NotifyContext(parent, windows.SIGTERM, windows.SIGINT, os.Interrupt)
}
11 changes: 9 additions & 2 deletions internal/util/testutil/testutil.go
Expand Up @@ -24,6 +24,13 @@ import (
func Ctx(tb testing.TB) context.Context {
tb.Helper()

// TODO handle signals to stop tests gracefully
return context.Background()
ctx, stop := notifyTestsTermination(context.Background())

go func() {
<-ctx.Done()
tb.Log("Stopping...")
stop()
}()

return ctx
}
29 changes: 29 additions & 0 deletions internal/util/testutil/testutil_unix.go
@@ -0,0 +1,29 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !windows

package testutil

import (
"context"
"os/signal"

"golang.org/x/sys/unix"
)

// notifyTestsTermination installs a signal handler that cancels the context.
func notifyTestsTermination(parent context.Context) (context.Context, context.CancelFunc) {
return signal.NotifyContext(parent, unix.SIGTERM, unix.SIGINT)
}
28 changes: 28 additions & 0 deletions internal/util/testutil/testutil_windows.go
@@ -0,0 +1,28 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package testutil

import (
"context"
"os"
"os/signal"

"golang.org/x/sys/windows"
)

// notifyTestsTermination installs a signal handler that cancels the context.
func notifyTestsTermination(parent context.Context) (context.Context, context.CancelFunc) {
return signal.NotifyContext(parent, windows.SIGTERM, windows.SIGINT, os.Interrupt)
}

0 comments on commit df6bb13

Please sign in to comment.