From 04b3a3475d2f213f93f58f48e47a35ecf59673fa Mon Sep 17 00:00:00 2001 From: Pior Bastida Date: Mon, 28 Oct 2024 12:48:56 +0100 Subject: [PATCH] Update tests to use the configured Redis address --- periodic_task_manager_test.go | 57 +++++++++++++++++------------------ scheduler_test.go | 4 +-- server_test.go | 10 +++--- 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/periodic_task_manager_test.go b/periodic_task_manager_test.go index 24d5e5b..b3c067a 100644 --- a/periodic_task_manager_test.go +++ b/periodic_task_manager_test.go @@ -32,6 +32,7 @@ func (p *FakeConfigProvider) GetConfigs() ([]*PeriodicTaskConfig, error) { } func TestNewPeriodicTaskManager(t *testing.T) { + redisConnOpt := getRedisConnOpt(t) cfgs := []*PeriodicTaskConfig{ {Cronspec: "* * * * *", Task: NewTask("foo", nil)}, {Cronspec: "* * * * *", Task: NewTask("bar", nil)}, @@ -43,14 +44,14 @@ func TestNewPeriodicTaskManager(t *testing.T) { { desc: "with provider and redisConnOpt", opts: PeriodicTaskManagerOpts{ - RedisConnOpt: RedisClientOpt{Addr: ":6379"}, + RedisConnOpt: redisConnOpt, PeriodicTaskConfigProvider: &FakeConfigProvider{cfgs: cfgs}, }, }, { desc: "with sync option", opts: PeriodicTaskManagerOpts{ - RedisConnOpt: RedisClientOpt{Addr: ":6379"}, + RedisConnOpt: redisConnOpt, PeriodicTaskConfigProvider: &FakeConfigProvider{cfgs: cfgs}, SyncInterval: 5 * time.Minute, }, @@ -58,7 +59,7 @@ func TestNewPeriodicTaskManager(t *testing.T) { { desc: "with scheduler option", opts: PeriodicTaskManagerOpts{ - RedisConnOpt: RedisClientOpt{Addr: ":6379"}, + RedisConnOpt: redisConnOpt, PeriodicTaskConfigProvider: &FakeConfigProvider{cfgs: cfgs}, SyncInterval: 5 * time.Minute, SchedulerOpts: &SchedulerOpts{ @@ -74,37 +75,33 @@ func TestNewPeriodicTaskManager(t *testing.T) { t.Errorf("%s; NewPeriodicTaskManager returned error: %v", tc.desc, err) } } -} -func TestNewPeriodicTaskManagerError(t *testing.T) { - cfgs := []*PeriodicTaskConfig{ - {Cronspec: "* * * * *", Task: NewTask("foo", nil)}, - {Cronspec: "* * * * *", Task: NewTask("bar", nil)}, - } - tests := []struct { - desc string - opts PeriodicTaskManagerOpts - }{ - { - desc: "without provider", - opts: PeriodicTaskManagerOpts{ - RedisConnOpt: RedisClientOpt{Addr: ":6379"}, + t.Run("error", func(t *testing.T) { + tests := []struct { + desc string + opts PeriodicTaskManagerOpts + }{ + { + desc: "without provider", + opts: PeriodicTaskManagerOpts{ + RedisConnOpt: redisConnOpt, + }, }, - }, - { - desc: "without redisConOpt", - opts: PeriodicTaskManagerOpts{ - PeriodicTaskConfigProvider: &FakeConfigProvider{cfgs: cfgs}, + { + desc: "without redisConOpt", + opts: PeriodicTaskManagerOpts{ + 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) { diff --git a/scheduler_test.go b/scheduler_test.go index a35f390..619a1b7 100644 --- a/scheduler_test.go +++ b/scheduler_test.go @@ -5,12 +5,12 @@ package asynq import ( - "github.com/redis/go-redis/v9" "sync" "testing" "time" "github.com/google/go-cmp/cmp" + "github.com/redis/go-redis/v9" "github.com/hibiken/asynq/internal/base" "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. scheduler := NewScheduler( - RedisClientOpt{Addr: ":9876"}, + RedisClientOpt{Addr: ":9876"}, // no Redis listening to this port. &SchedulerOpts{EnqueueErrorHandler: errorHandler}, ) diff --git a/server_test.go b/server_test.go index 3d39e4a..967f519 100644 --- a/server_test.go +++ b/server_test.go @@ -85,7 +85,7 @@ func TestServerRun(t *testing.T) { ignoreOpt := goleak.IgnoreTopFunction("github.com/redis/go-redis/v9/internal/pool.(*ConnPool).reaper") defer goleak.VerifyNone(t, ignoreOpt) - srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel}) + srv := NewServer(getRedisConnOpt(t), Config{LogLevel: testLogLevel}) done := make(chan struct{}) // Make sure server exits when receiving TERM signal. @@ -110,7 +110,7 @@ func TestServerRun(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() if err := srv.Start(handler); err != nil { t.Fatal(err) @@ -123,7 +123,7 @@ func TestServerErrServerClosed(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) if err == nil { 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) { - srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel}) + srv := NewServer(getRedisConnOpt(t), Config{LogLevel: testLogLevel}) handler := NewServeMux() if err := srv.Start(handler); err != nil { t.Fatal(err) @@ -153,7 +153,7 @@ func TestServerWithRedisDown(t *testing.T) { }() r := rdb.NewRDB(setup(t)) testBroker := testbroker.NewTestBroker(r) - srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel}) + srv := NewServer(getRedisConnOpt(t), Config{LogLevel: testLogLevel}) srv.broker = testBroker srv.forwarder.broker = testBroker srv.heartbeater.broker = testBroker