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"
|
|
|
|
|
|
|
|
"go.uber.org/goleak"
|
|
|
|
)
|
|
|
|
|
2020-04-12 23:16:42 +08:00
|
|
|
func TestServer(t *testing.T) {
|
2019-11-30 12:49:18 +08:00
|
|
|
// 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
|
|
|
}
|
2020-04-12 23:16:42 +08:00
|
|
|
c := NewClient(r)
|
|
|
|
srv := NewServer(r, Config{
|
2019-12-30 08:55:51 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-13 02:00:45 +08:00
|
|
|
err := srv.Start(HandlerFunc(h))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-30 12:49:18 +08:00
|
|
|
|
2020-04-13 02:00:45 +08:00
|
|
|
err = c.Enqueue(NewTask("send_email", map[string]interface{}{"recipient_id": 123}))
|
2020-02-24 07:40:04 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not enqueue a task: %v", err)
|
|
|
|
}
|
2020-01-05 05:13:46 +08:00
|
|
|
|
2020-04-12 23:16:42 +08:00
|
|
|
err = c.EnqueueAt(time.Now().Add(time.Hour), NewTask("send_email", map[string]interface{}{"recipient_id": 456}))
|
2020-02-24 07:40:04 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not enqueue a task: %v", err)
|
|
|
|
}
|
2019-11-30 12:49:18 +08:00
|
|
|
|
2020-04-13 02:00:45 +08:00
|
|
|
srv.Stop()
|
2019-11-30 12:49:18 +08:00
|
|
|
}
|
2020-01-08 13:17:01 +08:00
|
|
|
|
2020-04-15 00:01:22 +08:00
|
|
|
func TestServerErrServerStopped(t *testing.T) {
|
|
|
|
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{})
|
|
|
|
handler := NewServeMux()
|
|
|
|
if err := srv.Start(handler); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
srv.Stop()
|
|
|
|
err := srv.Start(handler)
|
|
|
|
if err != ErrServerStopped {
|
|
|
|
t.Errorf("Restarting server: (*Server).Start(handler) = %v, want ErrServerStopped error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServerErrNilHandler(t *testing.T) {
|
|
|
|
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{})
|
|
|
|
err := srv.Start(nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Starting server with nil handler: (*Server).Start(nil) did not return error")
|
|
|
|
srv.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServerErrServerRunning(t *testing.T) {
|
|
|
|
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{})
|
|
|
|
handler := NewServeMux()
|
|
|
|
if err := srv.Start(handler); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err := srv.Start(handler)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Calling (*Server).Start(handler) on already running server did not return error")
|
|
|
|
}
|
|
|
|
srv.Stop()
|
|
|
|
}
|