From ad1291e0a1b3e3bcd531a6a3b68b95e1e6497ebe Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Fri, 29 Nov 2019 20:49:18 -0800 Subject: [PATCH] Add test for background to verify no goroutine leaks --- background.go | 3 +++ background_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 4 files changed, 49 insertions(+) create mode 100644 background_test.go diff --git a/background.go b/background.go index 9aa9f6b..bddc2e8 100644 --- a/background.go +++ b/background.go @@ -14,6 +14,7 @@ type Background struct { mu sync.Mutex running bool + rdb *rdb poller *poller processor *processor } @@ -24,6 +25,7 @@ func NewBackground(numWorkers int, opt *RedisOpt) *Background { poller := newPoller(rdb, 5*time.Second, []string{scheduled, retry}) processor := newProcessor(rdb, numWorkers, nil) return &Background{ + rdb: rdb, poller: poller, processor: processor, } @@ -74,6 +76,7 @@ func (bg *Background) stop() { bg.poller.terminate() bg.processor.terminate() + bg.rdb.client.Close() bg.processor.handler = nil bg.running = false } diff --git a/background_test.go b/background_test.go new file mode 100644 index 0000000..e59611c --- /dev/null +++ b/background_test.go @@ -0,0 +1,43 @@ +package asynq + +import ( + "testing" + "time" + + "go.uber.org/goleak" +) + +func TestBackground(t *testing.T) { + // https://github.com/go-redis/redis/issues/1029 + ignoreOpt := goleak.IgnoreTopFunction("github.com/go-redis/redis/v7/internal/pool.(*ConnPool).reaper") + defer goleak.VerifyNoLeaks(t, ignoreOpt) + + bg := NewBackground(10, &RedisOpt{ + Addr: "localhost:6379", + DB: 15, + }) + + client := NewClient(&RedisOpt{ + Addr: "localhost:6379", + DB: 15, + }) + + // no-op handler + h := func(task *Task) error { + return nil + } + + bg.start(h) + + client.Process(&Task{ + Type: "send_email", + Payload: map[string]interface{}{"recipient_id": 123}, + }, time.Now()) + + client.Process(&Task{ + Type: "send_email", + Payload: map[string]interface{}{"recipient_id": 456}, + }, time.Now().Add(time.Hour)) + + bg.stop() +} diff --git a/go.mod b/go.mod index 0ae1b01..1c06dec 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,5 @@ require ( github.com/go-redis/redis/v7 v7.0.0-beta.4 github.com/google/go-cmp v0.3.1 github.com/google/uuid v1.1.1 + go.uber.org/goleak v0.10.0 ) diff --git a/go.sum b/go.sum index 7177735..9d573e1 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +go.uber.org/goleak v0.10.0 h1:G3eWbSNIskeRqtsN/1uI5B+eP73y3JUuBsv9AZjehb4= +go.uber.org/goleak v0.10.0/go.mod h1:VCZuO8V8mFPlL0F5J5GK1rtHV3DrFcQ1R8ryq7FK0aI= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=