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

fix flaky integration test: TestDatabaseAccessMongoConnectionCount #10869

Merged
merged 2 commits into from
Mar 8, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions integration/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func TestDatabaseAccessMongoRootCluster(t *testing.T) {
func TestDatabaseAccessMongoConnectionCount(t *testing.T) {
pack := setupDatabaseTest(t)

connectMongoClient := func(t *testing.T) {
connectMongoClient := func(t *testing.T) (serverConnectionCount int32) {
// Connect to the database service in root cluster.
client, err := mongodb.MakeTestClient(context.Background(), common.TestClientConfig{
AuthClient: pack.root.cluster.GetSiteAPI(pack.root.cluster.Secrets.SiteName),
Expand All @@ -231,23 +231,39 @@ func TestDatabaseAccessMongoConnectionCount(t *testing.T) {
},
})
require.NoError(t, err)

// Execute a query.
_, err = client.Database("test").Collection("test").Find(context.Background(), bson.M{})
require.NoError(t, err)

// Get a server connection count before disconnect.
serverConnectionCount = pack.root.mongo.GetActiveConnectionsCount()

// Disconnect.
err = client.Disconnect(context.Background())
require.NoError(t, err)

return serverConnectionCount
}
connectMongoClient(t)

// Get connection count after mongo driver indicated the connection pool.
initialConnectionCount := pack.root.mongo.GetActiveConnectionsCount()
// Get connection count while the first client is connected.
initialConnectionCount := connectMongoClient(t)

// Check if active connections count is not growing over time when new
// clients connect to the mongo server.
clientCount := 8
for i := 0; i < clientCount; i++ {
connectMongoClient(t)
// Note that connection count per client fluctuates between 6 and 9.
// Use InDelta to avoid flaky test.
require.InDelta(t, initialConnectionCount, connectMongoClient(t), 3)
}

// Wait until the server reports no more connections. This usually happens
// really quick but wait a little longer just in case.
waitUntilNoConnections := func() bool {
return 0 == pack.root.mongo.GetActiveConnectionsCount()
}
// Check if active connections count is not growing over time when new clients connect to the mongo server.
require.Equal(t, initialConnectionCount, pack.root.mongo.GetActiveConnectionsCount())
require.Eventually(t, waitUntilNoConnections, 5*time.Second, 100*time.Millisecond)
}

// TestDatabaseAccessMongoLeafCluster tests a scenario where a user connects
Expand Down