forked from gocraft/work
-
Notifications
You must be signed in to change notification settings - Fork 12
/
observer_test.go
124 lines (97 loc) · 3.06 KB
/
observer_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
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
package work
import (
"fmt"
"testing"
"github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/assert"
)
func TestObserverStarted(t *testing.T) {
pool := newTestPool(t)
ns := "work"
tMock := int64(1425263401)
setNowEpochSecondsMock(tMock)
defer resetNowEpochSecondsMock()
observer := newObserver(ns, pool, "abcd")
observer.start()
observer.observeStarted("foo", "bar", Q{"a": 1, "b": "wat"})
//observer.observeDone("foo", "bar", nil)
observer.drain()
observer.stop()
h := readHash(pool, redisKeyWorkerObservation(ns, "abcd"))
assert.Equal(t, "foo", h["job_name"])
assert.Equal(t, "bar", h["job_id"])
assert.Equal(t, fmt.Sprint(tMock), h["started_at"])
assert.Equal(t, `{"a":1,"b":"wat"}`, h["args"])
}
func TestObserverStartedDone(t *testing.T) {
pool := newTestPool(t)
ns := "work"
tMock := int64(1425263401)
setNowEpochSecondsMock(tMock)
defer resetNowEpochSecondsMock()
observer := newObserver(ns, pool, "abcd")
observer.start()
observer.observeStarted("foo", "bar", Q{"a": 1, "b": "wat"})
observer.observeDone("foo", "bar", nil)
observer.drain()
observer.stop()
h := readHash(pool, redisKeyWorkerObservation(ns, "abcd"))
assert.Equal(t, 0, len(h))
}
func TestObserverCheckin(t *testing.T) {
pool := newTestPool(t)
ns := "work"
observer := newObserver(ns, pool, "abcd")
observer.start()
tMock := int64(1425263401)
setNowEpochSecondsMock(tMock)
defer resetNowEpochSecondsMock()
observer.observeStarted("foo", "bar", Q{"a": 1, "b": "wat"})
tMockCheckin := int64(1425263402)
setNowEpochSecondsMock(tMockCheckin)
observer.observeCheckin("foo", "bar", "doin it")
observer.drain()
observer.stop()
h := readHash(pool, redisKeyWorkerObservation(ns, "abcd"))
assert.Equal(t, "foo", h["job_name"])
assert.Equal(t, "bar", h["job_id"])
assert.Equal(t, fmt.Sprint(tMock), h["started_at"])
assert.Equal(t, `{"a":1,"b":"wat"}`, h["args"])
assert.Equal(t, "doin it", h["checkin"])
assert.Equal(t, fmt.Sprint(tMockCheckin), h["checkin_at"])
}
func TestObserverCheckinFromJob(t *testing.T) {
pool := newTestPool(t)
ns := "work"
observer := newObserver(ns, pool, "abcd")
observer.start()
tMock := int64(1425263401)
setNowEpochSecondsMock(tMock)
defer resetNowEpochSecondsMock()
observer.observeStarted("foo", "barbar", Q{"a": 1, "b": "wat"})
tMockCheckin := int64(1425263402)
setNowEpochSecondsMock(tMockCheckin)
j := &Job{Name: "foo", ID: "barbar", observer: observer}
j.Checkin("sup")
observer.drain()
observer.stop()
h := readHash(pool, redisKeyWorkerObservation(ns, "abcd"))
assert.Equal(t, "foo", h["job_name"])
assert.Equal(t, "barbar", h["job_id"])
assert.Equal(t, fmt.Sprint(tMock), h["started_at"])
assert.Equal(t, "sup", h["checkin"])
assert.Equal(t, fmt.Sprint(tMockCheckin), h["checkin_at"])
}
func readHash(pool *redis.Pool, key string) map[string]string {
m := make(map[string]string)
conn := pool.Get()
defer conn.Close()
v, err := redis.Strings(conn.Do("HGETALL", key))
if err != nil {
panic("could not delete retry/dead queue: " + err.Error())
}
for i, l := 0, len(v); i < l; i += 2 {
m[v[i]] = v[i+1]
}
return m
}