-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask_test.go
55 lines (40 loc) · 1020 Bytes
/
task_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package queue_test
import (
"sync/atomic"
"testing"
"github.com/ibllex/go-queue"
"github.com/ibllex/go-queue/memq"
"github.com/stretchr/testify/assert"
)
const taskRoute = "task"
var counter = int32(0)
type FakeTask struct{}
type MockTask struct {
Count int32
}
func (t *MockTask) Handle() error {
atomic.AddInt32(&counter, t.Count)
return nil
}
func (t *MockTask) OnQueue() string {
return taskRoute
}
func TestRegisterTask(t *testing.T) {
assert.Panics(t, func() {
queue.RegisterTask(&FakeTask{})
})
assert.NotPanics(t, func() {
queue.RegisterTask(&MockTask{})
})
}
func TestDispatch(t *testing.T) {
atomic.StoreInt32(&counter, 0)
q, _ := memq.NewQueue(taskRoute, memq.WithSync(queue.TaskHandler()))
queue.Add(q)
queue.RegisterTask(&MockTask{})
assert.NotNil(t, queue.DispatchTask(&FakeTask{}))
assert.Nil(t, queue.DispatchTask(&MockTask{Count: 1}))
assert.Equal(t, int32(1), counter)
assert.Nil(t, queue.DispatchTask(&MockTask{Count: 3}))
assert.Equal(t, int32(4), counter)
}