Skip to content

Commit

Permalink
Reduce TestInitDatabaseService flakiness (#49477)
Browse files Browse the repository at this point in the history
* test(service): assert process exit status

* test(service): add cleanup to ensure server is closed

* chore(service): typo
  • Loading branch information
gabrielcorado authored Dec 11, 2024
1 parent cf407fa commit 12f0d12
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lib/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand All @@ -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())
})
}
}
Expand Down

0 comments on commit 12f0d12

Please sign in to comment.