-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepeat_test.go
94 lines (73 loc) · 1.98 KB
/
repeat_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
// Tideland Go Actor - Unit Tests
//
// Copyright (C) 2019-2023 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package actor_test
//--------------------
// IMPORTS
//--------------------
import (
"testing"
"time"
"tideland.dev/go/actor"
"tideland.dev/go/audit/asserts"
)
//--------------------
// TESTS
//--------------------
// TestRepeatStopActor verifies Repeat working and being
// stopped when the Actor is stopped.
func TestRepeatStopActor(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
finalized := make(chan struct{})
counter := 0
act, err := actor.Go(actor.WithFinalizer(func(err error) error {
defer close(finalized)
counter = 0
return err
}))
assert.OK(err)
assert.NotNil(act)
// Start the repeated action.
stop, err := act.Repeat(10*time.Millisecond, func() {
counter++
})
assert.OK(err)
assert.NotNil(stop)
time.Sleep(100 * time.Millisecond)
assert.True(counter >= 9, "possibly only 9 due to late interval start")
// Stop the Actor and check the finalization.
act.Stop()
<-finalized
assert.NoError(act.Err())
assert.Equal(counter, 0)
// Check if the Interval is stopped too.
time.Sleep(100 * time.Millisecond)
assert.Equal(counter, 0)
}
// TestPeriodicalStopInterval verifies Periodical working and being
// stopped when the periodical is stopped.
func TestIntervalStopInterval(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
counter := 0
act, err := actor.Go()
assert.OK(err)
assert.NotNil(act)
// Start the repeated action.
stop, err := act.Repeat(10*time.Millisecond, func() {
counter++
})
assert.OK(err)
assert.NotNil(stop)
time.Sleep(100 * time.Millisecond)
assert.True(counter >= 9, "possibly only 9 due to late interval start")
// Stop the periodical and check that it doesn't work anymore.
counterNow := counter
stop()
time.Sleep(100 * time.Millisecond)
assert.Equal(counter, counterNow)
act.Stop()
}
// EOF