forked from digitalocean/go-workers2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_utils.go
103 lines (90 loc) · 2.23 KB
/
test_utils.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
97
98
99
100
101
102
103
package workers
import (
"context"
"time"
)
const (
testServerAddr = "localhost:6379"
testDatabase = 15
)
// SetupDefaultTestOptions will setup processed default test options without a namespace
func SetupDefaultTestOptions() (Options, error) {
return SetupDefaultTestOptionsWithNamespace("")
}
// SetupDefaultTestOptionsWithNamespace sets up processed default test options with namespace and flushes redis
// if client is configured
func SetupDefaultTestOptionsWithNamespace(namespace string) (Options, error) {
opts, err := processOptions(testOptionsWithNamespace(namespace))
if opts.client != nil {
_, err = opts.client.FlushDB(context.Background()).Result()
if err != nil {
return Options{}, err
}
}
return opts, err
}
func testOptionsWithNamespace(namespace string) Options {
return Options{
ServerAddr: testServerAddr,
ProcessID: "1",
Database: testDatabase,
PoolSize: 1,
Namespace: namespace,
}
}
// SetupDefaultTestOptionsWithHeartbeat creates default options for testing heartbeat related features
func SetupDefaultTestOptionsWithHeartbeat(namespace, processID string) Options {
return Options{
ServerAddr: testServerAddr,
ProcessID: processID,
Database: testDatabase,
PoolSize: 1,
Namespace: namespace,
Heartbeat: &HeartbeatOptions{
Interval: 2 * time.Second,
HeartbeatTTL: 6 * time.Second,
},
}
}
// CallCounter counts and synchronizes calls
type CallCounter struct {
count int
syncCh chan *Msg
ackSyncCh chan bool
}
// NewCallCounter returns a new CallCounter
func NewCallCounter() *CallCounter {
return &CallCounter{
syncCh: make(chan *Msg),
ackSyncCh: make(chan bool),
}
}
func (j *CallCounter) getOpt(m *Msg, opt string) bool {
if m == nil {
return false
}
return m.Args().GetIndex(0).Get(opt).MustBool()
}
func (j *CallCounter) F(m *Msg) error {
j.count++
if m != nil {
if j.getOpt(m, "sync") {
j.syncCh <- m
<-j.ackSyncCh
}
m.ack = !j.getOpt(m, "noack")
}
return nil
}
func (j *CallCounter) syncMsg() *Msg {
m, _ := NewMsg(`{"args": [{"sync": true}]}`)
return m
}
func (j *CallCounter) msg() *Msg {
m, _ := NewMsg(`{"args": []}`)
return m
}
func (j *CallCounter) noAckMsg() *Msg {
m, _ := NewMsg(`{"args": [{"noack": true}]}`)
return m
}