Skip to content

Commit

Permalink
flaky test: TestDatabaseAccessMongoConnectionCount (#10869) (#10955)
Browse files Browse the repository at this point in the history
  • Loading branch information
greedy52 authored Mar 8, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 47dfc67 commit b654280
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions integration/db_integration_test.go
Original file line number Diff line number Diff line change
@@ -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),
@@ -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

0 comments on commit b654280

Please sign in to comment.