Skip to content

Commit 3404d97

Browse files
authored
feat(mock): support mock testing (#62)
1 parent d431277 commit 3404d97

17 files changed

+254
-50
lines changed

consumer.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ import (
77
"sync"
88
"sync/atomic"
99
"time"
10+
11+
"github.com/golang-queue/queue/core"
1012
)
1113

12-
var _ Worker = (*Consumer)(nil)
14+
var _ core.Worker = (*Consumer)(nil)
1315

1416
var errMaxCapacity = errors.New("max capacity reached")
1517

1618
// Consumer for simple queue using buffer channel
1719
type Consumer struct {
18-
taskQueue chan QueuedMessage
19-
runFunc func(context.Context, QueuedMessage) error
20+
taskQueue chan core.QueuedMessage
21+
runFunc func(context.Context, core.QueuedMessage) error
2022
stop chan struct{}
2123
logger Logger
2224
stopOnce sync.Once
@@ -75,7 +77,7 @@ func (s *Consumer) handle(job Job) error {
7577
}
7678

7779
// Run to execute new task
78-
func (s *Consumer) Run(task QueuedMessage) error {
80+
func (s *Consumer) Run(task core.QueuedMessage) error {
7981
var data Job
8082
_ = json.Unmarshal(task.Bytes(), &data)
8183
if v, ok := task.(Job); ok {
@@ -104,7 +106,7 @@ func (s *Consumer) Shutdown() error {
104106
}
105107

106108
// Queue send task to the buffer channel
107-
func (s *Consumer) Queue(task QueuedMessage) error {
109+
func (s *Consumer) Queue(task core.QueuedMessage) error {
108110
if atomic.LoadInt32(&s.stopFlag) == 1 {
109111
return ErrQueueShutdown
110112
}
@@ -118,7 +120,7 @@ func (s *Consumer) Queue(task QueuedMessage) error {
118120
}
119121

120122
// Request a new task from channel
121-
func (s *Consumer) Request() (QueuedMessage, error) {
123+
func (s *Consumer) Request() (core.QueuedMessage, error) {
122124
clock := 0
123125
loop:
124126
for {
@@ -143,7 +145,7 @@ loop:
143145
func NewConsumer(opts ...Option) *Consumer {
144146
o := NewOptions(opts...)
145147
w := &Consumer{
146-
taskQueue: make(chan QueuedMessage, o.queueSize),
148+
taskQueue: make(chan core.QueuedMessage, o.queueSize),
147149
stop: make(chan struct{}),
148150
logger: o.logger,
149151
runFunc: o.fn,

consumer_test.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/golang-queue/queue/core"
13+
1214
"github.com/stretchr/testify/assert"
1315
)
1416

@@ -28,7 +30,7 @@ func TestCustomFuncAndWait(t *testing.T) {
2830
message: "foo",
2931
}
3032
w := NewConsumer(
31-
WithFn(func(ctx context.Context, m QueuedMessage) error {
33+
WithFn(func(ctx context.Context, m core.QueuedMessage) error {
3234
time.Sleep(500 * time.Millisecond)
3335
return nil
3436
}),
@@ -77,7 +79,7 @@ func TestJobReachTimeout(t *testing.T) {
7779
message: "foo",
7880
}
7981
w := NewConsumer(
80-
WithFn(func(ctx context.Context, m QueuedMessage) error {
82+
WithFn(func(ctx context.Context, m core.QueuedMessage) error {
8183
for {
8284
select {
8385
case <-ctx.Done():
@@ -111,7 +113,7 @@ func TestCancelJobAfterShutdown(t *testing.T) {
111113
}
112114
w := NewConsumer(
113115
WithLogger(NewEmptyLogger()),
114-
WithFn(func(ctx context.Context, m QueuedMessage) error {
116+
WithFn(func(ctx context.Context, m core.QueuedMessage) error {
115117
for {
116118
select {
117119
case <-ctx.Done():
@@ -144,7 +146,7 @@ func TestCancelJobAfterShutdown(t *testing.T) {
144146
func TestGoroutineLeak(t *testing.T) {
145147
w := NewConsumer(
146148
WithLogger(NewLogger()),
147-
WithFn(func(ctx context.Context, m QueuedMessage) error {
149+
WithFn(func(ctx context.Context, m core.QueuedMessage) error {
148150
for {
149151
select {
150152
case <-ctx.Done():
@@ -187,7 +189,7 @@ func TestGoroutinePanic(t *testing.T) {
187189
message: "foo",
188190
}
189191
w := NewConsumer(
190-
WithFn(func(ctx context.Context, m QueuedMessage) error {
192+
WithFn(func(ctx context.Context, m core.QueuedMessage) error {
191193
panic("missing something")
192194
}),
193195
)
@@ -208,7 +210,7 @@ func TestHandleTimeout(t *testing.T) {
208210
Payload: []byte("foo"),
209211
}
210212
w := NewConsumer(
211-
WithFn(func(ctx context.Context, m QueuedMessage) error {
213+
WithFn(func(ctx context.Context, m core.QueuedMessage) error {
212214
time.Sleep(200 * time.Millisecond)
213215
return nil
214216
}),
@@ -224,7 +226,7 @@ func TestHandleTimeout(t *testing.T) {
224226
}
225227

226228
w = NewConsumer(
227-
WithFn(func(ctx context.Context, m QueuedMessage) error {
229+
WithFn(func(ctx context.Context, m core.QueuedMessage) error {
228230
time.Sleep(200 * time.Millisecond)
229231
return nil
230232
}),
@@ -248,7 +250,7 @@ func TestJobComplete(t *testing.T) {
248250
Payload: []byte("foo"),
249251
}
250252
w := NewConsumer(
251-
WithFn(func(ctx context.Context, m QueuedMessage) error {
253+
WithFn(func(ctx context.Context, m core.QueuedMessage) error {
252254
return errors.New("job completed")
253255
}),
254256
)
@@ -263,7 +265,7 @@ func TestJobComplete(t *testing.T) {
263265
}
264266

265267
w = NewConsumer(
266-
WithFn(func(ctx context.Context, m QueuedMessage) error {
268+
WithFn(func(ctx context.Context, m core.QueuedMessage) error {
267269
time.Sleep(200 * time.Millisecond)
268270
return errors.New("job completed")
269271
}),
@@ -324,7 +326,7 @@ func TestTaskJobComplete(t *testing.T) {
324326
func TestIncreaseWorkerCount(t *testing.T) {
325327
w := NewConsumer(
326328
WithLogger(NewEmptyLogger()),
327-
WithFn(func(ctx context.Context, m QueuedMessage) error {
329+
WithFn(func(ctx context.Context, m core.QueuedMessage) error {
328330
time.Sleep(500 * time.Millisecond)
329331
return nil
330332
}),
@@ -354,7 +356,7 @@ func TestIncreaseWorkerCount(t *testing.T) {
354356

355357
func TestDecreaseWorkerCount(t *testing.T) {
356358
w := NewConsumer(
357-
WithFn(func(ctx context.Context, m QueuedMessage) error {
359+
WithFn(func(ctx context.Context, m core.QueuedMessage) error {
358360
time.Sleep(100 * time.Millisecond)
359361
return nil
360362
}),

worker.go renamed to core/worker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package queue
1+
package core
22

33
// Worker interface
44
type Worker interface {

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/golang-queue/queue
33
go 1.18
44

55
require (
6+
github.com/golang/mock v1.6.0
67
github.com/stretchr/testify v1.7.1
78
go.uber.org/goleak v1.1.12
89
)

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
4+
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
35
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
46
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
57
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -36,6 +38,7 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
3638
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
3739
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
3840
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
41+
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
3942
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
4043
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
4144
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

metric_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import (
66
"testing"
77
"time"
88

9+
"github.com/golang-queue/queue/core"
10+
911
"github.com/stretchr/testify/assert"
1012
)
1113

1214
func TestMetricData(t *testing.T) {
1315
w := NewConsumer(
14-
WithFn(func(ctx context.Context, m QueuedMessage) error {
16+
WithFn(func(ctx context.Context, m core.QueuedMessage) error {
1517
switch string(m.Bytes()) {
1618
case "foo1":
1719
panic("missing something")

mocks/mock_message.go

+48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mocks/mock_worker.go

+92
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mocks/mocks.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package mocks
2+
3+
import _ "github.com/golang/mock/mockgen/model"
4+
5+
//go:generate mockgen -package=mocks -destination=mock_worker.go github.com/golang-queue/queue/core Worker
6+
//go:generate mockgen -package=mocks -destination=mock_message.go github.com/golang-queue/queue/core QueuedMessage

0 commit comments

Comments
 (0)