forked from lithdew/reliable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol_test.go
61 lines (48 loc) · 1.04 KB
/
protocol_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package reliable
import (
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
"sync"
"testing"
)
func testConnWaitForWriteDetails(inc uint16) func(t testing.TB) {
return func(t testing.TB) {
defer goleak.VerifyNone(t)
p := NewProtocol()
p.wi = uint16(len(p.rq))
var wg sync.WaitGroup
wg.Add(8)
ch := make(chan uint16, 8)
for i := 0; i < 8; i++ {
go func() {
defer wg.Done()
p.ouc.L.Lock()
idx, _, _, _ := p.waitForNextWriteDetails()
p.ouc.L.Unlock()
ch <- idx
}()
}
for i := 0; i < 8; i++ {
p.ouc.L.Lock()
p.oui += inc
p.ouc.Broadcast()
p.ouc.L.Unlock()
}
wg.Wait()
expected := make(map[uint16]struct{}, 8)
close(ch)
for idx := range ch {
expected[idx] = struct{}{}
}
for i := 0; i < 8; i++ {
actual := uint16(len(p.wq) + i)
require.Contains(t, expected, actual)
delete(expected, actual)
}
}
}
func TestConnWaitForWriteDetails(t *testing.T) {
testConnWaitForWriteDetails(1)(t)
testConnWaitForWriteDetails(2)(t)
testConnWaitForWriteDetails(4)(t)
}