2020-01-03 10:13:16 +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.
|
|
|
|
|
2019-11-30 12:49:18 +08:00
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
2020-02-11 23:06:52 +08:00
|
|
|
"context"
|
2019-11-30 12:49:18 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-01-08 13:17:01 +08:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-11-30 12:49:18 +08:00
|
|
|
"go.uber.org/goleak"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBackground(t *testing.T) {
|
|
|
|
// https://github.com/go-redis/redis/issues/1029
|
|
|
|
ignoreOpt := goleak.IgnoreTopFunction("github.com/go-redis/redis/v7/internal/pool.(*ConnPool).reaper")
|
|
|
|
defer goleak.VerifyNoLeaks(t, ignoreOpt)
|
|
|
|
|
2020-01-17 13:01:27 +08:00
|
|
|
r := &RedisClientOpt{
|
2019-11-30 12:49:18 +08:00
|
|
|
Addr: "localhost:6379",
|
|
|
|
DB: 15,
|
2020-01-17 13:01:27 +08:00
|
|
|
}
|
2019-12-30 05:42:49 +08:00
|
|
|
client := NewClient(r)
|
2019-12-30 08:55:51 +08:00
|
|
|
bg := NewBackground(r, &Config{
|
|
|
|
Concurrency: 10,
|
|
|
|
})
|
2019-11-30 12:49:18 +08:00
|
|
|
|
|
|
|
// no-op handler
|
2020-02-11 23:06:52 +08:00
|
|
|
h := func(ctx context.Context, task *Task) error {
|
2019-11-30 12:49:18 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-03 12:42:21 +08:00
|
|
|
bg.start(HandlerFunc(h))
|
2019-11-30 12:49:18 +08:00
|
|
|
|
2020-02-24 07:40:04 +08:00
|
|
|
err := client.Enqueue(NewTask("send_email", map[string]interface{}{"recipient_id": 123}))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not enqueue a task: %v", err)
|
|
|
|
}
|
2020-01-05 05:13:46 +08:00
|
|
|
|
2020-02-24 07:40:04 +08:00
|
|
|
err = client.EnqueueAt(time.Now().Add(time.Hour), NewTask("send_email", map[string]interface{}{"recipient_id": 456}))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not enqueue a task: %v", err)
|
|
|
|
}
|
2019-11-30 12:49:18 +08:00
|
|
|
|
|
|
|
bg.stop()
|
|
|
|
}
|
2020-01-08 13:17:01 +08:00
|
|
|
|
|
|
|
func TestGCD(t *testing.T) {
|
|
|
|
tests := []struct {
|
2020-02-13 14:23:25 +08:00
|
|
|
input []int
|
|
|
|
want int
|
2020-01-08 13:17:01 +08:00
|
|
|
}{
|
2020-02-13 14:23:25 +08:00
|
|
|
{[]int{6, 2, 12}, 2},
|
|
|
|
{[]int{3, 3, 3}, 3},
|
|
|
|
{[]int{6, 3, 1}, 1},
|
|
|
|
{[]int{1}, 1},
|
|
|
|
{[]int{1, 0, 2}, 1},
|
|
|
|
{[]int{8, 0, 4}, 4},
|
|
|
|
{[]int{9, 12, 18, 30}, 3},
|
2020-01-08 13:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
got := gcd(tc.input...)
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("gcd(%v) = %d, want %d", tc.input, got, tc.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeQueueCfg(t *testing.T) {
|
|
|
|
tests := []struct {
|
2020-02-13 14:23:25 +08:00
|
|
|
input map[string]int
|
|
|
|
want map[string]int
|
2020-01-08 13:17:01 +08:00
|
|
|
}{
|
|
|
|
{
|
2020-02-13 14:23:25 +08:00
|
|
|
input: map[string]int{
|
2020-01-08 13:17:01 +08:00
|
|
|
"high": 100,
|
|
|
|
"default": 20,
|
|
|
|
"low": 5,
|
|
|
|
},
|
2020-02-13 14:23:25 +08:00
|
|
|
want: map[string]int{
|
2020-01-08 13:17:01 +08:00
|
|
|
"high": 20,
|
|
|
|
"default": 4,
|
|
|
|
"low": 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-02-13 14:23:25 +08:00
|
|
|
input: map[string]int{
|
2020-01-08 13:17:01 +08:00
|
|
|
"default": 10,
|
|
|
|
},
|
2020-02-13 14:23:25 +08:00
|
|
|
want: map[string]int{
|
2020-01-08 13:17:01 +08:00
|
|
|
"default": 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-02-13 14:23:25 +08:00
|
|
|
input: map[string]int{
|
2020-01-08 13:17:01 +08:00
|
|
|
"critical": 5,
|
|
|
|
"default": 1,
|
|
|
|
},
|
2020-02-13 14:23:25 +08:00
|
|
|
want: map[string]int{
|
2020-01-08 13:17:01 +08:00
|
|
|
"critical": 5,
|
|
|
|
"default": 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-02-13 14:23:25 +08:00
|
|
|
input: map[string]int{
|
2020-01-08 13:17:01 +08:00
|
|
|
"critical": 6,
|
|
|
|
"default": 3,
|
|
|
|
"low": 0,
|
|
|
|
},
|
2020-02-13 14:23:25 +08:00
|
|
|
want: map[string]int{
|
2020-01-08 13:17:01 +08:00
|
|
|
"critical": 2,
|
|
|
|
"default": 1,
|
|
|
|
"low": 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
got := normalizeQueueCfg(tc.input)
|
|
|
|
if diff := cmp.Diff(tc.want, got); diff != "" {
|
|
|
|
t.Errorf("normalizeQueueCfg(%v) = %v, want %v; (-want, +got):\n%s",
|
|
|
|
tc.input, got, tc.want, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|