-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor_test.go
More file actions
59 lines (47 loc) · 1.05 KB
/
Copy pathexecutor_test.go
File metadata and controls
59 lines (47 loc) · 1.05 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
package executor
import (
"context"
"fmt"
"log"
"math/rand"
"testing"
"time"
)
func TestCanRunExecutor(t *testing.T) {
//TODO simple test NEED TO CHANGE
ctx, cancel := context.WithTimeout(context.Background(), 20000*time.Second)
defer cancel()
executor := NewService(ctx, 10)
defer executor.Stop()
ts := 20
runnables := make([]Runnable, ts)
for i := 0; i < ts; i++ {
runnables[i] = NewTask(fmt.Sprintf("test_%v", i+1), fmt.Sprintf("testValue_%v", i+1), exec(ctx), complete, errH)
}
executor.Start()
executor.AddTasks(runnables...)
}
func exec(ctx context.Context) func(v string) error {
return func(v string) error {
for i := 0; i < 5; i++ {
select {
case <-ctx.Done():
return nil
default:
rand.Seed(time.Now().UnixNano())
time.Sleep(time.Duration(rand.Intn(2)) * time.Second)
fmt.Printf("executing job %s\n", v)
}
}
return nil
}
}
func complete(v string) error {
fmt.Printf("Complete successfull %s\n", v)
return nil
}
func errH(v string, err error) {
if err != nil {
log.Fatal(v + err.Error())
}
}