Skip to content

Commit bed471f

Browse files
committed
Add producer/consumer test for goredis
1 parent 503243b commit bed471f

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

celery_test.go

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import (
1111

1212
"github.com/go-kit/log"
1313

14+
"github.com/marselester/gopher-celery/goredis"
1415
"github.com/marselester/gopher-celery/protocol"
1516
)
1617

1718
func TestExecuteTaskPanic(t *testing.T) {
18-
a := NewApp()
19-
a.Register(
19+
app := NewApp()
20+
app.Register(
2021
"myproject.apps.myapp.tasks.mytask",
2122
"important",
2223
func(ctx context.Context, p *TaskParam) error {
@@ -35,7 +36,7 @@ func TestExecuteTaskPanic(t *testing.T) {
3536
}
3637

3738
want := "unexpected task error"
38-
err := a.executeTask(context.Background(), &m)
39+
err := app.executeTask(context.Background(), &m)
3940
if !strings.HasPrefix(err.Error(), want) {
4041
t.Errorf("expected %q got %q", want, err)
4142
}
@@ -100,18 +101,18 @@ func TestExecuteTaskMiddlewares(t *testing.T) {
100101
}
101102
for name, tc := range tests {
102103
t.Run(name, func(t *testing.T) {
103-
a := NewApp(
104+
app := NewApp(
104105
WithMiddlewares(tc.middlewares...),
105106
)
106-
a.Register(
107+
app.Register(
107108
"myproject.apps.myapp.tasks.mytask",
108109
"important",
109110
func(ctx context.Context, p *TaskParam) error {
110111
return fmt.Errorf("task")
111112
},
112113
)
113114

114-
err := a.executeTask(ctx, &m)
115+
err := app.executeTask(ctx, &m)
115116
if !strings.HasPrefix(err.Error(), tc.want) {
116117
t.Errorf("expected %q got %q", tc.want, err)
117118
}
@@ -157,7 +158,7 @@ func TestProduceAndConsume(t *testing.T) {
157158
}
158159
}
159160

160-
func TestProduceAndConsume_100times(t *testing.T) {
161+
func TestProduceAndConsume100times(t *testing.T) {
161162
app := NewApp(WithLogger(log.NewJSONLogger(os.Stderr)))
162163
for i := 0; i < 100; i++ {
163164
err := app.Delay(
@@ -197,3 +198,47 @@ func TestProduceAndConsume_100times(t *testing.T) {
197198
t.Errorf("expected sum %d got %d", want, sum)
198199
}
199200
}
201+
202+
func TestGoredisProduceAndConsume100times(t *testing.T) {
203+
app := NewApp(
204+
WithBroker(goredis.NewBroker()),
205+
WithLogger(log.NewJSONLogger(os.Stderr)),
206+
)
207+
for i := 0; i < 100; i++ {
208+
err := app.Delay(
209+
"myproject.apps.myapp.tasks.mytask",
210+
"important",
211+
2,
212+
3,
213+
)
214+
if err != nil {
215+
t.Fatal(err)
216+
}
217+
}
218+
219+
// The test finishes either when ctx times out or all the tasks finish.
220+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
221+
t.Cleanup(cancel)
222+
223+
var sum int32
224+
app.Register(
225+
"myproject.apps.myapp.tasks.mytask",
226+
"important",
227+
func(ctx context.Context, p *TaskParam) error {
228+
p.NameArgs("a", "b")
229+
atomic.AddInt32(
230+
&sum,
231+
int32(p.MustInt("a")+p.MustInt("b")),
232+
)
233+
return nil
234+
},
235+
)
236+
if err := app.Run(ctx); err != nil {
237+
t.Error(err)
238+
}
239+
240+
var want int32 = 500
241+
if want != sum {
242+
t.Errorf("expected sum %d got %d", want, sum)
243+
}
244+
}

0 commit comments

Comments
 (0)