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 (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-12-30 05:42:49 +08:00
|
|
|
"github.com/go-redis/redis/v7"
|
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)
|
|
|
|
|
2019-12-30 05:42:49 +08:00
|
|
|
r := redis.NewClient(&redis.Options{
|
2019-11-30 12:49:18 +08:00
|
|
|
Addr: "localhost:6379",
|
|
|
|
DB: 15,
|
|
|
|
})
|
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
|
|
|
|
h := func(task *Task) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-03 12:42:21 +08:00
|
|
|
bg.start(HandlerFunc(h))
|
2019-11-30 12:49:18 +08:00
|
|
|
|
2020-01-04 12:24:18 +08:00
|
|
|
client.Schedule(&Task{
|
2019-11-30 12:49:18 +08:00
|
|
|
Type: "send_email",
|
|
|
|
Payload: map[string]interface{}{"recipient_id": 123},
|
|
|
|
}, time.Now())
|
|
|
|
|
2020-01-04 12:24:18 +08:00
|
|
|
client.Schedule(&Task{
|
2019-11-30 12:49:18 +08:00
|
|
|
Type: "send_email",
|
|
|
|
Payload: map[string]interface{}{"recipient_id": 456},
|
|
|
|
}, time.Now().Add(time.Hour))
|
|
|
|
|
|
|
|
bg.stop()
|
|
|
|
}
|