This repository has been archived by the owner on Feb 23, 2019. It is now read-only.
/
test.go
96 lines (73 loc) · 3.4 KB
/
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
package module_test
import (
"testing"
"time"
. "github.com/onsi/gomega"
. "github.com/sportfun/gakisitor/env"
"github.com/sportfun/gakisitor/log"
"github.com/sportfun/gakisitor/module"
)
var NProcesses = 5
// custom module test tool
func Test(t *testing.T, m module.Module, e *environment) {
e.module = m
e.queue = module.NewNotificationQueue()
e.logger = log.NewDevelopment()
t.Run("Start", func(t *testing.T) { eval(start, t, e) })
if e.test.pre != nil {
t.Run("PreTests", func(t *testing.T) {
RegisterTestingT(t)
e.test.pre(t, m)
})
}
t.Run("Configure", func(t *testing.T) { eval(configure, t, e) })
t.Run("StartSession", func(t *testing.T) { eval(startSessions, t, e) })
t.Run("Process", func(t *testing.T) { eval(process, t, e) })
t.Run("StopSession", func(t *testing.T) { eval(stopSession, t, e) })
if e.test.post != nil {
t.Run("PostTest", func(t *testing.T) {
RegisterTestingT(t)
e.test.post(t, NProcesses, e.module, e.queue)
})
}
t.Run("Stop", func(t *testing.T) { eval(stop, t, e) })
}
func eval(fnc func(*testing.T, *environment), t *testing.T, environment *environment) {
RegisterTestingT(t)
fnc(t, environment)
}
func start(t *testing.T, e *environment) {
Expect(e.module).Should(HaveState(UndefinedState))
Expect(e.module.Start(nil, e.logger)).Should(HaveOccurred()) // failed: queue is nil
Expect(e.module.Start(e.queue, nil)).Should(ExpectFor(e.module).Panic()) // failed: logger is nil
Expect(e.module.Start(e.queue, e.logger)).Should(ExpectFor(e.module).SucceedWith(StartedState)) // succeed: start the module
}
func configure(t *testing.T, e *environment) {
Expect(e.module.Configure(e.definition(t))).Should(ExpectFor(e.module).SucceedWith(IdleState)) // succeed: configure the module
}
func startSessions(t *testing.T, e *environment) {
Expect(e.module).Should(HaveState(IdleState))
Expect(e.module.Process()).Should(ExpectFor(e.module).SucceedWith(IdleState)) // failed: no session started, but no need to return error
Expect(e.module.StartSession()).Should(ExpectFor(e.module).SucceedWith(WorkingState)) // succeed: start session successfully
Expect(e.module.StartSession()).Should(ExpectFor(e.module).FailedWith(IdleState)) // failed: session already started
Expect(e.module.StartSession()).Should(ExpectFor(e.module).SucceedWith(WorkingState)) // succeed: start session successfully
}
func process(t *testing.T, e *environment) {
Expect(e.module).Should(HaveState(WorkingState))
for i := 0; i < NProcesses; i++ {
time.Sleep(e.tick)
Expect(e.module.Process()).Should(Succeed())
}
}
func stopSession(t *testing.T, e *environment) {
Expect(e.module).Should(HaveState(WorkingState))
Expect(e.module.StopSession()).Should(ExpectFor(e.module).SucceedWith(IdleState)) // succeed: stop session successfully
Expect(e.module.Process()).Should(ExpectFor(e.module).SucceedWith(IdleState)) // failed: no session started, but no need to return error
Expect(e.module.StopSession()).Should(ExpectFor(e.module).FailedWith(IdleState)) // failed: session already stopped
}
func stop(t *testing.T, e *environment) {
Expect(e.module).Should(HaveState(IdleState))
Expect(e.module.StartSession()).Should(ExpectFor(e.module).SucceedWith(WorkingState)) // succeed: start session successfully
Expect(e.module.Stop()).Should(ExpectFor(e.module).SucceedWith(StoppedState)) // succeed: stop session & module
time.Sleep(2 * e.tick) // wait all goroutine
}