Skip to content

Commit 72ed53f

Browse files
test(blocker): add synctest to blocker pkg (#5258)
1 parent 54b963a commit 72ed53f

File tree

1 file changed

+67
-66
lines changed

1 file changed

+67
-66
lines changed

pkg/blocker/blocker_test.go

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package blocker_test
77
import (
88
"os"
99
"testing"
10+
"testing/synctest"
1011
"time"
1112

1213
"go.uber.org/goleak"
@@ -36,98 +37,98 @@ func TestMain(m *testing.M) {
3637
}
3738

3839
func TestBlocksAfterFlagTimeout(t *testing.T) {
39-
t.Parallel()
40+
synctest.Test(t, func(t *testing.T) {
41+
addr := swarm.RandAddress(t)
42+
blockedC := make(chan swarm.Address, 10)
4043

41-
addr := swarm.RandAddress(t)
42-
blockedC := make(chan swarm.Address, 10)
44+
mock := mockBlockLister(func(a swarm.Address, d time.Duration, r string) error {
45+
blockedC <- a
4346

44-
mock := mockBlockLister(func(a swarm.Address, d time.Duration, r string) error {
45-
blockedC <- a
47+
if d != blockTime {
48+
t.Fatalf("block time: want %v, got %v", blockTime, d)
49+
}
4650

47-
if d != blockTime {
48-
t.Fatalf("block time: want %v, got %v", blockTime, d)
49-
}
50-
51-
return nil
52-
})
51+
return nil
52+
})
5353

54-
b := blocker.New(mock, flagTime, blockTime, time.Millisecond, nil, log.Noop)
55-
testutil.CleanupCloser(t, b)
54+
b := blocker.New(mock, flagTime, blockTime, time.Millisecond, nil, log.Noop)
55+
testutil.CleanupCloser(t, b)
5656

57-
// Flagging address shouldn't block it immediately
58-
b.Flag(addr)
59-
if len(blockedC) != 0 {
60-
t.Fatal("blocker did not wait flag duration")
61-
}
57+
// Flagging address shouldn't block it immediately
58+
b.Flag(addr)
59+
if len(blockedC) != 0 {
60+
t.Fatal("blocker did not wait flag duration")
61+
}
6262

63-
time.Sleep(flagTime / 2)
64-
b.Flag(addr) // check that this flag call does not override previous call
65-
if len(blockedC) != 0 {
66-
t.Fatal("blocker did not wait flag duration")
67-
}
63+
synctest.Wait()
64+
b.Flag(addr) // check that this flag call does not override previous call
65+
if len(blockedC) != 0 {
66+
t.Fatal("blocker did not wait flag duration")
67+
}
6868

69-
// Suspending current goroutine and expect that in this interval
70-
// block listener was called to block flagged address
71-
time.Sleep(flagTime * 3)
69+
// Suspending current goroutine and expect that in this interval
70+
// block listener was called to block flagged address
71+
synctest.Wait()
7272

73-
if a := <-blockedC; !a.Equal(addr) {
74-
t.Fatalf("expecting flagged address to be blocked")
75-
}
76-
if len(blockedC) != 0 {
77-
t.Fatalf("address should only be blocked once")
78-
}
73+
if a := <-blockedC; !a.Equal(addr) {
74+
t.Fatalf("expecting flagged address to be blocked")
75+
}
76+
if len(blockedC) != 0 {
77+
t.Fatalf("address should only be blocked once")
78+
}
79+
})
7980
}
8081

8182
func TestUnflagBeforeBlock(t *testing.T) {
82-
t.Parallel()
83+
synctest.Test(t, func(t *testing.T) {
84+
addr := swarm.RandAddress(t)
85+
mock := mockBlockLister(func(a swarm.Address, d time.Duration, r string) error {
86+
t.Fatalf("address should not be blocked")
8387

84-
addr := swarm.RandAddress(t)
85-
mock := mockBlockLister(func(a swarm.Address, d time.Duration, r string) error {
86-
t.Fatalf("address should not be blocked")
87-
88-
return nil
89-
})
88+
return nil
89+
})
9090

91-
b := blocker.New(mock, flagTime, blockTime, time.Millisecond, nil, log.Noop)
92-
testutil.CleanupCloser(t, b)
91+
b := blocker.New(mock, flagTime, blockTime, time.Millisecond, nil, log.Noop)
92+
testutil.CleanupCloser(t, b)
9393

94-
// Flagging address shouldn't block it imidietly
95-
b.Flag(addr)
94+
// Flagging address shouldn't block it immediately
95+
b.Flag(addr)
9696

97-
time.Sleep(flagTime / 2)
98-
b.Flag(addr) // check that this flag call does not override previous call
97+
synctest.Wait()
98+
b.Flag(addr) // check that this flag call does not override previous call
9999

100-
b.Unflag(addr)
100+
b.Unflag(addr)
101101

102-
// Suspending current goroutine and expect that in this interval
103-
// block listener was not called to block flagged address
104-
time.Sleep(flagTime * 3)
102+
// Suspending current goroutine and expect that in this interval
103+
// block listener was not called to block flagged address
104+
synctest.Wait()
105+
})
105106
}
106107

107108
func TestPruneBeforeBlock(t *testing.T) {
108-
t.Parallel()
109-
110-
addr := swarm.RandAddress(t)
111-
mock := mockBlockLister(func(a swarm.Address, d time.Duration, r string) error {
112-
t.Fatalf("address should not be blocked")
109+
synctest.Test(t, func(t *testing.T) {
110+
addr := swarm.RandAddress(t)
111+
mock := mockBlockLister(func(a swarm.Address, d time.Duration, r string) error {
112+
t.Fatalf("address should not be blocked")
113113

114-
return nil
115-
})
114+
return nil
115+
})
116116

117-
b := blocker.New(mock, flagTime, blockTime, time.Millisecond, nil, log.Noop)
118-
testutil.CleanupCloser(t, b)
117+
b := blocker.New(mock, flagTime, blockTime, time.Millisecond, nil, log.Noop)
118+
testutil.CleanupCloser(t, b)
119119

120-
// Flagging address shouldn't block it imidietly
121-
b.Flag(addr)
120+
// Flagging address shouldn't block it immediately
121+
b.Flag(addr)
122122

123-
time.Sleep(flagTime / 2)
123+
synctest.Wait()
124124

125-
// communicate that we have seen no peers, resulting in the peer being removed
126-
b.PruneUnseen([]swarm.Address{})
125+
// communicate that we have seen no peers, resulting in the peer being removed
126+
b.PruneUnseen([]swarm.Address{})
127127

128-
// Suspending current goroutine expect that in this interval
129-
// block listener was not called to block flagged address
130-
time.Sleep(flagTime * 3)
128+
// Suspending current goroutine expect that in this interval
129+
// block listener was not called to block flagged address
130+
synctest.Wait()
131+
})
131132
}
132133

133134
type blocklister struct {

0 commit comments

Comments
 (0)