-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool_test.go
More file actions
173 lines (127 loc) · 2.66 KB
/
pool_test.go
File metadata and controls
173 lines (127 loc) · 2.66 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
package hypercache
import (
"errors"
"sync"
"testing"
"time"
)
func TestWorkerPool_EnqueueAndShutdown(t *testing.T) {
pool := NewWorkerPool(3)
var mu sync.Mutex
results := []int{}
// Enqueue 5 jobs
for i := range 5 {
val := i
pool.Enqueue(func() error {
mu.Lock()
results = append(results, val)
mu.Unlock()
return nil
})
}
pool.Shutdown()
if len(results) != 5 {
t.Errorf("expected 5 results, got %d", len(results))
}
}
func TestWorkerPool_JobErrorHandling(t *testing.T) {
pool := NewWorkerPool(2)
expectedErr := errors.New("job error")
pool.Enqueue(func() error {
return expectedErr
})
pool.Enqueue(func() error {
return nil
})
go func() {
time.Sleep(100 * time.Millisecond)
pool.Shutdown()
}()
var gotErr error
for err := range pool.Errors() {
if errors.Is(err, expectedErr) {
gotErr = err
}
}
if gotErr == nil {
t.Errorf("expected error to be received from Errors channel")
}
}
func TestWorkerPool_ResizeIncrease(t *testing.T) {
pool := NewWorkerPool(1)
var mu sync.Mutex
count := 0
for range 10 {
pool.Enqueue(func() error {
time.Sleep(10 * time.Millisecond)
mu.Lock()
count++
mu.Unlock()
return nil
})
}
pool.Resize(5)
pool.Shutdown()
if count != 10 {
t.Errorf("expected 10 jobs to be processed, got %d", count)
}
}
func TestWorkerPool_ResizeDecrease(t *testing.T) {
pool := NewWorkerPool(4)
var mu sync.Mutex
count := 0
for range 8 {
pool.Enqueue(func() error {
time.Sleep(10 * time.Millisecond)
mu.Lock()
count++
mu.Unlock()
return nil
})
}
pool.Resize(2)
pool.Shutdown()
if count != 8 {
t.Errorf("expected 8 jobs to be processed, got %d", count)
}
}
func TestWorkerPool_ResizeToZeroAndBack(t *testing.T) {
pool := NewWorkerPool(2)
done := make(chan struct{})
called := false
pool.Resize(0)
pool.Enqueue(func() error {
called = true
close(done)
return nil
})
// Resize back to 1 so the job can be processed
pool.Resize(1)
select {
case <-done:
case <-time.After(500 * time.Millisecond):
t.Fatal("job was not processed after resizing back up")
}
pool.Shutdown()
if !called {
t.Errorf("expected job to be called after resizing back up")
}
}
func TestWorkerPool_NegativeResizeDoesNothing(t *testing.T) {
pool := NewWorkerPool(2)
pool.Resize(-1)
if pool.workers != 2 {
t.Errorf("expected workers to remain 2, got %d", pool.workers)
}
pool.Shutdown()
}
func TestWorkerPool_EnqueueAfterShutdownPanics(t *testing.T) {
pool := NewWorkerPool(1)
pool.Shutdown()
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic when enqueuing after shutdown")
}
}()
pool.Enqueue(func() error { return nil })
}