From b6542807ba73d8dc0e38a011f3ab7e4d5c9fd8c0 Mon Sep 17 00:00:00 2001 From: "STeve (Xin) Huang" Date: Tue, 8 Mar 2022 15:59:11 -0500 Subject: [PATCH] flaky test: TestDatabaseAccessMongoConnectionCount (#10869) (#10955) --- integration/db_integration_test.go | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/integration/db_integration_test.go b/integration/db_integration_test.go index dcf31fd27a143..72f2d942698a0 100644 --- a/integration/db_integration_test.go +++ b/integration/db_integration_test.go @@ -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