-
Notifications
You must be signed in to change notification settings - Fork 36
/
heartbeat_test.go
80 lines (61 loc) · 1.9 KB
/
heartbeat_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
package workers
import (
"encoding/json"
"log"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestBuildHeartbeat(t *testing.T) {
namespace := "prod"
opts := testOptionsWithNamespace(namespace)
mgr, err := newTestManager(opts, true)
assert.NoError(t, err)
mgr.AddWorker("somequeue", 5, func(m *Msg) error {
return nil
})
mgr.AddWorker("second_queue", 10, func(m *Msg) error {
return nil
})
heartbeat, err := mgr.buildHeartbeat(time.Now().UTC(), time.Second)
assert.Nil(t, err)
hostname, _ := os.Hostname()
info := &HeartbeatInfo{}
err = json.Unmarshal([]byte(heartbeat.Info), info)
assert.Nil(t, err)
assert.Equal(t, hostname, info.Hostname)
assert.Equal(t, "prod", info.Tag)
assert.ElementsMatch(t, []string{"somequeue", "second_queue"}, info.Queues)
assert.Equal(t, 15, info.Concurrency)
assert.Equal(t, []string{}, info.Labels)
assert.Equal(t, false, heartbeat.Quiet)
}
func TestBuildHeartbeatWorkerMessage(t *testing.T) {
namespace := "prod"
opts := testOptionsWithNamespace(namespace)
mgr, err := newTestManager(opts, true)
assert.NoError(t, err)
mgr.AddWorker("somequeue", 1, func(m *Msg) error {
return nil
})
msg, err := NewMsg("{\"class\":\"MyWorker\",\"jid\":\"jid-123\"}")
assert.NoError(t, err)
testLogger := log.New(os.Stdout, "test-go-workers2: ", log.Ldate|log.Lmicroseconds)
tr := newTaskRunner(testLogger, func(m *Msg) error {
return nil
})
tr.currentMsg = msg
firstWorker := mgr.workers[0]
firstWorker.inProgressQueue = "testinprogressqueue"
firstWorker.runners = []*taskRunner{tr}
heartbeat, err := mgr.buildHeartbeat(time.Now().UTC(), time.Second)
assert.Nil(t, err)
assert.Equal(t, 1, len(heartbeat.WorkerHeartbeats))
for _, v := range heartbeat.WorkerHeartbeats {
assert.Equal(t, firstWorker.queue, v.Queue)
assert.Equal(t, firstWorker.inProgressQueue, v.InProgressQueue)
assert.Equal(t, tr.tid, v.Tid)
assert.Nil(t, err)
}
}