-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasyncTask_test.go
More file actions
105 lines (84 loc) · 2.34 KB
/
Copy pathasyncTask_test.go
File metadata and controls
105 lines (84 loc) · 2.34 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package goasync
import (
"context"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
)
type mockTask struct {
Data int
}
func (m *mockTask) Process(ctx context.Context) error {
return nil
}
func TestNewAsyncTask(t *testing.T) {
assert := assert.New(t)
tests := map[string]struct {
inputs []Option
wants []interface{}
}{
"empty": {inputs: []Option{},
wants: []interface{}{1000, 5, time.Duration(60 * time.Second)}},
"check": {inputs: []Option{WithQueueSizeOption(10),
WithWorkerSizeOption(20),
WithTimeoutOption(10 * time.Second)},
wants: []interface{}{10, 20, time.Duration(10 * time.Second)}},
}
for _, test := range tests {
k, err := NewAsyncTask(test.inputs...)
assert.NoError(err)
assert.True(cmp.Equal(k.queueSize, test.wants[0]))
assert.True(cmp.Equal(k.workerSize, test.wants[1]))
assert.True(cmp.Equal(k.timeout, test.wants[2]))
}
}
func TestKeeper_AddTask(t *testing.T) {
assert := assert.New(t)
k, err := NewAsyncTask(WithQueueSizeOption(10), WithWorkerSizeOption(1), WithTimeoutOption(5*time.Second))
assert.NoError(err)
ctx := context.Background()
// fail
err = k.AddTask(ctx, nil)
assert.Error(err)
// success
for i := 0; i < 100; i++ {
err := k.AddTask(ctx, &mockTask{Data: i})
assert.NoError(err)
}
time.Sleep(1 * time.Second)
// stop
k.dispatcher.stop()
for i := 0; i < 10; i++ {
err := k.AddTask(ctx, &mockTask{Data: i})
assert.NoError(err)
}
assert.Equal(10, len(k.dispatcher.taskQueue))
timectx, _ := context.WithTimeout(ctx, time.Second*2)
err = k.AddTask(timectx, &mockTask{Data: 10})
assert.Error(err)
// start
k.dispatcher.start()
time.Sleep(1 * time.Second)
assert.Equal(0, len(k.dispatcher.taskQueue))
}
func TestKeeper_UnProcessedTaskSize(t *testing.T) {
assert := assert.New(t)
empty, err := NewAsyncTask(WithQueueSizeOption(10), WithWorkerSizeOption(1), WithTimeoutOption(5*time.Second))
assert.NoError(err)
exist, err := NewAsyncTask(WithQueueSizeOption(200), WithWorkerSizeOption(1), WithTimeoutOption(5*time.Second))
assert.NoError(err)
for i := 0; i < 10; i++ {
exist.AddTask(context.Background(), &mockTask{})
}
tests := map[string]struct {
dispatcher *keeper
count int
}{
"empty": {empty, 0},
"exist": {exist, 10},
}
for _, t := range tests {
assert.Equal(t.count, t.dispatcher.UnProcessedTaskSize())
}
}