-
Notifications
You must be signed in to change notification settings - Fork 1
/
var_test.go
94 lines (76 loc) · 2.17 KB
/
var_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
package incr
import (
"context"
"testing"
"github.com/wcharczuk/go-incr/testutil"
)
func Test_Var_Set_unobserved(t *testing.T) {
g := New()
v := Var(g, "foo")
testutil.Equal(t, "foo", v.Value())
v.Set("not-foo")
testutil.Equal(t, "not-foo", v.Value())
}
func Test_Var_Stabilize_zero(t *testing.T) {
g := New()
v := Var(g, "foo")
_ = MustObserve(g, v)
_ = g.Stabilize(context.TODO())
testutil.Equal(t, "foo", v.Value())
}
func Test_Var_Set_duringStabilization(t *testing.T) {
g := New()
v := Var(g, "foo")
_ = MustObserve(g, v)
g.status = StatusStabilizing
v.Set("not-foo")
testutil.Equal(t, true, v.(*varIncr[string]).setDuringStabilization)
testutil.Equal(t, "not-foo", v.(*varIncr[string]).setDuringStabilizationValue)
testutil.Equal(t, "foo", v.(*varIncr[string]).value)
_ = v.(*varIncr[string]).Stabilize(context.TODO())
testutil.Equal(t, false, v.(*varIncr[string]).setDuringStabilization)
testutil.Equal(t, "", v.(*varIncr[string]).setDuringStabilizationValue)
testutil.Equal(t, "not-foo", v.(*varIncr[string]).value)
}
func Test_Var_Set_duringStabilization_realistic(t *testing.T) {
ctx := testContext()
g := New()
v := Var(g, "foo")
proceed := make(chan struct{})
invoked := make(chan struct{})
m0 := Map(g, v, func(vv string) string {
close(invoked)
<-proceed
return vv + "-done!"
})
o := MustObserve(g, m0)
stabilizationDone := make(chan struct{})
go func() {
_ = g.Stabilize(ctx)
close(stabilizationDone)
}()
<-invoked
v.Set("during-stab")
testutil.Equal(t, true, v.(*varIncr[string]).setDuringStabilization)
testutil.Equal(t, "during-stab", v.(*varIncr[string]).setDuringStabilizationValue)
close(proceed)
<-stabilizationDone
testutil.Equal(t, "foo-done!", o.Value())
testutil.Equal(t, "during-stab", v.Value())
proceed = make(chan struct{})
invoked = make(chan struct{})
stabilizationDone = make(chan struct{})
go func() {
_ = g.Stabilize(ctx)
close(stabilizationDone)
}()
<-invoked
close(proceed)
<-stabilizationDone
testutil.Equal(t, "during-stab-done!", o.Value())
}
func Test_Var_ShouldBeInvalidated(t *testing.T) {
g := New()
v := Var(g, "foo")
testutil.Equal(t, false, v.(*varIncr[string]).ShouldBeInvalidated())
}