From 72ebe01b1c63e8587ecb81500e6a5e37997747d4 Mon Sep 17 00:00:00 2001 From: Suhas Karanth Date: Fri, 5 Mar 2021 13:55:19 +0530 Subject: [PATCH] Restore test for stop with unresponsive redis --- worker_pool_test.go | 4 ++-- worker_test.go | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/worker_pool_test.go b/worker_pool_test.go index 2aec6008..feef4ff4 100644 --- a/worker_pool_test.go +++ b/worker_pool_test.go @@ -154,7 +154,7 @@ func TestWorkersPoolRunSingleThreaded(t *testing.T) { assert.EqualValues(t, 0, listSize(pool, redisKeyJobs(ns, job1))) assert.EqualValues(t, 0, listSize(pool, redisKeyJobsInProgress(ns, wp.workerPoolID, job1))) assert.EqualValues(t, 0, getInt64(pool, redisKeyJobsLock(ns, job1))) - assert.EqualValues(t, 0, hgetInt64(pool, redisKeyJobsLockInfo(ns, job1), wp.workerPoolID)) + assert.False(t, hexists(pool, redisKeyJobsLockInfo(ns, job1), wp.workerPoolID)) } func TestWorkerPoolPauseSingleThreadedJobs(t *testing.T) { @@ -203,7 +203,7 @@ func TestWorkerPoolPauseSingleThreadedJobs(t *testing.T) { assert.EqualValues(t, 0, listSize(pool, redisKeyJobs(ns, job1))) assert.EqualValues(t, 0, listSize(pool, redisKeyJobsInProgress(ns, wp.workerPoolID, job1))) assert.EqualValues(t, 0, getInt64(pool, redisKeyJobsLock(ns, job1))) - assert.EqualValues(t, 0, hgetInt64(pool, redisKeyJobsLockInfo(ns, job1), wp.workerPoolID)) + assert.False(t, hexists(pool, redisKeyJobsLockInfo(ns, job1), wp.workerPoolID)) } // Test Helpers diff --git a/worker_test.go b/worker_test.go index 5652c6be..b609747b 100644 --- a/worker_test.go +++ b/worker_test.go @@ -366,6 +366,17 @@ func TestWorkersPaused(t *testing.T) { // Test that in the case of an unavailable Redis server, // the worker loop exits in the case of a WorkerPool.Stop +func TestStopWithUnavailableRedis(t *testing.T) { + redisPool := &redis.Pool{ + Dial: func() (redis.Conn, error) { + return redis.Dial("tcp", "notworking:6379", redis.DialConnectTimeout(1*time.Second)) + }, + } + wp := NewWorkerPool(TestContext{}, 10, "work", redisPool) + wp.Start() + wp.Stop() +} + func TestStop(t *testing.T) { s, err := miniredis.Run() assert.Nil(t, err) @@ -554,6 +565,17 @@ func hgetInt64(pool *redis.Pool, redisKey, hashKey string) int64 { return v } +func hexists(pool *redis.Pool, redisKey, hashKey string) bool { + conn := pool.Get() + defer conn.Close() + + ok, err := redis.Bool(conn.Do("HEXISTS", redisKey, hashKey)) + if err != nil { + panic("could not HEXISTS: " + err.Error()) + } + return ok +} + func jobOnZset(pool *redis.Pool, key string) (int64, *Job) { conn := pool.Get() defer conn.Close()