-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner_test.go
More file actions
225 lines (187 loc) · 5.33 KB
/
runner_test.go
File metadata and controls
225 lines (187 loc) · 5.33 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
225
/*
Copyright 2016 - Jaume Arús
Author Jaume Arús - jaumearus@gmail.com
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package runner
import (
"fmt"
"testing"
"time"
)
var err error
var myrunner Runner_I
var mytask Task_I
var mytaskimplementation TASK_IMPLEMENTATION
func getRunner(t *testing.T) Runner_I {
// Take the runner
if myrunner, err = GetRunner(100); err != nil {
t.Error(err)
} else {
// Starting the runner
if err = myrunner.Start(); err != nil {
t.Error(err)
} else {
t.Logf("Runner started \n")
}
}
return myrunner
}
func TestRunner_Start(t *testing.T) {
getRunner(t)
}
// Test for a finite task which is executed during all of its duration
func TestRunner_WakeUpTaskDuration(t *testing.T) {
myrunner := getRunner(t)
// Define the task implementation
mytaskimplementation = func(eot chan struct{}, taskManager TaskManager_I, args []interface{}) ([]interface{}, error) {
var c int = 0
var sleep_time int64 = args[0].(int64)
var t time.Time
for {
select {
case <-eot:
fmt.Println("The task finalizes by EOT signal ...")
return []interface{}{c, t}, nil
default:
time.Sleep(time.Duration(sleep_time) * time.Millisecond)
c += 1
t = time.Now()
}
}
fmt.Println("The task has finished by its self ....")
return []interface{}{c, t}, nil
}
// Get the task
if mytask, err = GetTask(1, time.Duration(3)*time.Second, mytaskimplementation); err != nil {
t.Error(err)
} else {
t.Logf("The task has been woken up\n")
// Wakeup the task
if err = myrunner.WakeUpTask(mytask, int64(100)); err != nil {
t.Error(err)
} else {
// Take the response
response := <-mytask.GetResponseChan()
// Check for the response
if response.Err != nil {
t.Error(response.Err)
} else {
t.Logf("The result is : %#v\n", response.Result)
}
}
}
}
// Test for a finite task which finishes before its maximum duration
func TestRunner_WakeUpTaskFinite(t *testing.T) {
myrunner := getRunner(t)
// Define the task implementation
mytaskimplementation = func(eot chan struct{}, taskManager TaskManager_I, args []interface{}) ([]interface{}, error) {
var c int = 0
var sleep_time int64 = args[0].(int64)
var t time.Time
var goon bool = true
for goon {
select {
case <-eot:
fmt.Println("The task finalizes by EOT signal ...")
return []interface{}{c, t}, nil
default:
time.Sleep(time.Duration(sleep_time) * time.Millisecond)
c += 1
t = time.Now()
if c == 3 {
// Finishing the task
goon = false
}
}
}
fmt.Println("The task has finished by its self ....")
// Says the runner this task must be finished
taskManager.Finish()
return []interface{}{c, t}, nil
}
// Get the task
if mytask, err = GetTask(2, time.Duration(10)*time.Second, mytaskimplementation); err != nil {
t.Error(err)
} else {
t.Logf("The task has been woken up\n")
// Wakeup the task
if err = myrunner.WakeUpTask(mytask, int64(100)); err != nil {
t.Error(err)
} else {
// Take the response
response := <-mytask.GetResponseChan()
// Check for the response
if response.Err != nil {
t.Error(response.Err)
} else {
t.Logf("The result is : %#v\n", response.Result)
}
}
}
}
// Test for a finite task that breaks down and is resumed several times during its executin time
func TestRunner_WakeUpTaskResuming(t *testing.T) {
myrunner := getRunner(t)
// Define the task implementation
mytaskimplementation = func(eot chan struct{}, taskManager TaskManager_I, args []interface{}) ([]interface{}, error) {
var c int = 0
var sleep_time int64 = args[0].(int64)
var t time.Time
var goon bool = true
fmt.Printf("Resuming task started ...\n")
for goon {
select {
case <-eot:
fmt.Println("The task finalizes by EOT signal ...")
return []interface{}{c, t}, nil
default:
time.Sleep(time.Duration(sleep_time) * time.Millisecond)
c += 1
t = time.Now()
if c == 3 {
//forces the flush of the task
taskManager.Flush()
panic("ooppp 0!!!")
}
}
}
fmt.Println("The task has finished by its self ....")
return []interface{}{c, t}, nil
}
// Get the task
if mytask, err = GetTask(3, time.Duration(3)*time.Second, mytaskimplementation); err != nil {
t.Error(err)
} else {
t.Logf("The task has been woken up\n")
// Wakeup the task
if err = myrunner.WakeUpTask(mytask, int64(100)); err != nil {
t.Error(err)
} else {
_now := time.Now()
results := make([]*TaskResponse, 0)
for {
results = append(results, <-mytask.GetResponseChan())
if time.Now().Sub(_now).Seconds() > 3 {
break
}
}
// Check for the response
t.Logf("The result is : %#v\n", results)
}
}
}