Skip to content

Commit 6560f6a

Browse files
committed
feat: switch to connection strings and add debug option
- Add `github.com/yassinebenaid/godump` dependency to `go.mod` - Add `debug` field to `options` struct - Import `godump` package in `redis.go` - Add conditional debug dumping of options in `NewWorker` function - Replace `WithAddr(host01)` with `WithConnectionString(endpoint)` in multiple test functions - Initialize Redis container and clean up in multiple test functions Signed-off-by: appleboy <[email protected]>
1 parent dc7e447 commit 6560f6a

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/redis/go-redis/v9 v9.7.0
88
github.com/stretchr/testify v1.10.0
99
github.com/testcontainers/testcontainers-go v0.35.0
10+
github.com/yassinebenaid/godump v0.11.1
1011
go.uber.org/goleak v1.3.0
1112
)
1213

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFA
125125
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
126126
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
127127
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
128+
github.com/yassinebenaid/godump v0.11.1 h1:SPujx/XaYqGDfmNh7JI3dOyCUVrG0bG2duhO3Eh2EhI=
129+
github.com/yassinebenaid/godump v0.11.1/go.mod h1:dc/0w8wmg6kVIvNGAzbKH1Oa54dXQx8SNKh4dPRyW44=
128130
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
129131
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
130132
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=

options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type options struct {
2525
sentinel bool
2626
masterName string
2727
tls *tls.Config
28+
debug bool
2829
}
2930

3031
// WithAddr setup the addr of redis

redis.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/golang-queue/queue/job"
1414

1515
"github.com/redis/go-redis/v9"
16+
"github.com/yassinebenaid/godump"
1617
)
1718

1819
var _ core.Worker = (*Worker)(nil)
@@ -40,6 +41,10 @@ func NewWorker(opts ...Option) *Worker {
4041
stop: make(chan struct{}),
4142
}
4243

44+
if w.opts.debug {
45+
godump.Dump(w.opts)
46+
}
47+
4348
options := &redis.Options{
4449
Addr: w.opts.addr,
4550
Username: w.opts.username,

redis_test.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,12 @@ func TestRedisDefaultFlow(t *testing.T) {
8989
}
9090

9191
func TestRedisShutdown(t *testing.T) {
92+
ctx := context.Background()
93+
redisC, endpoint := setupRedisContainer(ctx, t)
94+
defer testcontainers.CleanupContainer(t, redisC)
95+
9296
w := NewWorker(
93-
WithAddr(host01),
97+
WithConnectionString(endpoint),
9498
WithChannel("test2"),
9599
)
96100
q, err := queue.NewQueue(
@@ -108,11 +112,14 @@ func TestRedisShutdown(t *testing.T) {
108112
}
109113

110114
func TestCustomFuncAndWait(t *testing.T) {
115+
ctx := context.Background()
116+
redisC, endpoint := setupRedisContainer(ctx, t)
117+
defer testcontainers.CleanupContainer(t, redisC)
111118
m := &mockMessage{
112119
Message: "foo",
113120
}
114121
w := NewWorker(
115-
WithAddr(host01),
122+
WithConnectionString(endpoint),
116123
WithChannel("test3"),
117124
WithRunFunc(func(ctx context.Context, m core.QueuedMessage) error {
118125
time.Sleep(500 * time.Millisecond)
@@ -196,11 +203,14 @@ func TestRedisSentinel(t *testing.T) {
196203
}
197204

198205
func TestEnqueueJobAfterShutdown(t *testing.T) {
206+
ctx := context.Background()
207+
redisC, endpoint := setupRedisContainer(ctx, t)
208+
defer testcontainers.CleanupContainer(t, redisC)
199209
m := mockMessage{
200210
Message: "foo",
201211
}
202212
w := NewWorker(
203-
WithAddr(host01),
213+
WithConnectionString(endpoint),
204214
)
205215
q, err := queue.NewQueue(
206216
queue.WithWorker(w),
@@ -218,11 +228,14 @@ func TestEnqueueJobAfterShutdown(t *testing.T) {
218228
}
219229

220230
func TestJobReachTimeout(t *testing.T) {
231+
ctx := context.Background()
232+
redisC, endpoint := setupRedisContainer(ctx, t)
233+
defer testcontainers.CleanupContainer(t, redisC)
221234
m := mockMessage{
222235
Message: "foo",
223236
}
224237
w := NewWorker(
225-
WithAddr(host01),
238+
WithConnectionString(endpoint),
226239
WithChannel("timeout"),
227240
WithRunFunc(func(ctx context.Context, m core.QueuedMessage) error {
228241
for {
@@ -255,11 +268,14 @@ func TestJobReachTimeout(t *testing.T) {
255268
}
256269

257270
func TestCancelJobAfterShutdown(t *testing.T) {
271+
ctx := context.Background()
272+
redisC, endpoint := setupRedisContainer(ctx, t)
273+
defer testcontainers.CleanupContainer(t, redisC)
258274
m := mockMessage{
259275
Message: "test",
260276
}
261277
w := NewWorker(
262-
WithAddr(host01),
278+
WithConnectionString(endpoint),
263279
WithChannel("cancel"),
264280
WithLogger(queue.NewLogger()),
265281
WithRunFunc(func(ctx context.Context, m core.QueuedMessage) error {
@@ -293,11 +309,14 @@ func TestCancelJobAfterShutdown(t *testing.T) {
293309
}
294310

295311
func TestGoroutineLeak(t *testing.T) {
312+
ctx := context.Background()
313+
redisC, endpoint := setupRedisContainer(ctx, t)
314+
defer testcontainers.CleanupContainer(t, redisC)
296315
m := mockMessage{
297316
Message: "foo",
298317
}
299318
w := NewWorker(
300-
WithAddr(host01),
319+
WithConnectionString(endpoint),
301320
WithChannel("GoroutineLeak"),
302321
WithLogger(queue.NewEmptyLogger()),
303322
WithRunFunc(func(ctx context.Context, m core.QueuedMessage) error {
@@ -338,11 +357,14 @@ func TestGoroutineLeak(t *testing.T) {
338357
}
339358

340359
func TestGoroutinePanic(t *testing.T) {
360+
ctx := context.Background()
361+
redisC, endpoint := setupRedisContainer(ctx, t)
362+
defer testcontainers.CleanupContainer(t, redisC)
341363
m := mockMessage{
342364
Message: "foo",
343365
}
344366
w := NewWorker(
345-
WithAddr(host01),
367+
WithConnectionString(endpoint),
346368
WithChannel("GoroutinePanic"),
347369
WithRunFunc(func(ctx context.Context, m core.QueuedMessage) error {
348370
panic("missing something")

0 commit comments

Comments
 (0)