2020-01-31 22:48:58 +08:00
|
|
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
|
|
|
h "github.com/hibiken/asynq/internal/asynqtest"
|
|
|
|
"github.com/hibiken/asynq/internal/base"
|
|
|
|
"github.com/hibiken/asynq/internal/rdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHeartbeater(t *testing.T) {
|
|
|
|
r := setup(t)
|
|
|
|
rdbClient := rdb.NewRDB(r)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
interval time.Duration
|
|
|
|
host string
|
|
|
|
pid int
|
2020-02-13 14:23:25 +08:00
|
|
|
queues map[string]int
|
2020-01-31 22:48:58 +08:00
|
|
|
concurrency int
|
|
|
|
}{
|
2020-02-13 14:23:25 +08:00
|
|
|
{time.Second, "some.address.ec2.aws.com", 45678, map[string]int{"default": 1}, 10},
|
2020-01-31 22:48:58 +08:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:22:48 +08:00
|
|
|
timeCmpOpt := cmpopts.EquateApproxTime(10 * time.Millisecond)
|
|
|
|
ignoreOpt := cmpopts.IgnoreUnexported(base.ProcessInfo{})
|
2020-01-31 22:48:58 +08:00
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r)
|
|
|
|
|
2020-02-16 13:59:06 +08:00
|
|
|
stateCh := make(chan string)
|
|
|
|
workerCh := make(chan int)
|
|
|
|
hb := newHeartbeater(rdbClient, tc.host, tc.pid, tc.concurrency, tc.queues, false, tc.interval, stateCh, workerCh)
|
2020-01-31 22:48:58 +08:00
|
|
|
|
2020-02-02 14:22:48 +08:00
|
|
|
want := &base.ProcessInfo{
|
2020-01-31 22:48:58 +08:00
|
|
|
Host: tc.host,
|
|
|
|
PID: tc.pid,
|
|
|
|
Queues: tc.queues,
|
|
|
|
Concurrency: tc.concurrency,
|
|
|
|
Started: time.Now(),
|
|
|
|
State: "running",
|
|
|
|
}
|
|
|
|
hb.start()
|
|
|
|
|
|
|
|
// allow for heartbeater to write to redis
|
|
|
|
time.Sleep(tc.interval * 2)
|
|
|
|
|
2020-02-02 14:22:48 +08:00
|
|
|
got, err := rdbClient.ReadProcessInfo(tc.host, tc.pid)
|
2020-01-31 22:48:58 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not read process status from redis: %v", err)
|
|
|
|
hb.terminate()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:22:48 +08:00
|
|
|
if diff := cmp.Diff(want, got, timeCmpOpt, ignoreOpt); diff != "" {
|
2020-01-31 22:48:58 +08:00
|
|
|
t.Errorf("redis stored process status %+v, want %+v; (-want, +got)\n%s", got, want, diff)
|
|
|
|
hb.terminate()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// state change
|
2020-02-16 13:59:06 +08:00
|
|
|
stateCh <- "stopped"
|
2020-01-31 22:48:58 +08:00
|
|
|
|
|
|
|
// allow for heartbeater to write to redis
|
|
|
|
time.Sleep(tc.interval * 2)
|
|
|
|
|
|
|
|
want.State = "stopped"
|
2020-02-02 14:22:48 +08:00
|
|
|
got, err = rdbClient.ReadProcessInfo(tc.host, tc.pid)
|
2020-01-31 22:48:58 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not read process status from redis: %v", err)
|
|
|
|
hb.terminate()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:22:48 +08:00
|
|
|
if diff := cmp.Diff(want, got, timeCmpOpt, ignoreOpt); diff != "" {
|
2020-01-31 22:48:58 +08:00
|
|
|
t.Errorf("redis stored process status %+v, want %+v; (-want, +got)\n%s", got, want, diff)
|
|
|
|
hb.terminate()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
hb.terminate()
|
|
|
|
}
|
|
|
|
}
|