-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathccontainer_test.go
112 lines (100 loc) · 2.13 KB
/
ccontainer_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package ccontainer
import (
"context"
"testing"
"time"
)
// TestCContainer tests the concurrent container
func TestCContainer(t *testing.T) {
ctx := context.Background()
c := NewCContainer[*int](nil)
errCh := make(chan error, 1)
_ = c.WaitValueEmpty(ctx, errCh) // should be instant
val := 5
go c.SetValue(&val)
gv, err := c.WaitValue(ctx, errCh)
if err != nil {
t.Fatal(err.Error())
}
if gv == nil || *gv != 5 {
t.Fail()
}
dl, dlCancel := context.WithDeadline(ctx, time.Now().Add(time.Millisecond*1))
defer dlCancel()
err = c.WaitValueEmpty(dl, errCh)
if err != context.DeadlineExceeded {
t.Fail()
}
c.SetValue(nil)
_ = c.WaitValueEmpty(ctx, errCh) // should be instant
swapPlusOne := func(val *int) *int {
nv := 1
if val != nil {
nv = *val + 1
}
return &nv
}
for i := 1; i < 10; i++ {
out := c.SwapValue(swapPlusOne)
if out == nil || *out != i {
t.Fail()
}
}
}
// TestCContainerWithEqual tests the concurrent container with an equal checker
func TestCContainerWithEqual(t *testing.T) {
type data struct {
value string
}
ctx := context.Background()
c := NewCContainerWithEqual[*data](nil, func(a, b *data) bool {
if (a == nil) != (b == nil) {
return false
}
if b.value == "same" {
return true
}
return a.value == b.value
})
mkInitial := func() *data {
return &data{value: "hello"}
}
c.SetValue(mkInitial())
var done chan struct{}
start := func() {
done = make(chan struct{})
go func() {
_, _ = c.WaitValueChange(ctx, mkInitial(), nil)
close(done)
}()
}
start()
assertDone := func() {
select {
case <-done:
case <-time.After(time.Millisecond * 100):
t.Fatal("expected WaitValueChange to have returned")
}
}
assertNotDone := func() {
select {
case <-done:
t.Fatal("expected WaitValueChange to not return yet")
case <-time.After(time.Millisecond * 50):
}
}
assertNotDone()
c.SetValue(mkInitial())
assertNotDone()
c.SetValue(&data{value: "same"})
assertNotDone()
c.SetValue(&data{value: "different"})
assertDone()
start()
assertDone()
c.SetValue(mkInitial())
start()
assertNotDone()
c.SetValue(&data{value: "different"})
assertDone()
}