Skip to content

Commit 12f0d12

Browse files
Reduce TestInitDatabaseService flakiness (#49477)
* test(service): assert process exit status * test(service): add cleanup to ensure server is closed * chore(service): typo
1 parent cf407fa commit 12f0d12

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

lib/service/service_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/jonboulle/clockwork"
4242
"github.com/sirupsen/logrus"
4343
"github.com/stretchr/testify/require"
44+
"golang.org/x/sync/errgroup"
4445
"google.golang.org/grpc"
4546
"google.golang.org/grpc/credentials"
4647
"google.golang.org/grpc/credentials/insecure"
@@ -1818,19 +1819,28 @@ func TestInitDatabaseService(t *testing.T) {
18181819
cfg.Databases.Enabled = test.enabled
18191820
cfg.Databases.Databases = test.databases
18201821

1822+
// This timeout should consider time to receive the event + shutdown
1823+
// time.
1824+
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
1825+
defer cancel()
1826+
1827+
var eg errgroup.Group
18211828
process, err := NewTeleport(cfg)
18221829
require.NoError(t, err)
1830+
require.NoError(t, process.Start())
1831+
eg.Go(func() error { return process.WaitForSignals(ctx, nil) })
1832+
// Ensures the process is closed in failure scenarios.
18231833
t.Cleanup(func() {
1824-
require.NoError(t, process.Close())
1834+
cancel()
1835+
_ = eg.Wait()
18251836
})
1826-
require.NoError(t, process.Start())
1827-
1828-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1829-
defer cancel()
18301837

18311838
if !test.expectErr {
18321839
_, err := process.WaitForEvent(ctx, TeleportReadyEvent)
18331840
require.NoError(t, err)
1841+
require.NoError(t, process.Close())
1842+
// Expect Teleport to shutdown without reporting any issue.
1843+
require.NoError(t, eg.Wait())
18341844
return
18351845
}
18361846

@@ -1840,6 +1850,9 @@ func TestInitDatabaseService(t *testing.T) {
18401850
exitPayload, ok := event.Payload.(ExitEventPayload)
18411851
require.True(t, ok, "expected ExitEventPayload but got %T", event.Payload)
18421852
require.Equal(t, "db.init", exitPayload.Service.Name())
1853+
// Database service init is a critical service, meaning failures on
1854+
// it should cause the process to exit with error.
1855+
require.Error(t, eg.Wait())
18431856
})
18441857
}
18451858
}

0 commit comments

Comments
 (0)