Skip to content

Commit

Permalink
Add env var to override common timeouts in tests
Browse files Browse the repository at this point in the history
There had been 4s & 5s timeouts that can lead to test failure, this
rounds up both of these to 5s and provide LIBKV_TEST_SHORT_TIMEOUT
for CI to override if needed.

Signed-off-by: Ilya Dmitrichenko <[email protected]>
  • Loading branch information
errordeveloper committed Dec 17, 2021
1 parent 2a16f79 commit c11ccde
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ package testutils

import (
"fmt"
"os"
"testing"
"time"

"github.com/docker/libkv/store"
"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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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")
}

Expand Down Expand Up @@ -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.
}

Expand All @@ -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())
Expand Down

0 comments on commit c11ccde

Please sign in to comment.