From 12f0d1253dffb6437e6f0b5c1cd8596f463970ba Mon Sep 17 00:00:00 2001 From: Gabriel Corado Date: Wed, 11 Dec 2024 16:02:37 -0300 Subject: [PATCH] Reduce `TestInitDatabaseService` flakiness (#49477) * test(service): assert process exit status * test(service): add cleanup to ensure server is closed * chore(service): typo --- lib/service/service_test.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/service/service_test.go b/lib/service/service_test.go index e1ddfa148eb5d..1c05149527304 100644 --- a/lib/service/service_test.go +++ b/lib/service/service_test.go @@ -41,6 +41,7 @@ import ( "github.com/jonboulle/clockwork" "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" + "golang.org/x/sync/errgroup" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials/insecure" @@ -1818,19 +1819,28 @@ func TestInitDatabaseService(t *testing.T) { cfg.Databases.Enabled = test.enabled cfg.Databases.Databases = test.databases + // This timeout should consider time to receive the event + shutdown + // time. + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + + var eg errgroup.Group process, err := NewTeleport(cfg) require.NoError(t, err) + require.NoError(t, process.Start()) + eg.Go(func() error { return process.WaitForSignals(ctx, nil) }) + // Ensures the process is closed in failure scenarios. t.Cleanup(func() { - require.NoError(t, process.Close()) + cancel() + _ = eg.Wait() }) - require.NoError(t, process.Start()) - - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() if !test.expectErr { _, err := process.WaitForEvent(ctx, TeleportReadyEvent) require.NoError(t, err) + require.NoError(t, process.Close()) + // Expect Teleport to shutdown without reporting any issue. + require.NoError(t, eg.Wait()) return } @@ -1840,6 +1850,9 @@ func TestInitDatabaseService(t *testing.T) { exitPayload, ok := event.Payload.(ExitEventPayload) require.True(t, ok, "expected ExitEventPayload but got %T", event.Payload) require.Equal(t, "db.init", exitPayload.Service.Name()) + // Database service init is a critical service, meaning failures on + // it should cause the process to exit with error. + require.Error(t, eg.Wait()) }) } }