From 41616e68972190eaeddacb6f379a7b688193a943 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Wed, 9 Sep 2020 10:51:54 +0530 Subject: [PATCH] MM-27957: Fix flaky test TestRecycleDBConns 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 85fbcb6abffae..803c0cae8d843 100644 --- a/store/sqlstore/supplier_test.go +++ b/store/sqlstore/supplier_test.go @@ -239,10 +239,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") }) } }