forked from gree/futurama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_test.go
76 lines (64 loc) · 1.63 KB
/
init_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
package futurama
import (
"encoding/json"
"flag"
"github.com/golang/glog"
"time"
)
func init() {
flag.Set("v", "100")
}
const (
Test_TriggerType_Default = "test-default"
Test_TriggerType_Retry = "test-retry"
Test_TriggerType_Panic = "test-panic"
)
type TestTrigger_Schedule struct {
C chan string
}
func (self *TestTrigger_Schedule) Trigger(ev *Event) *TriggerResult {
glog.Infoln("TestTrigger_Schedule", ev.Id)
defer func() {
self.C <- ev.Id
}()
return &TriggerResult{Status: EventStatus_OK}
}
type TestTrigger_Retry struct {
C chan string
}
func (self *TestTrigger_Retry) Trigger(ev *Event) *TriggerResult {
glog.Infoln("TestTrigger_Retry", ev.Id)
defer func() {
self.C <- ev.Id
}()
data := ev.Data.(map[string]interface{})
numRetry, _ := data["NumRetry"].(json.Number).Int64()
retryTimeNano, _ := data["RetryTimeNano"].(json.Number).Int64()
var retryTime time.Time
if retryTimeNano == 0 {
retryTime = time.Time{}
} else {
retryTime = time.Unix(0, retryTimeNano)
}
if int(numRetry) == ev.Attempts {
return &TriggerResult{Status: EventStatus_OK}
} else {
return &TriggerResult{EventStatus_RETRY, retryTime, nil}
}
}
type TestTrigger_Panic struct{}
func (self *TestTrigger_Panic) Trigger(ev *Event) *TriggerResult {
glog.Infoln("TestTrigger_Panic", ev.Id)
panic("Expected panic")
return nil
}
func SetupQueue(cfg *Config) (*Queue, chan string) {
c := make(chan string, 64)
q, _ := CreateQueue(cfg, map[string]TriggerInterface{
Test_TriggerType_Default: &TestTrigger_Schedule{c},
Test_TriggerType_Retry: &TestTrigger_Retry{c},
Test_TriggerType_Panic: &TestTrigger_Panic{},
})
q.Start()
return q, c
}