From 42eca5315b6bd3d822e565aa4d4df1a77e018224 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Fri, 11 Sep 2020 18:33:09 +0530 Subject: [PATCH] MM-27957: Fix flaky test TestRecycleDBConns (#15415) https://mattermost.atlassian.net/browse/MM-27957 --- store/sqlstore/supplier_test.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/store/sqlstore/supplier_test.go b/store/sqlstore/supplier_test.go index 69d4cea6d6..5f3bd45bb4 100644 --- a/store/sqlstore/supplier_test.go +++ b/store/sqlstore/supplier_test.go @@ -269,10 +269,22 @@ func TestRecycleDBConns(t *testing.T) { assert.Equal(t, 0, int(stats.MaxLifetimeClosed), "unexpected number of connections closed due to maxlifetime") supplier.RecycleDBConnections(2 * time.Second) - // We cannot reliably control exactly how many open connections are there. So we - // just do a basic check and confirm that atleast one has been closed. - stats = supplier.GetMaster().Db.Stats() - assert.Greater(t, int(stats.MaxLifetimeClosed), 0, "unexpected number of connections closed due to maxlifetime") + var success bool + // We try 3 times to let the connections be closed. + // Because sometimes, there can be significant goroutine contention which does not + // give enough time for the connection cleaner goroutine to run. + for i := 0; i < 3; i++ { + // We cannot reliably control exactly how many open connections are there. So we + // just do a basic check and confirm that atleast one has been closed. + stats = supplier.GetMaster().Db.Stats() + if int(stats.MaxLifetimeClosed) == 0 { + time.Sleep(2 * time.Second) + continue + } + success = true + break + } + assert.True(t, success, "No connections were closed due to maxlifetime") }) } }