Skip to content

Commit 1dd4d4f

Browse files
Add env var to override common timeouts in tests
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 <errordeveloper@gmail.com>
1 parent 14ee2ce commit 1dd4d4f

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,7 @@ jobs:
3838
- run: go vet ./...
3939
# - run: fgt /home/runner/go/bin/lint ./...
4040
- run: go test -v -race ./...
41+
env:
42+
LIBKV_TEST_SHORT_TIMEOUT: 10s
4143
- run: script/coverage
4244
- run: goveralls -service=travis-ci -coverprofile=goverage.report

testutils/utils.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@ package testutils
22

33
import (
44
"fmt"
5+
"os"
56
"testing"
67
"time"
78

89
"github.com/docker/libkv/store"
910
"github.com/stretchr/testify/assert"
1011
)
1112

13+
func getShortTimeout() time.Duration {
14+
if timeout := os.Getenv("LIBKV_TEST_SHORT_TIMEOUT"); timeout != "" {
15+
if timeout, err := time.ParseDuration(timeout); err != nil {
16+
return timeout
17+
}
18+
}
19+
return 5 * time.Second
20+
}
21+
1222
// RunTestCommon tests the minimal required APIs which
1323
// should be supported by all K/V backends
1424
func RunTestCommon(t *testing.T, kv store.Store) {
@@ -184,7 +194,7 @@ func testWatch(t *testing.T, kv store.Store) {
184194
if eventCount >= 4 {
185195
return
186196
}
187-
case <-time.After(4 * time.Second):
197+
case <-time.After(getShortTimeout()):
188198
t.Fatal("Timeout reached")
189199
return
190200
}
@@ -240,7 +250,7 @@ func testWatchTree(t *testing.T, kv store.Store) {
240250
return
241251
}
242252
eventCount++
243-
case <-time.After(4 * time.Second):
253+
case <-time.After(getShortTimeout()):
244254
t.Fatal("Timeout reached")
245255
return
246256
}
@@ -457,7 +467,7 @@ func testLockTTL(t *testing.T, kv store.Store, otherConn store.Store) {
457467
select {
458468
case _ = <-done:
459469
t.Fatal("Lock succeeded on a key that is supposed to be locked by another client")
460-
case <-time.After(4 * time.Second):
470+
case <-time.After(getShortTimeout()):
461471
// Stop requesting the lock as we are blocked as expected
462472
stop <- struct{}{}
463473
break
@@ -484,7 +494,7 @@ func testLockTTL(t *testing.T, kv store.Store, otherConn store.Store) {
484494
select {
485495
case _ = <-locked:
486496
break
487-
case <-time.After(4 * time.Second):
497+
case <-time.After(getShortTimeout()):
488498
t.Fatal("Unable to take the lock, timed out")
489499
}
490500

@@ -539,7 +549,7 @@ func testLockWait(t *testing.T, kv, otherConn store.Store) {
539549
select {
540550
case <-gotLock2:
541551
t.FailNow() // The other client should not have the lock.
542-
case <-time.After(5 * time.Second):
552+
case <-time.After(getShortTimeout()):
543553
// Success! The other client is still waiting.
544554
}
545555

@@ -551,7 +561,7 @@ func testLockWait(t *testing.T, kv, otherConn store.Store) {
551561
select {
552562
case <-gotLock2:
553563
// Success! The other client has acquired the lock.
554-
case <-time.After(5 * time.Second):
564+
case <-time.After(getShortTimeout()):
555565
t.FailNow() // The other client should no longer be blocking.
556566
}
557567
assert.NoError(t, lock2.Unlock())

0 commit comments

Comments
 (0)