-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker-pool_test.go
48 lines (31 loc) · 1.21 KB
/
worker-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
package tools
import "testing"
func TestWorkerPool(t *testing.T) {
t.Run("should Not create a worker if number of workers to create is less than one", func(t *testing.T) {
numberOfWorkers := 0
_, err := NewWorker(uint8(numberOfWorkers))
if err == nil {
t.Fatalf("should not have createe an instance of the worker %v", err)
}
})
t.Run("should Acquire an worker and successfully release it back to the pool", func(t *testing.T) {
numberOfWorkers := 5
workerPool, err := NewWorker(uint8(numberOfWorkers))
if err != nil {
t.Fatalf("failed to create an instance of the worker %v", err)
}
releaseFirstWorker := workerPool.Acquire()
if workerPool.size() != numberOfWorkers {
t.Fatalf("expected %d but got %d", numberOfWorkers, workerPool.size())
}
availableWorkersBeforeRelease := numberOfWorkers - 1
if workerPool.available() != availableWorkersBeforeRelease {
t.Fatalf("expected %d but got %d", availableWorkersBeforeRelease, workerPool.available())
}
releaseFirstWorker()
availableWorkersAfterRelease := numberOfWorkers
if workerPool.available() != availableWorkersAfterRelease {
t.Fatalf("expected %d but got %d", availableWorkersAfterRelease, workerPool.available())
}
})
}