forked from status-im/status-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditionalrepeater_test.go
More file actions
79 lines (69 loc) · 1.61 KB
/
conditionalrepeater_test.go
File metadata and controls
79 lines (69 loc) · 1.61 KB
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
package transactions
import (
"context"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestConditionalRepeater_RunOnce(t *testing.T) {
var wg sync.WaitGroup
runCount := 0
wg.Add(1)
taskRunner := NewConditionalRepeater(1*time.Nanosecond, func(ctx context.Context) bool {
runCount++
defer wg.Done()
return WorkDone
})
taskRunner.RunUntilDone()
// Wait for task to run
wg.Wait()
taskRunner.Stop()
require.Greater(t, runCount, 0)
}
func TestConditionalRepeater_RunUntilDone_MultipleCalls(t *testing.T) {
var wg sync.WaitGroup
wg.Add(5)
runCount := 0
taskRunner := NewConditionalRepeater(1*time.Nanosecond, func(ctx context.Context) bool {
runCount++
wg.Done()
return runCount == 5
})
for i := 0; i < 10; i++ {
taskRunner.RunUntilDone()
}
// Wait for all tasks to run
wg.Wait()
taskRunner.Stop()
require.Greater(t, runCount, 4)
}
func TestConditionalRepeater_Stop(t *testing.T) {
var taskRunningWG, taskCanceledWG, taskFinishedWG sync.WaitGroup
taskRunningWG.Add(1)
taskCanceledWG.Add(1)
taskFinishedWG.Add(1)
taskRunner := NewConditionalRepeater(1*time.Nanosecond, func(ctx context.Context) bool {
defer taskFinishedWG.Done()
select {
case <-ctx.Done():
require.Fail(t, "task should not be canceled yet")
default:
}
// Wait to caller to stop the task
taskRunningWG.Done()
taskCanceledWG.Wait()
select {
case <-ctx.Done():
require.Error(t, ctx.Err())
default:
require.Fail(t, "task should be canceled")
}
return WorkDone
})
taskRunner.RunUntilDone()
taskRunningWG.Wait()
taskRunner.Stop()
taskCanceledWG.Done()
taskFinishedWG.Wait()
}