Repository navigation
Expand file tree
/
Copy pathrunnable_test.go
More file actions
94 lines (86 loc) · 1.82 KB
/
Copy pathrunnable_test.go
File metadata and controls
94 lines (86 loc) · 1.82 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package framework
import (
"testing"
"time"
)
func Test_FunctionRunner(t *testing.T) {
var r = func(c RoutineController) {
t.Log("routine started")
for !c.IsStopping() {
select {
case <- c.GetNotifyChannel():
c.SetStopping()
}
}
t.Log("routine stoped")
c.NotifyExit()
}
var runner = CreateSimpleRunner(r)
var err = runner.Start()
if err != nil{
t.Fatalf("start runner fail: %s", err.Error())
}
var exitChan = make(chan bool, 1)
go func() {
runner.Stop()
exitChan <- true
}()
var timer = time.NewTimer(3*time.Second)
select{
case <- timer.C:
t.Fatal("stop runner timeout")
case <- exitChan:
t.Log("runner stopped")
}
}
type stub struct {
t *testing.T
}
func (s *stub)SomeMember(c RoutineController){
s.t.Log("routine started")
for !c.IsStopping() {
select {
case <- c.GetNotifyChannel():
c.SetStopping()
}
}
s.t.Log("routine stoped")
c.NotifyExit()
}
func Test_MemberRunner(t *testing.T) {
var s = stub{t}
var runner = CreateSimpleRunner(s.SomeMember)
var err = runner.Start()
if err != nil{
t.Fatalf("start runner fail: %s", err.Error())
}
if err = runner.Start(); nil == err{
t.Fatal("duplicate start success")
}
t.Logf("duplicate start fail: %s", err.Error())
if !runner.IsRunning(){
t.Fatal("runner not in running status")
}
t.Log("runner in running status")
var exitChan = make(chan bool, 1)
go func() {
runner.Stop()
exitChan <- true
}()
var timer = time.NewTimer(3*time.Second)
select{
case <- timer.C:
t.Fatal("stop runner timeout")
case <- exitChan:
t.Log("runner stopped")
if runner.IsRunning(){
t.Fatal("runner in running status")
}
t.Log("runner not in running status")
if err = runner.Stop(); nil == err{
t.Fatal("duplicate stop success")
}
t.Logf("duplicate stop fail: %s", err.Error())
}
t.Log("member runner test: ok")
}