Skip to content

Commit b2ec9f5

Browse files
committed
fix: Use localStorage for memqueue tests instead of Redis for #162
1 parent 303ad56 commit b2ec9f5

File tree

4 files changed

+93
-89
lines changed

4 files changed

+93
-89
lines changed

memqueue/bench_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ func BenchmarkCallAsync(b *testing.B) {
1313
ctx := context.Background()
1414

1515
q := memqueue.NewQueue(&taskq.QueueOptions{
16-
Name: "test",
16+
Name: "test",
17+
Storage: taskq.NewLocalStorage(),
1718
})
1819
defer q.Close()
1920

@@ -36,8 +37,8 @@ func BenchmarkNamedMessage(b *testing.B) {
3637
ctx := context.Background()
3738

3839
q := memqueue.NewQueue(&taskq.QueueOptions{
39-
Name: "test",
40-
Redis: redisRing(),
40+
Name: "test",
41+
Storage: taskq.NewLocalStorage(),
4142
})
4243
defer q.Close()
4344

memqueue/memqueue_test.go

+21-28
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"testing"
1212
"time"
1313

14-
"github.com/go-redis/redis/v8"
1514
. "github.com/onsi/ginkgo"
1615
. "github.com/onsi/gomega"
1716
uuid "github.com/satori/go.uuid"
@@ -39,7 +38,8 @@ var _ = Describe("message with args", func() {
3938

4039
BeforeEach(func() {
4140
q := memqueue.NewQueue(&taskq.QueueOptions{
42-
Name: "test",
41+
Name: "test",
42+
Storage: taskq.NewLocalStorage(),
4343
})
4444
task := taskq.RegisterTask(&taskq.TaskOptions{
4545
Name: "test",
@@ -68,7 +68,8 @@ var _ = Describe("context.Context", func() {
6868

6969
BeforeEach(func() {
7070
q := memqueue.NewQueue(&taskq.QueueOptions{
71-
Name: "test",
71+
Name: "test",
72+
Storage: taskq.NewLocalStorage(),
7273
})
7374
task := taskq.RegisterTask(&taskq.TaskOptions{
7475
Name: "test",
@@ -97,7 +98,8 @@ var _ = Describe("message with invalid number of args", func() {
9798

9899
BeforeEach(func() {
99100
q := memqueue.NewQueue(&taskq.QueueOptions{
100-
Name: "test",
101+
Name: "test",
102+
Storage: taskq.NewLocalStorage(),
101103
})
102104
task := taskq.RegisterTask(&taskq.TaskOptions{
103105
Name: "test",
@@ -132,7 +134,8 @@ var _ = Describe("HandlerFunc", func() {
132134

133135
BeforeEach(func() {
134136
q := memqueue.NewQueue(&taskq.QueueOptions{
135-
Name: "test",
137+
Name: "test",
138+
Storage: taskq.NewLocalStorage(),
136139
})
137140
task := taskq.RegisterTask(&taskq.TaskOptions{
138141
Name: "test",
@@ -168,7 +171,8 @@ var _ = Describe("message retry timing", func() {
168171
count = 0
169172
ch = make(chan time.Time, 10)
170173
q = memqueue.NewQueue(&taskq.QueueOptions{
171-
Name: "test",
174+
Name: "test",
175+
Storage: taskq.NewLocalStorage(),
172176
})
173177
task = taskq.RegisterTask(&taskq.TaskOptions{
174178
Name: "test",
@@ -231,7 +235,8 @@ var _ = Describe("failing queue with error handler", func() {
231235

232236
BeforeEach(func() {
233237
q = memqueue.NewQueue(&taskq.QueueOptions{
234-
Name: "test",
238+
Name: "test",
239+
Storage: taskq.NewLocalStorage(),
235240
})
236241
task := taskq.RegisterTask(&taskq.TaskOptions{
237242
Name: "test",
@@ -261,8 +266,8 @@ var _ = Describe("named message", func() {
261266

262267
BeforeEach(func() {
263268
q := memqueue.NewQueue(&taskq.QueueOptions{
264-
Name: "test",
265-
Redis: redisRing(),
269+
Name: "test",
270+
Storage: taskq.NewLocalStorage(),
266271
})
267272
task := taskq.RegisterTask(&taskq.TaskOptions{
268273
Name: "test",
@@ -306,8 +311,8 @@ var _ = Describe("CallOnce", func() {
306311
now = time.Now()
307312

308313
q := memqueue.NewQueue(&taskq.QueueOptions{
309-
Name: "test",
310-
Redis: redisRing(),
314+
Name: "test",
315+
Storage: taskq.NewLocalStorage(),
311316
})
312317
task := taskq.RegisterTask(&taskq.TaskOptions{
313318
Name: "test",
@@ -349,7 +354,8 @@ var _ = Describe("stress testing", func() {
349354

350355
BeforeEach(func() {
351356
q := memqueue.NewQueue(&taskq.QueueOptions{
352-
Name: "test",
357+
Name: "test",
358+
Storage: taskq.NewLocalStorage(),
353359
})
354360
task := taskq.RegisterTask(&taskq.TaskOptions{
355361
Name: "test",
@@ -381,6 +387,7 @@ var _ = Describe("stress testing failing queue", func() {
381387
q := memqueue.NewQueue(&taskq.QueueOptions{
382388
Name: "test",
383389
PauseErrorsThreshold: -1,
390+
Storage: taskq.NewLocalStorage(),
384391
})
385392
task := taskq.RegisterTask(&taskq.TaskOptions{
386393
Name: "test",
@@ -416,8 +423,8 @@ var _ = Describe("empty queue", func() {
416423
BeforeEach(func() {
417424
processed = 0
418425
q = memqueue.NewQueue(&taskq.QueueOptions{
419-
Name: "test",
420-
Redis: redisRing(),
426+
Name: "test",
427+
Storage: taskq.NewLocalStorage(),
421428
})
422429
task = taskq.RegisterTask(&taskq.TaskOptions{
423430
Name: "test",
@@ -511,17 +518,3 @@ func slot(period time.Duration) int64 {
511518
}
512519
return tm.Unix() / periodSec
513520
}
514-
515-
var (
516-
ringOnce sync.Once
517-
ring *redis.Ring
518-
)
519-
520-
func redisRing() *redis.Ring {
521-
ringOnce.Do(func() {
522-
ring = redis.NewRing(&redis.RingOptions{
523-
Addrs: map[string]string{"0": ":6379"},
524-
})
525-
})
526-
return ring
527-
}

storage.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package taskq
2+
3+
import (
4+
"context"
5+
"sync"
6+
"time"
7+
8+
"github.com/hashicorp/golang-lru/simplelru"
9+
)
10+
11+
type Storage interface {
12+
Exists(ctx context.Context, key string) bool
13+
}
14+
15+
var _ Storage = (*localStorage)(nil)
16+
var _ Storage = (*redisStorage)(nil)
17+
18+
// LOCAL
19+
20+
type localStorage struct {
21+
mu sync.Mutex
22+
cache *simplelru.LRU
23+
}
24+
25+
func NewLocalStorage() Storage {
26+
return &localStorage{}
27+
}
28+
29+
func (s *localStorage) Exists(_ context.Context, key string) bool {
30+
s.mu.Lock()
31+
defer s.mu.Unlock()
32+
33+
if s.cache == nil {
34+
var err error
35+
s.cache, err = simplelru.NewLRU(128000, nil)
36+
if err != nil {
37+
panic(err)
38+
}
39+
}
40+
41+
_, ok := s.cache.Get(key)
42+
if ok {
43+
return true
44+
}
45+
46+
s.cache.Add(key, nil)
47+
return false
48+
}
49+
50+
// REDIS
51+
52+
type redisStorage struct {
53+
redis Redis
54+
}
55+
56+
func newRedisStorage(redis Redis) Storage {
57+
return &redisStorage{
58+
redis: redis,
59+
}
60+
}
61+
62+
func (s *redisStorage) Exists(ctx context.Context, key string) bool {
63+
val, err := s.redis.SetNX(ctx, key, "", 24*time.Hour).Result()
64+
if err != nil {
65+
return true
66+
}
67+
return !val
68+
}

taskq.go

-58
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import (
44
"context"
55
"log"
66
"os"
7-
"sync"
87
"time"
98

109
"github.com/go-redis/redis/v8"
11-
"github.com/hashicorp/golang-lru/simplelru"
1210

1311
"github.com/vmihailenco/taskq/v3/internal"
1412
)
@@ -42,59 +40,3 @@ type Redis interface {
4240
ScriptExists(ctx context.Context, scripts ...string) *redis.BoolSliceCmd
4341
ScriptLoad(ctx context.Context, script string) *redis.StringCmd
4442
}
45-
46-
type Storage interface {
47-
Exists(ctx context.Context, key string) bool
48-
}
49-
50-
type redisStorage struct {
51-
redis Redis
52-
}
53-
54-
var _ Storage = (*redisStorage)(nil)
55-
56-
func newRedisStorage(redis Redis) redisStorage {
57-
return redisStorage{
58-
redis: redis,
59-
}
60-
}
61-
62-
func (s redisStorage) Exists(ctx context.Context, key string) bool {
63-
if localCacheExists(key) {
64-
return true
65-
}
66-
67-
val, err := s.redis.SetNX(ctx, key, "", 24*time.Hour).Result()
68-
if err != nil {
69-
return true
70-
}
71-
return !val
72-
}
73-
74-
//------------------------------------------------------------------------------
75-
76-
var (
77-
mu sync.Mutex
78-
cache *simplelru.LRU
79-
)
80-
81-
func localCacheExists(key string) bool {
82-
mu.Lock()
83-
defer mu.Unlock()
84-
85-
if cache == nil {
86-
var err error
87-
cache, err = simplelru.NewLRU(128000, nil)
88-
if err != nil {
89-
panic(err)
90-
}
91-
}
92-
93-
_, ok := cache.Get(key)
94-
if ok {
95-
return true
96-
}
97-
98-
cache.Add(key, nil)
99-
return false
100-
}

0 commit comments

Comments
 (0)