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 (
|
2020-02-16 15:14:30 +08:00
|
|
|
"sync"
|
2020-01-31 22:48:58 +08:00
|
|
|
"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"
|
2020-04-18 22:56:27 +08:00
|
|
|
"github.com/hibiken/asynq/internal/testbroker"
|
2020-01-31 22:48:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHeartbeater(t *testing.T) {
|
|
|
|
r := setup(t)
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-01-31 22:48:58 +08:00
|
|
|
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-09-01 20:25:08 +08:00
|
|
|
{2 * time.Second, "localhost", 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)
|
2020-04-13 07:42:11 +08:00
|
|
|
ignoreOpt := cmpopts.IgnoreUnexported(base.ServerInfo{})
|
|
|
|
ignoreFieldOpt := cmpopts.IgnoreFields(base.ServerInfo{}, "ServerID")
|
2020-01-31 22:48:58 +08:00
|
|
|
for _, tc := range tests {
|
|
|
|
h.FlushDB(t, r)
|
|
|
|
|
2021-03-23 21:20:54 +08:00
|
|
|
state := base.NewServerState()
|
2020-05-18 03:33:55 +08:00
|
|
|
hb := newHeartbeater(heartbeaterParams{
|
2020-05-19 11:47:35 +08:00
|
|
|
logger: testLogger,
|
|
|
|
broker: rdbClient,
|
|
|
|
interval: tc.interval,
|
|
|
|
concurrency: tc.concurrency,
|
|
|
|
queues: tc.queues,
|
|
|
|
strictPriority: false,
|
2021-03-23 21:20:54 +08:00
|
|
|
state: state,
|
2021-01-28 07:55:43 +08:00
|
|
|
starting: make(chan *workerInfo),
|
2020-05-19 11:47:35 +08:00
|
|
|
finished: make(chan *base.TaskMessage),
|
2020-05-18 03:33:55 +08:00
|
|
|
})
|
2020-01-31 22:48:58 +08:00
|
|
|
|
2020-05-19 11:47:35 +08:00
|
|
|
// Change host and pid fields for testing purpose.
|
|
|
|
hb.host = tc.host
|
|
|
|
hb.pid = tc.pid
|
|
|
|
|
2021-03-23 21:20:54 +08:00
|
|
|
state.Set(base.StateActive)
|
2020-02-17 01:45:44 +08:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
hb.start(&wg)
|
|
|
|
|
2020-04-13 07:42:11 +08:00
|
|
|
want := &base.ServerInfo{
|
2020-01-31 22:48:58 +08:00
|
|
|
Host: tc.host,
|
|
|
|
PID: tc.pid,
|
|
|
|
Queues: tc.queues,
|
|
|
|
Concurrency: tc.concurrency,
|
|
|
|
Started: time.Now(),
|
2021-03-23 21:20:54 +08:00
|
|
|
Status: "active",
|
2020-01-31 22:48:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// allow for heartbeater to write to redis
|
2020-09-01 20:25:08 +08:00
|
|
|
time.Sleep(tc.interval)
|
2020-01-31 22:48:58 +08:00
|
|
|
|
2020-04-13 08:09:58 +08:00
|
|
|
ss, err := rdbClient.ListServers()
|
2020-01-31 22:48:58 +08:00
|
|
|
if err != nil {
|
2020-04-13 08:09:58 +08:00
|
|
|
t.Errorf("could not read server info from redis: %v", err)
|
2021-03-23 21:20:54 +08:00
|
|
|
hb.shutdown()
|
2020-01-31 22:48:58 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-13 08:09:58 +08:00
|
|
|
if len(ss) != 1 {
|
|
|
|
t.Errorf("(*RDB).ListServers returned %d process info, want 1", len(ss))
|
2021-03-23 21:20:54 +08:00
|
|
|
hb.shutdown()
|
2020-02-17 01:45:44 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-13 08:09:58 +08:00
|
|
|
if diff := cmp.Diff(want, ss[0], timeCmpOpt, ignoreOpt, ignoreFieldOpt); diff != "" {
|
|
|
|
t.Errorf("redis stored process status %+v, want %+v; (-want, +got)\n%s", ss[0], want, diff)
|
2021-03-23 21:20:54 +08:00
|
|
|
hb.shutdown()
|
2020-01-31 22:48:58 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-18 22:57:39 +08:00
|
|
|
// status change
|
2021-03-23 21:20:54 +08:00
|
|
|
state.Set(base.StateClosed)
|
2020-01-31 22:48:58 +08:00
|
|
|
|
|
|
|
// allow for heartbeater to write to redis
|
|
|
|
time.Sleep(tc.interval * 2)
|
|
|
|
|
2021-03-23 21:20:54 +08:00
|
|
|
want.Status = "closed"
|
2020-04-13 08:09:58 +08:00
|
|
|
ss, err = rdbClient.ListServers()
|
2020-01-31 22:48:58 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not read process status from redis: %v", err)
|
2021-03-23 21:20:54 +08:00
|
|
|
hb.shutdown()
|
2020-01-31 22:48:58 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-13 08:09:58 +08:00
|
|
|
if len(ss) != 1 {
|
|
|
|
t.Errorf("(*RDB).ListProcesses returned %d process info, want 1", len(ss))
|
2021-03-23 21:20:54 +08:00
|
|
|
hb.shutdown()
|
2020-02-17 01:45:44 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-13 08:09:58 +08:00
|
|
|
if diff := cmp.Diff(want, ss[0], timeCmpOpt, ignoreOpt, ignoreFieldOpt); diff != "" {
|
|
|
|
t.Errorf("redis stored process status %+v, want %+v; (-want, +got)\n%s", ss[0], want, diff)
|
2021-03-23 21:20:54 +08:00
|
|
|
hb.shutdown()
|
2020-01-31 22:48:58 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-23 21:20:54 +08:00
|
|
|
hb.shutdown()
|
2020-01-31 22:48:58 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-18 22:56:27 +08:00
|
|
|
|
|
|
|
func TestHeartbeaterWithRedisDown(t *testing.T) {
|
|
|
|
// Make sure that heartbeater goroutine doesn't panic
|
|
|
|
// if it cannot connect to redis.
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
t.Errorf("panic occurred: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
r := rdb.NewRDB(setup(t))
|
2020-09-08 21:51:01 +08:00
|
|
|
defer r.Close()
|
2020-04-18 22:56:27 +08:00
|
|
|
testBroker := testbroker.NewTestBroker(r)
|
2021-03-23 21:20:54 +08:00
|
|
|
state := base.NewServerState()
|
|
|
|
state.Set(base.StateActive)
|
2020-05-18 03:33:55 +08:00
|
|
|
hb := newHeartbeater(heartbeaterParams{
|
2020-05-19 11:47:35 +08:00
|
|
|
logger: testLogger,
|
|
|
|
broker: testBroker,
|
|
|
|
interval: time.Second,
|
|
|
|
concurrency: 10,
|
|
|
|
queues: map[string]int{"default": 1},
|
|
|
|
strictPriority: false,
|
2021-03-23 21:20:54 +08:00
|
|
|
state: state,
|
2021-01-28 07:55:43 +08:00
|
|
|
starting: make(chan *workerInfo),
|
2020-05-19 11:47:35 +08:00
|
|
|
finished: make(chan *base.TaskMessage),
|
2020-05-18 03:33:55 +08:00
|
|
|
})
|
2020-04-18 22:56:27 +08:00
|
|
|
|
|
|
|
testBroker.Sleep()
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
hb.start(&wg)
|
|
|
|
|
|
|
|
// wait for heartbeater to try writing data to redis
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
2021-03-23 21:20:54 +08:00
|
|
|
hb.shutdown()
|
2020-04-18 22:56:27 +08:00
|
|
|
}
|