This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_test.go
160 lines (125 loc) · 3.22 KB
/
pool_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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package priopool_test
import (
"fmt"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/alexvanin/priopool"
)
type syncList struct {
mu sync.Mutex
list []int
}
func (s *syncList) append(i int) {
s.mu.Lock()
defer s.mu.Unlock()
s.list = append(s.list, i)
}
func TestPriorityPool_New(t *testing.T) {
const pos, zero, neg = 1, 0, -1
cases := [...]struct {
pool, queue int
err error
}{
{pool: pos, queue: pos, err: nil},
{pool: pos, queue: zero, err: nil},
{pool: pos, queue: neg, err: nil},
{pool: zero, queue: pos, err: priopool.ErrPoolCapacitySize},
{pool: neg, queue: pos, err: priopool.ErrPoolCapacitySize},
}
for _, c := range cases {
_, err := priopool.New(c.pool, c.queue)
require.Equal(t, c.err, err, c)
}
}
func TestPriorityPool_Submit(t *testing.T) {
const (
poolCap, queueCap = 2, 4
totalCap = poolCap + queueCap
highPriority = 10
midPriority = 5
lowPriority = 1
)
p, err := priopool.New(poolCap, queueCap)
require.NoError(t, err)
result := new(syncList)
wg := new(sync.WaitGroup)
wg.Add(totalCap)
for i := 0; i < totalCap; i++ {
var priority uint32
switch {
case i < poolCap:
priority = midPriority // first put two middle priority tasks
case i < poolCap+queueCap/2:
priority = lowPriority // then add to queue two low priority tasks
default:
priority = highPriority // in the end fill queue with high priority tasks
}
err = p.Submit(priority, taskGenerator(int(priority), result, wg))
require.NoError(t, err)
}
err = p.Submit(highPriority, func() {})
require.Error(t, err, priopool.ErrQueueOverload)
wg.Wait()
expected := []int{
midPriority, midPriority, // first tasks that took workers from pool
highPriority, highPriority, // tasks from queue with higher priority
lowPriority, lowPriority, // remaining tasks from queue
}
require.Equal(t, expected, result.list)
t.Run("disabled queue", func(t *testing.T) {
p, err := priopool.New(poolCap, 0)
require.NoError(t, err)
wg := new(sync.WaitGroup)
wg.Add(poolCap)
for i := 0; i < poolCap; i++ {
id := i
err = p.Submit(lowPriority, taskGenerator(id, nil, wg))
require.NoError(t, err)
}
err = p.Submit(highPriority, func() {})
require.Error(t, err, priopool.ErrQueueOverload)
wg.Wait()
})
t.Run("disabled queue limit", func(t *testing.T) {
const n = 50
p, err := priopool.New(poolCap, -1)
require.NoError(t, err)
wg := new(sync.WaitGroup)
wg.Add(n)
for i := 0; i < n; i++ {
id := i
err = p.Submit(lowPriority, taskGenerator(id, nil, wg))
require.NoError(t, err)
}
wg.Wait()
})
t.Run("non-priority order", func(t *testing.T) {
const n = 5
p, err := priopool.New(1, -1)
require.NoError(t, err)
wg := new(sync.WaitGroup)
wg.Add(n)
result := new(syncList)
for i := 0; i < n; i++ {
id := i
err = p.Submit(lowPriority, taskGenerator(id, result, wg))
require.NoError(t, err)
}
wg.Wait()
fmt.Println(result.list)
for i := 0; i < n; i++ {
require.Equal(t, i, result.list[i])
}
})
}
func taskGenerator(ind int, output *syncList, wg *sync.WaitGroup) func() {
return func() {
time.Sleep(100 * time.Millisecond)
if output != nil {
output.append(ind)
}
wg.Done()
}
}