From bf0c0a6a5ae24e7e866af95880975356bf19809c Mon Sep 17 00:00:00 2001 From: Gabriel Corado Date: Thu, 12 Dec 2024 15:22:02 -0300 Subject: [PATCH] Reduce `TestInitDatabaseService` flakiness (#49477) (#50087) --- 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 5c06f83b62dec..33e87d00e1979 100644 --- a/lib/service/service_test.go +++ b/lib/service/service_test.go @@ -42,6 +42,7 @@ import ( "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" "golang.org/x/crypto/ssh" + "golang.org/x/sync/errgroup" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials/insecure" @@ -1819,19 +1820,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 } @@ -1841,6 +1851,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()) }) } }