-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager_test.go
110 lines (78 loc) · 1.96 KB
/
manager_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
package gorman
import (
"context"
"testing"
"time"
)
func TestManager_Add(t *testing.T) {
manager := NewManager(Options{})
g := func(ctx context.Context) error {
return nil
}
manager.Add("test", g, NeverRestart())
if len(manager.goroutines) != 1 {
t.Errorf("expected one goroutine to be added, got %d", len(manager.goroutines))
}
if manager.goroutines["test"].Func == nil {
t.Errorf("expected goroutine function to be added")
}
}
func TestManager_Start(t *testing.T) {
manager := NewManager(Options{})
g := func(ctx context.Context) error {
<-ctx.Done()
return nil
}
manager.Add("test", g, NeverRestart())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := manager.Start(ctx, "test")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
time.Sleep(time.Millisecond * 100)
if !manager.goroutines["test"].Running() {
t.Errorf("expected goroutine to be running")
}
err = manager.Start(ctx, "invalid")
if err == nil {
t.Errorf("expected an error")
}
}
func TestManager_Stop(t *testing.T) {
manager := NewManager(Options{})
g := func(ctx context.Context) error {
<-ctx.Done()
return nil
}
manager.Add("test", g, NeverRestart())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := manager.Start(ctx, "test")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
time.Sleep(time.Millisecond * 100)
err = manager.Stop("test")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if manager.goroutines["test"].Running() {
t.Errorf("expected goroutine to be stopped")
}
err = manager.Stop("invalid")
if err == nil {
t.Errorf("expected an error")
}
}
func TestManager_Run(t *testing.T) {
manager := NewManager(Options{})
g := func(ctx context.Context) error {
<-ctx.Done()
return nil
}
manager.Add("test", g, NeverRestart())
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
defer cancel()
manager.Run(ctx)
}