mirror of
https://github.com/hibiken/asynq.git
synced 2024-12-26 07:42:17 +08:00
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)
This commit is contained in:
parent
711bfa371f
commit
0faf97f146
@ -5,6 +5,7 @@
|
|||||||
package asynq
|
package asynq
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -14,16 +15,28 @@ import (
|
|||||||
"github.com/hibiken/asynq/internal/log"
|
"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.
|
// variables used for package testing.
|
||||||
const (
|
var (
|
||||||
redisAddr = "localhost:6379"
|
redisAddr string
|
||||||
redisDB = 14
|
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 {
|
func setup(tb testing.TB) *redis.Client {
|
||||||
tb.Helper()
|
tb.Helper()
|
||||||
|
@ -28,6 +28,7 @@ func TestServer(t *testing.T) {
|
|||||||
c := NewClient(r)
|
c := NewClient(r)
|
||||||
srv := NewServer(r, Config{
|
srv := NewServer(r, Config{
|
||||||
Concurrency: 10,
|
Concurrency: 10,
|
||||||
|
LogLevel: testLogLevel,
|
||||||
})
|
})
|
||||||
|
|
||||||
// no-op handler
|
// 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")
|
ignoreOpt := goleak.IgnoreTopFunction("github.com/go-redis/redis/v7/internal/pool.(*ConnPool).reaper")
|
||||||
defer goleak.VerifyNoLeaks(t, ignoreOpt)
|
defer goleak.VerifyNoLeaks(t, ignoreOpt)
|
||||||
|
|
||||||
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{})
|
srv := NewServer(RedisClientOpt{Addr: ":6379"}, 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.
|
||||||
@ -83,7 +84,7 @@ func TestServerRun(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestServerErrServerStopped(t *testing.T) {
|
func TestServerErrServerStopped(t *testing.T) {
|
||||||
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{})
|
srv := NewServer(RedisClientOpt{Addr: ":6379"}, 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)
|
||||||
@ -96,7 +97,7 @@ func TestServerErrServerStopped(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestServerErrNilHandler(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)
|
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")
|
||||||
@ -105,7 +106,7 @@ func TestServerErrNilHandler(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestServerErrServerRunning(t *testing.T) {
|
func TestServerErrServerRunning(t *testing.T) {
|
||||||
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{})
|
srv := NewServer(RedisClientOpt{Addr: ":6379"}, 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)
|
||||||
@ -126,7 +127,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{})
|
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel})
|
||||||
srv.broker = testBroker
|
srv.broker = testBroker
|
||||||
srv.scheduler.broker = testBroker
|
srv.scheduler.broker = testBroker
|
||||||
srv.heartbeater.broker = testBroker
|
srv.heartbeater.broker = testBroker
|
||||||
@ -158,7 +159,7 @@ func TestServerWithFlakyBroker(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: redisAddr, DB: redisDB}, Config{})
|
srv := NewServer(RedisClientOpt{Addr: redisAddr, DB: redisDB}, Config{LogLevel: testLogLevel})
|
||||||
srv.broker = testBroker
|
srv.broker = testBroker
|
||||||
srv.scheduler.broker = testBroker
|
srv.scheduler.broker = testBroker
|
||||||
srv.heartbeater.broker = testBroker
|
srv.heartbeater.broker = testBroker
|
||||||
|
Loading…
Reference in New Issue
Block a user