diff --git a/testutils/utils.go b/testutils/utils.go index 842d9536..e22b5301 100644 --- a/testutils/utils.go +++ b/testutils/utils.go @@ -2,6 +2,7 @@ package testutils import ( "fmt" + "os" "testing" "time" @@ -9,6 +10,15 @@ import ( "github.com/stretchr/testify/assert" ) +func getShortTimeout() time.Duration { + if timeout := os.Getenv("LIBKV_TEST_SHORT_TIMEOUT"); timeout != "" { + if timeout, err := time.ParseDuration(timeout); err != nil { + return timeout + } + } + return 5 * time.Second +} + // RunTestCommon tests the minimal required APIs which // should be supported by all K/V backends func RunTestCommon(t *testing.T, kv store.Store) { @@ -184,7 +194,7 @@ func testWatch(t *testing.T, kv store.Store) { if eventCount >= 4 { return } - case <-time.After(4 * time.Second): + case <-time.After(getShortTimeout()): t.Fatal("Timeout reached") return } @@ -240,7 +250,7 @@ func testWatchTree(t *testing.T, kv store.Store) { return } eventCount++ - case <-time.After(4 * time.Second): + case <-time.After(getShortTimeout()): t.Fatal("Timeout reached") return } @@ -457,7 +467,7 @@ func testLockTTL(t *testing.T, kv store.Store, otherConn store.Store) { select { case _ = <-done: t.Fatal("Lock succeeded on a key that is supposed to be locked by another client") - case <-time.After(4 * time.Second): + case <-time.After(getShortTimeout()): // Stop requesting the lock as we are blocked as expected stop <- struct{}{} break @@ -484,7 +494,7 @@ func testLockTTL(t *testing.T, kv store.Store, otherConn store.Store) { select { case _ = <-locked: break - case <-time.After(4 * time.Second): + case <-time.After(getShortTimeout()): t.Fatal("Unable to take the lock, timed out") } @@ -539,7 +549,7 @@ func testLockWait(t *testing.T, kv, otherConn store.Store) { select { case <-gotLock2: t.FailNow() // The other client should not have the lock. - case <-time.After(5 * time.Second): + case <-time.After(getShortTimeout()): // Success! The other client is still waiting. } @@ -551,7 +561,7 @@ func testLockWait(t *testing.T, kv, otherConn store.Store) { select { case <-gotLock2: // Success! The other client has acquired the lock. - case <-time.After(5 * time.Second): + case <-time.After(getShortTimeout()): t.FailNow() // The other client should no longer be blocking. } assert.NoError(t, lock2.Unlock())