2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-25 07:12:17 +08:00

Add test for background to verify no goroutine leaks

This commit is contained in:
Ken Hibino 2019-11-29 20:49:18 -08:00
parent 09662432c8
commit ad1291e0a1
4 changed files with 49 additions and 0 deletions

View File

@ -14,6 +14,7 @@ type Background struct {
mu sync.Mutex mu sync.Mutex
running bool running bool
rdb *rdb
poller *poller poller *poller
processor *processor processor *processor
} }
@ -24,6 +25,7 @@ func NewBackground(numWorkers int, opt *RedisOpt) *Background {
poller := newPoller(rdb, 5*time.Second, []string{scheduled, retry}) poller := newPoller(rdb, 5*time.Second, []string{scheduled, retry})
processor := newProcessor(rdb, numWorkers, nil) processor := newProcessor(rdb, numWorkers, nil)
return &Background{ return &Background{
rdb: rdb,
poller: poller, poller: poller,
processor: processor, processor: processor,
} }
@ -74,6 +76,7 @@ func (bg *Background) stop() {
bg.poller.terminate() bg.poller.terminate()
bg.processor.terminate() bg.processor.terminate()
bg.rdb.client.Close()
bg.processor.handler = nil bg.processor.handler = nil
bg.running = false bg.running = false
} }

43
background_test.go Normal file
View File

@ -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()
}

1
go.mod
View File

@ -7,4 +7,5 @@ require (
github.com/go-redis/redis/v7 v7.0.0-beta.4 github.com/go-redis/redis/v7 v7.0.0-beta.4
github.com/google/go-cmp v0.3.1 github.com/google/go-cmp v0.3.1
github.com/google/uuid v1.1.1 github.com/google/uuid v1.1.1
go.uber.org/goleak v0.10.0
) )

2
go.sum
View File

@ -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.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.8.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= 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/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/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= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=