-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer_test.go
64 lines (50 loc) · 1.32 KB
/
timer_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
package incr
import (
"context"
"testing"
"time"
"github.com/wcharczuk/go-incr/testutil"
)
func Test_Timer(t *testing.T) {
ctx := testContext()
clock := time.Now()
g := New()
timer := Timer(g, Return(g, 0), 500*time.Millisecond)
timer.Node().SetLabel("timer-a")
timer.(*timerIncr[int]).clockSource = func(_ context.Context) time.Time {
return clock
}
testutil.Matches(t, `timer\[(.*)\]:timer-a@-1`, timer.(*timerIncr[int]).String())
// nop
timer.(*timerIncr[int]).Always()
var counterTimed int
timed := Map(g, timer, func(base int) int {
counterTimed++
return base + counterTimed + 1
})
timed.Node().SetLabel("timed")
var counterUntimed int
untimed := Map(g, Return(g, 0), func(base int) int {
counterUntimed++
return base + counterUntimed + 1
})
untimed.Node().SetLabel("untimed")
final := Map2(g, timed, untimed, func(a, b int) int {
return a + b
})
final.Node().SetLabel("final")
o := MustObserve(g, final)
err := g.Stabilize(ctx)
testutil.Nil(t, err)
testutil.Equal(t, 0, timer.Value())
testutil.Equal(t, 2, timed.Value())
testutil.Equal(t, 2, untimed.Value())
testutil.Equal(t, 4, o.Value())
err = g.Stabilize(ctx)
testutil.Nil(t, err)
testutil.Equal(t, 4, o.Value())
clock = clock.Add(time.Second)
err = g.Stabilize(ctx)
testutil.Nil(t, err)
testutil.Equal(t, 5, o.Value())
}