From 24ee4b9693020196ddc28f901f01679f6cf4d462 Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Sun, 10 May 2020 16:47:46 -0700 Subject: [PATCH] Define test flags for package testing Added test flags for - redis address (defaults to "localhost:6379") - redis db number (defaults to 14) - log level (defaults to FATAL) --- asynq_test.go | 27 ++++++++++++++++++++------- server_test.go | 13 +++++++------ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/asynq_test.go b/asynq_test.go index 3aff019..b68256d 100644 --- a/asynq_test.go +++ b/asynq_test.go @@ -5,6 +5,7 @@ package asynq import ( + "flag" "sort" "testing" @@ -14,16 +15,28 @@ import ( "github.com/hibiken/asynq/internal/log" ) -// This file defines test helper functions used by -// other test files. +//============================================================================ +// This file defines helper functions and variables used in other test files. +//============================================================================ -// redis used for package testing. -const ( - redisAddr = "localhost:6379" - redisDB = 14 +// variables used for package testing. +var ( + redisAddr string + redisDB int + + testLogLevel = FatalLevel ) -var testLogger = log.NewLogger(nil) +var testLogger *log.Logger + +func init() { + flag.StringVar(&redisAddr, "redis_addr", "localhost:6379", "redis address to use in testing") + flag.IntVar(&redisDB, "redis_db", 14, "redis db number to use in testing") + flag.Var(&testLogLevel, "loglevel", "log level to use in testing") + + testLogger = log.NewLogger(nil) + testLogger.SetLevel(log.Level(testLogLevel)) +} func setup(tb testing.TB) *redis.Client { tb.Helper() diff --git a/server_test.go b/server_test.go index 6fb7f28..82d1c72 100644 --- a/server_test.go +++ b/server_test.go @@ -28,6 +28,7 @@ func TestServer(t *testing.T) { c := NewClient(r) srv := NewServer(r, Config{ Concurrency: 10, + LogLevel: testLogLevel, }) // no-op handler @@ -58,7 +59,7 @@ func TestServerRun(t *testing.T) { ignoreOpt := goleak.IgnoreTopFunction("github.com/go-redis/redis/v7/internal/pool.(*ConnPool).reaper") defer goleak.VerifyNoLeaks(t, ignoreOpt) - srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{}) + srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel}) done := make(chan struct{}) // Make sure server exits when receiving TERM signal. @@ -83,7 +84,7 @@ func TestServerRun(t *testing.T) { } func TestServerErrServerStopped(t *testing.T) { - srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{}) + srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel}) handler := NewServeMux() if err := srv.Start(handler); err != nil { t.Fatal(err) @@ -96,7 +97,7 @@ func TestServerErrServerStopped(t *testing.T) { } func TestServerErrNilHandler(t *testing.T) { - srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{}) + srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel}) err := srv.Start(nil) if err == nil { t.Error("Starting server with nil handler: (*Server).Start(nil) did not return error") @@ -105,7 +106,7 @@ func TestServerErrNilHandler(t *testing.T) { } func TestServerErrServerRunning(t *testing.T) { - srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{}) + srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel}) handler := NewServeMux() if err := srv.Start(handler); err != nil { t.Fatal(err) @@ -126,7 +127,7 @@ func TestServerWithRedisDown(t *testing.T) { }() r := rdb.NewRDB(setup(t)) testBroker := testbroker.NewTestBroker(r) - srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{}) + srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel}) srv.broker = testBroker srv.scheduler.broker = testBroker srv.heartbeater.broker = testBroker @@ -158,7 +159,7 @@ func TestServerWithFlakyBroker(t *testing.T) { }() r := rdb.NewRDB(setup(t)) testBroker := testbroker.NewTestBroker(r) - srv := NewServer(RedisClientOpt{Addr: redisAddr, DB: redisDB}, Config{}) + srv := NewServer(RedisClientOpt{Addr: redisAddr, DB: redisDB}, Config{LogLevel: testLogLevel}) srv.broker = testBroker srv.scheduler.broker = testBroker srv.heartbeater.broker = testBroker