2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-25 23:32:17 +08:00

Merge pull request #945 from Shopify/fix-test-default-port

Update tests to use the configured Redis address
This commit is contained in:
Mohamed Sohail 2024-10-29 08:45:33 +03:00 committed by GitHub
commit ef0d32965f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 37 deletions

View File

@ -32,6 +32,7 @@ func (p *FakeConfigProvider) GetConfigs() ([]*PeriodicTaskConfig, error) {
} }
func TestNewPeriodicTaskManager(t *testing.T) { func TestNewPeriodicTaskManager(t *testing.T) {
redisConnOpt := getRedisConnOpt(t)
cfgs := []*PeriodicTaskConfig{ cfgs := []*PeriodicTaskConfig{
{Cronspec: "* * * * *", Task: NewTask("foo", nil)}, {Cronspec: "* * * * *", Task: NewTask("foo", nil)},
{Cronspec: "* * * * *", Task: NewTask("bar", nil)}, {Cronspec: "* * * * *", Task: NewTask("bar", nil)},
@ -43,14 +44,14 @@ func TestNewPeriodicTaskManager(t *testing.T) {
{ {
desc: "with provider and redisConnOpt", desc: "with provider and redisConnOpt",
opts: PeriodicTaskManagerOpts{ opts: PeriodicTaskManagerOpts{
RedisConnOpt: RedisClientOpt{Addr: ":6379"}, RedisConnOpt: redisConnOpt,
PeriodicTaskConfigProvider: &FakeConfigProvider{cfgs: cfgs}, PeriodicTaskConfigProvider: &FakeConfigProvider{cfgs: cfgs},
}, },
}, },
{ {
desc: "with sync option", desc: "with sync option",
opts: PeriodicTaskManagerOpts{ opts: PeriodicTaskManagerOpts{
RedisConnOpt: RedisClientOpt{Addr: ":6379"}, RedisConnOpt: redisConnOpt,
PeriodicTaskConfigProvider: &FakeConfigProvider{cfgs: cfgs}, PeriodicTaskConfigProvider: &FakeConfigProvider{cfgs: cfgs},
SyncInterval: 5 * time.Minute, SyncInterval: 5 * time.Minute,
}, },
@ -58,7 +59,7 @@ func TestNewPeriodicTaskManager(t *testing.T) {
{ {
desc: "with scheduler option", desc: "with scheduler option",
opts: PeriodicTaskManagerOpts{ opts: PeriodicTaskManagerOpts{
RedisConnOpt: RedisClientOpt{Addr: ":6379"}, RedisConnOpt: redisConnOpt,
PeriodicTaskConfigProvider: &FakeConfigProvider{cfgs: cfgs}, PeriodicTaskConfigProvider: &FakeConfigProvider{cfgs: cfgs},
SyncInterval: 5 * time.Minute, SyncInterval: 5 * time.Minute,
SchedulerOpts: &SchedulerOpts{ SchedulerOpts: &SchedulerOpts{
@ -74,37 +75,33 @@ func TestNewPeriodicTaskManager(t *testing.T) {
t.Errorf("%s; NewPeriodicTaskManager returned error: %v", tc.desc, err) t.Errorf("%s; NewPeriodicTaskManager returned error: %v", tc.desc, err)
} }
} }
}
func TestNewPeriodicTaskManagerError(t *testing.T) { t.Run("error", func(t *testing.T) {
cfgs := []*PeriodicTaskConfig{ tests := []struct {
{Cronspec: "* * * * *", Task: NewTask("foo", nil)}, desc string
{Cronspec: "* * * * *", Task: NewTask("bar", nil)}, opts PeriodicTaskManagerOpts
} }{
tests := []struct { {
desc string desc: "without provider",
opts PeriodicTaskManagerOpts opts: PeriodicTaskManagerOpts{
}{ RedisConnOpt: redisConnOpt,
{ },
desc: "without provider",
opts: PeriodicTaskManagerOpts{
RedisConnOpt: RedisClientOpt{Addr: ":6379"},
}, },
}, {
{ desc: "without redisConOpt",
desc: "without redisConOpt", opts: PeriodicTaskManagerOpts{
opts: PeriodicTaskManagerOpts{ PeriodicTaskConfigProvider: &FakeConfigProvider{cfgs: cfgs},
PeriodicTaskConfigProvider: &FakeConfigProvider{cfgs: cfgs}, },
}, },
},
}
for _, tc := range tests {
_, err := NewPeriodicTaskManager(tc.opts)
if err == nil {
t.Errorf("%s; NewPeriodicTaskManager did not return error", tc.desc)
} }
}
for _, tc := range tests {
_, err := NewPeriodicTaskManager(tc.opts)
if err == nil {
t.Errorf("%s; NewPeriodicTaskManager did not return error", tc.desc)
}
}
})
} }
func TestPeriodicTaskConfigHash(t *testing.T) { func TestPeriodicTaskConfigHash(t *testing.T) {

View File

@ -5,12 +5,12 @@
package asynq package asynq
import ( import (
"github.com/redis/go-redis/v9"
"sync" "sync"
"testing" "testing"
"time" "time"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/redis/go-redis/v9"
"github.com/hibiken/asynq/internal/base" "github.com/hibiken/asynq/internal/base"
"github.com/hibiken/asynq/internal/testutil" "github.com/hibiken/asynq/internal/testutil"
@ -114,7 +114,7 @@ func TestSchedulerWhenRedisDown(t *testing.T) {
// Connect to non-existent redis instance to simulate a redis server being down. // Connect to non-existent redis instance to simulate a redis server being down.
scheduler := NewScheduler( scheduler := NewScheduler(
RedisClientOpt{Addr: ":9876"}, RedisClientOpt{Addr: ":9876"}, // no Redis listening to this port.
&SchedulerOpts{EnqueueErrorHandler: errorHandler}, &SchedulerOpts{EnqueueErrorHandler: errorHandler},
) )

View File

@ -85,7 +85,7 @@ func TestServerRun(t *testing.T) {
ignoreOpt := goleak.IgnoreTopFunction("github.com/redis/go-redis/v9/internal/pool.(*ConnPool).reaper") ignoreOpt := goleak.IgnoreTopFunction("github.com/redis/go-redis/v9/internal/pool.(*ConnPool).reaper")
defer goleak.VerifyNone(t, ignoreOpt) defer goleak.VerifyNone(t, ignoreOpt)
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel}) srv := NewServer(getRedisConnOpt(t), Config{LogLevel: testLogLevel})
done := make(chan struct{}) done := make(chan struct{})
// Make sure server exits when receiving TERM signal. // Make sure server exits when receiving TERM signal.
@ -110,7 +110,7 @@ func TestServerRun(t *testing.T) {
} }
func TestServerErrServerClosed(t *testing.T) { func TestServerErrServerClosed(t *testing.T) {
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel}) srv := NewServer(getRedisConnOpt(t), Config{LogLevel: testLogLevel})
handler := NewServeMux() handler := NewServeMux()
if err := srv.Start(handler); err != nil { if err := srv.Start(handler); err != nil {
t.Fatal(err) t.Fatal(err)
@ -123,7 +123,7 @@ func TestServerErrServerClosed(t *testing.T) {
} }
func TestServerErrNilHandler(t *testing.T) { func TestServerErrNilHandler(t *testing.T) {
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel}) srv := NewServer(getRedisConnOpt(t), Config{LogLevel: testLogLevel})
err := srv.Start(nil) err := srv.Start(nil)
if err == nil { if err == nil {
t.Error("Starting server with nil handler: (*Server).Start(nil) did not return error") t.Error("Starting server with nil handler: (*Server).Start(nil) did not return error")
@ -132,7 +132,7 @@ func TestServerErrNilHandler(t *testing.T) {
} }
func TestServerErrServerRunning(t *testing.T) { func TestServerErrServerRunning(t *testing.T) {
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel}) srv := NewServer(getRedisConnOpt(t), Config{LogLevel: testLogLevel})
handler := NewServeMux() handler := NewServeMux()
if err := srv.Start(handler); err != nil { if err := srv.Start(handler); err != nil {
t.Fatal(err) t.Fatal(err)
@ -153,7 +153,7 @@ func TestServerWithRedisDown(t *testing.T) {
}() }()
r := rdb.NewRDB(setup(t)) r := rdb.NewRDB(setup(t))
testBroker := testbroker.NewTestBroker(r) testBroker := testbroker.NewTestBroker(r)
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel}) srv := NewServer(getRedisConnOpt(t), Config{LogLevel: testLogLevel})
srv.broker = testBroker srv.broker = testBroker
srv.forwarder.broker = testBroker srv.forwarder.broker = testBroker
srv.heartbeater.broker = testBroker srv.heartbeater.broker = testBroker