-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoroutine_test.go
106 lines (91 loc) · 2.06 KB
/
goroutine_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
package gorman
import (
"context"
"github.com/morebec/go-errors/errors"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestGoroutine_Start(t *testing.T) {
g := Goroutine{
Name: "start_test",
Func: func(ctx context.Context) error {
return nil
},
}
g.Start(context.Background())
assert.True(t, g.Running())
time.Sleep(time.Millisecond * 10)
assert.Len(t, g.executions, 1)
}
func TestGoroutine_Stop(t *testing.T) {
g := Goroutine{
Name: "stop_test",
Func: func(ctx context.Context) error {
t := time.NewTimer(time.Second * 2)
select {
case <-ctx.Done():
return nil
case <-t.C:
return errors.NewWithMessage("failure", "failed")
}
},
}
g.Start(context.Background())
assert.True(t, g.Running())
err := g.Stop()
assert.NoError(t, err)
assert.False(t, g.Running())
exec, _ := g.LastExecution()
assert.NoError(t, exec.Error)
}
func TestGoroutine_StopByCancellingContext(t *testing.T) {
g := Goroutine{
Name: "stop_test",
Func: func(ctx context.Context) error {
t := time.NewTimer(time.Second * 2)
select {
case <-ctx.Done():
return nil
case <-t.C:
return errors.NewWithMessage("failure", "failed")
}
},
}
ctx, cancel := context.WithCancel(context.Background())
g.Start(ctx)
assert.True(t, g.Running())
listen := g.Listen()
cancel()
<-listen
<-listen
assert.False(t, g.Running())
exec, _ := g.LastExecution()
assert.NoError(t, exec.Error)
}
func TestGoroutine_Wait(t *testing.T) {
g := Goroutine{
Name: "wait_test",
Func: func(ctx context.Context) error {
time.Sleep(time.Second * 1)
return errors.NewWithMessage("failure", "failed")
},
}
err := g.Wait(context.Background())
assert.False(t, g.Running())
assert.Error(t, err)
}
func TestGoroutine_Running(t *testing.T) {
g := Goroutine{
Name: "listen_test",
Func: func(ctx context.Context) error {
time.Sleep(time.Second * 1)
return errors.NewWithMessage("failure", "failed")
},
}
g.Start(context.Background())
assert.True(t, g.Running())
err := g.Stop()
assert.Error(t, err)
assert.False(t, g.Running())
}