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

Improve tests cleanup #1287

Merged
merged 2 commits into from Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}
7 changes: 5 additions & 2 deletions internal/handlers/pg/pgdb/query_test.go
Expand Up @@ -87,6 +87,7 @@ func TestQueryDocuments(t *testing.T) {

tx, err := pool.Begin(ctx)
require.NoError(t, err)
defer tx.Rollback(ctx)

for _, doc := range tc.documents {
require.NoError(t, InsertDocument(ctx, tx, databaseName, tc.collection, doc))
Expand Down Expand Up @@ -117,6 +118,7 @@ func TestQueryDocuments(t *testing.T) {
t.Run("cancel_context", func(t *testing.T) {
tx, err := pool.Begin(ctx)
require.NoError(t, err)
defer tx.Rollback(ctx)

for i := 1; i <= FetchedChannelBufSize*FetchedSliceCapacity+1; i++ {
require.NoError(t, InsertDocument(ctx, tx, databaseName, collectionName+"_cancel",
Expand All @@ -125,7 +127,7 @@ func TestQueryDocuments(t *testing.T) {
}

sp := &SQLParam{DB: databaseName, Collection: collectionName + "_cancel"}
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(ctx)
fetchedChan, err := pool.QueryDocuments(ctx, tx, sp)
cancel()
require.NoError(t, err)
Expand All @@ -149,9 +151,10 @@ func TestQueryDocuments(t *testing.T) {
t.Run("non-existing_collection", func(t *testing.T) {
tx, err := pool.Begin(ctx)
require.NoError(t, err)
defer tx.Rollback(ctx)

sp := &SQLParam{DB: databaseName, Collection: collectionName + "_non-existing"}
fetchedChan, err := pool.QueryDocuments(context.Background(), tx, sp)
fetchedChan, err := pool.QueryDocuments(ctx, tx, sp)
require.NoError(t, err)
res, ok := <-fetchedChan
require.False(t, ok)
Expand Down
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)
}