Repository navigation
Expand file tree
/
Copy pathtransaction_engine_test.go
More file actions
224 lines (197 loc) · 5.24 KB
/
Copy pathtransaction_engine_test.go
File metadata and controls
224 lines (197 loc) · 5.24 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package framework
import (
"time"
"testing"
"fmt"
)
const (
KeyTaskID = 1
)
var resultChan = make(chan uint, 1 << 12)
type WaitExecutor struct {
t *testing.T
}
func (executor *WaitExecutor)Execute(id SessionID, message Message, incoming chan Message, terminate chan bool) error{
taskID, _ := message.GetUInt(KeyTaskID)
//executor.t.Logf("[%08X] task %d begin wait", id, taskID)
time.Sleep(1000 * time.Millisecond)
//executor.t.Logf("[%08X] task %d wait finish", id, taskID)
resultChan <- taskID
return nil
}
func TestTransactionEngine_WaitExecutor(t *testing.T) {
engine, err := CreateTransactionEngine()
if err != nil{
t.Fatalf("create engine fail: %s", err.Error())
}
const (
testMessage = 1
id1 = 1
id2 = 2
)
engine.RegisterExecutor(testMessage, &WaitExecutor{t})
if err = engine.Start(); err != nil{
t.Fatalf("start engine fail: %s", err.Error())
}
t.Log("engine started")
task1, _ := CreateJsonMessage(testMessage)
task1.SetUInt(KeyTaskID, id1)
task2, _ := CreateJsonMessage(testMessage)
task2.SetUInt(KeyTaskID, id2)
engine.InvokeTask(task1)
time.Sleep(50*time.Millisecond)
engine.InvokeTask(task2)
var expected = []uint{id1, id2}
var executed = make([]uint, 0)
var timer = time.NewTimer(5*time.Second)
for len(executed) < 2{
select {
case <- timer.C:
t.Fatal("execute timeout")
case id := <-resultChan:
executed = append(executed, id)
}
}
if len(executed) != len(expected){
t.Fatal("unexpect queue length")
}
for i := 0; i < len(expected); i++{
if expected[i] != executed[i]{
t.Fatalf("unexpect task %d at index %d", executed[i], i)
}
}
if err = engine.Stop(); err != nil{
t.Fatalf("stop engine fail: %s", err.Error())
}
t.Log("engine stopped")
}
type TimeoutExecutor struct {
Timeout time.Duration
}
func (executor *TimeoutExecutor)Execute(id SessionID, message Message, incoming chan Message, terminate chan bool) error{
taskID, _ := message.GetUInt(KeyTaskID)
var timer = time.NewTimer(executor.Timeout)
select {
case <- timer.C:
resultChan <- taskID
}
return nil
}
func TestTransactionEngine_SessionTimeout(t *testing.T) {
engine, err := CreateTransactionEngine()
if err != nil{
t.Fatalf("create engine fail: %s", err.Error())
}
const (
testMessage = 2
timeout = 1000*time.Millisecond
taskCount = 5
)
engine.RegisterExecutor(testMessage, &TimeoutExecutor{Timeout:timeout})
if err = engine.Start(); err != nil{
t.Fatalf("start engine fail: %s", err.Error())
}
var expected []uint
t.Log("engine started")
for taskID := 0; taskID < taskCount; taskID++{
msg, _ := CreateJsonMessage(testMessage)
msg.SetUInt(KeyTaskID, uint(taskID))
expected = append(expected, uint(taskID))
engine.InvokeTask(msg)
time.Sleep(50*time.Millisecond)
}
var executed = make([]uint, 0)
var timer = time.NewTimer(6*time.Second)
for len(executed) < taskCount{
select {
case <- timer.C:
t.Fatal("execute timeout")
case id := <-resultChan:
executed = append(executed, id)
}
}
if len(executed) != len(expected){
t.Fatal("unexpect queue length")
}
for i := 0; i < len(expected); i++{
if expected[i] != executed[i]{
t.Fatalf("unexpect task %d at index %d", executed[i], i)
}
}
if err = engine.Stop(); err != nil{
t.Fatalf("stop engine fail: %s", err.Error())
}
t.Log("engine stopped")
}
type NopeExecutor struct {
}
func (executor *NopeExecutor)Execute(id SessionID, message Message, incoming chan Message, terminate chan bool) error{
taskID, _ := message.GetUInt(KeyTaskID)
resultChan <- taskID
return nil
}
func TestTransactionEngine_NopeExecutor(t *testing.T) {
engine, err := CreateTransactionEngine()
if err != nil{
t.Fatalf("create engine fail: %s", err.Error())
}
const (
testMessage = 3
taskCount = 1 << 11
)
engine.RegisterExecutor(testMessage, &NopeExecutor{})
if err = engine.Start(); err != nil{
t.Fatalf("start engine fail: %s", err.Error())
}
var expected []uint
t.Log("engine started")
for taskID := 0; taskID < taskCount; taskID++{
msg, _ := CreateJsonMessage(testMessage)
msg.SetUInt(KeyTaskID, uint(taskID))
expected = append(expected, uint(taskID))
engine.InvokeTask(msg)
time.Sleep(5*time.Millisecond)
}
var executed = make([]uint, 0)
var timer = time.NewTimer(6*time.Second)
for len(executed) < taskCount{
select {
case <- timer.C:
t.Fatal("execute timeout")
case id := <-resultChan:
executed = append(executed, id)
}
}
if len(executed) != len(expected){
t.Fatal("unexpect queue length")
}
for i := 0; i < len(expected); i++{
if expected[i] != executed[i]{
t.Fatalf("unexpect task %d at index %d", executed[i], i)
}
}
if err = engine.Stop(); err != nil{
t.Fatalf("stop engine fail: %s", err.Error())
}
t.Logf("engine stopped, %d task executed", len(executed))
}
type OneStepExecutor struct {
}
const (
FirstMessage = iota
SecondMessage
)
func (executor *OneStepExecutor)Execute(id SessionID, message Message, incoming chan Message, terminate chan bool) error{
taskID, _ := message.GetUInt(KeyTaskID)
var timer = time.NewTimer(5*time.Second)
select {
case received := <- incoming:
if received.GetID() != FirstMessage{
return fmt.Errorf("unexpected message %d", received.GetID())
}
resultChan <- taskID
case <- timer.C:
return fmt.Errorf("wait message for task %d timeout", taskID)
}
return nil
}