Skip to content

Commit c7cdc6b

Browse files
test(spinlock): add synctest to wait test
1 parent bf26745 commit c7cdc6b

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,17 @@ endif
121121
.PHONY: test-ci
122122
test-ci:
123123
ifdef cover
124-
$(GO) test -run "[^FLAKY]$$" -coverprofile=cover.out ./...
124+
$(GO) test -run "[^FLAKY]$$" -coverprofile=cover.out ./pkg/spinlock
125125
else
126-
$(GO) test -run "[^FLAKY]$$" ./...
126+
$(GO) test -run "[^FLAKY]$$" ./pkg/spinlock
127127
endif
128128

129129
.PHONY: test-ci-race
130130
test-ci-race:
131131
ifdef cover
132-
$(GO) test -race -run "[^FLAKY]$$" -coverprofile=cover.out ./...
132+
$(GO) test -race -run "[^FLAKY]$$" -coverprofile=cover.out ./pkg/spinlock
133133
else
134-
$(GO) test -race -run "[^FLAKY]$$" ./...
134+
$(GO) test -race -run "[^FLAKY]$$" ./pkg/spinlock
135135
endif
136136

137137
.PHONY: test-ci-flaky

pkg/spinlock/wait_test.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package spinlock_test
77
import (
88
"errors"
99
"testing"
10+
"testing/synctest"
1011
"time"
1112

1213
"github.com/ethersphere/bee/v2/pkg/spinlock"
@@ -17,27 +18,29 @@ func TestWait(t *testing.T) {
1718

1819
t.Run("timed out", func(t *testing.T) {
1920
t.Parallel()
20-
21-
err := spinlock.Wait(time.Millisecond*20, func() bool { return false })
22-
if !errors.Is(err, spinlock.ErrTimedOut) {
23-
t.Fatal("expecting to time out")
24-
}
21+
synctest.Test(t, func(t *testing.T) {
22+
err := spinlock.Wait(time.Millisecond*20, func() bool { return false })
23+
if !errors.Is(err, spinlock.ErrTimedOut) {
24+
t.Fatal("expecting to time out")
25+
}
26+
})
2527
})
2628

2729
t.Run("condition satisfied", func(t *testing.T) {
2830
t.Parallel()
29-
30-
spinStartTime := time.Now()
31-
condCallCount := 0
32-
err := spinlock.Wait(time.Millisecond*200, func() bool {
33-
condCallCount++
34-
return time.Since(spinStartTime) >= time.Millisecond*100
31+
synctest.Test(t, func(t *testing.T) {
32+
spinStartTime := time.Now()
33+
condCallCount := 0
34+
err := spinlock.Wait(time.Millisecond*200, func() bool {
35+
condCallCount++
36+
return time.Since(spinStartTime) >= time.Millisecond*100
37+
})
38+
if err != nil {
39+
t.Fatal("expecting to end wait without time out")
40+
}
41+
if condCallCount == 0 {
42+
t.Fatal("expecting condition function to be called")
43+
}
3544
})
36-
if err != nil {
37-
t.Fatal("expecting to end wait without time out")
38-
}
39-
if condCallCount == 0 {
40-
t.Fatal("expecting condition function to be called")
41-
}
4245
})
4346
}

0 commit comments

Comments
 (0)