forked from gocraft/work
-
Notifications
You must be signed in to change notification settings - Fork 11
/
heartbeater_test.go
55 lines (41 loc) · 1.22 KB
/
heartbeater_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
package work
import (
"testing"
"time"
"github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/assert"
)
func TestHeartbeater(t *testing.T) {
pool := newTestPool(t)
ns := "work"
tMock := int64(1425263409)
setNowEpochSecondsMock(tMock)
defer resetNowEpochSecondsMock()
jobTypes := map[string]*jobType{
"foo": nil,
"bar": nil,
}
heart := newWorkerPoolHeartbeater(ns, pool, "abcd", jobTypes, 10, []string{"ccc", "bbb"})
heart.start()
time.Sleep(20 * time.Millisecond)
assert.True(t, redisInSet(pool, redisKeyWorkerPools(ns), "abcd"))
h := readHash(pool, redisKeyHeartbeat(ns, "abcd"))
assert.Equal(t, "1425263409", h["heartbeat_at"])
assert.Equal(t, "1425263409", h["started_at"])
assert.Equal(t, "bar,foo", h["job_names"])
assert.Equal(t, "bbb,ccc", h["worker_ids"])
assert.Equal(t, "10", h["concurrency"])
assert.True(t, h["pid"] != "")
assert.True(t, h["host"] != "")
heart.stop()
assert.False(t, redisInSet(pool, redisKeyWorkerPools(ns), "abcd"))
}
func redisInSet(pool *redis.Pool, key, member string) bool {
conn := pool.Get()
defer conn.Close()
v, err := redis.Bool(conn.Do("SISMEMBER", key, member))
if err != nil {
panic("could not delete retry/dead queue: " + err.Error())
}
return v
}