-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_test.go
149 lines (128 loc) · 3.27 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
/*
* Copyright (c) 2019.
*
* This file is part of gopool.
*
* gopool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gopool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gopool. If not, see <https://www.gnu.org/licenses/>.
*/
package gopool
import (
"fmt"
"log"
"testing"
"time"
)
func TestPoolSize(t *testing.T) {
pool := NewPool(10, func(in interface{}) (interface{}, error) { return "hello " + in.(string) + "!", nil })
if exp, act := 10, len(pool.workers); exp != act {
t.Errorf("Wrong size of pool: %v != %v", act, exp)
}
pool.SetSize(10)
if exp, act := 10, pool.GetSize(); exp != act {
t.Errorf("Wrong size of pool: %v != %v", act, exp)
}
pool.SetSize(9)
if exp, act := 9, pool.GetSize(); exp != act {
t.Errorf("Wrong size of pool: %v != %v", act, exp)
}
pool.SetSize(0)
if exp, act := 0, pool.GetSize(); exp != act {
t.Errorf("Wrong size of pool: %v != %v", act, exp)
}
pool.SetSize(10)
if exp, act := 10, pool.GetSize(); exp != act {
t.Errorf("Wrong size of pool: %v != %v", act, exp)
}
// Finally, make sure we still have actual active workers
r, _ := pool.Execute("Orc")
if exp, act := "hello Orc!", r.(string); exp != act {
t.Errorf("Wrong result: %v != %v", act, exp)
}
pool.Close()
if exp, act := 0, pool.GetSize(); exp != act {
t.Errorf("Wrong size of pool: %v != %v", act, exp)
}
}
func TestPoolFunc(t *testing.T) {
pool := NewPool(10, func(in interface{}) (interface{}, error) {
intVal := in.(int)
return intVal * 2, nil
})
defer pool.Close()
for i := 0; i < 10; i++ {
ret, _ := pool.Execute(10)
if exp, act := 20, ret.(int); exp != act {
t.Errorf("Wrong result: %v != %v", act, exp)
}
}
}
type Operation struct {
a int
b int
}
func Execute(op Operation) int {
time.Sleep(3000 * time.Millisecond)
return op.a + op.b
}
//func Test_Single_Pool(t *testing.T) {
//
// pool := NewPool(1, func(in interface{}) interface{} {
// return Execute(in.(Operation))
// })
//
// r := pool.Execute(Operation{1,1})
//
// log.Println(fmt.Sprintf("Result => %v", r))
//
// if pool.GetQueuedJobs() != 0 {
// panic("Invalid jobs in the queue")
// }
// pool.Close()
//
//}
//
//
func Test_Basic_Pool(t *testing.T) {
pool := NewPool(5, func(in interface{}) (interface{}, error) {
return Execute(in.(Operation)), nil
})
ops := []interface{}{Operation{1, 1},
Operation{1, 2},
Operation{1, 3},
Operation{1, 43},
Operation{1, 13},
Operation{1, 23},
Operation{1, 33},
Operation{1, 4},
Operation{1, 5},
Operation{1, 6},
Operation{1, 7},
Operation{1, 14},
Operation{1, 15},
Operation{1, 16},
Operation{1, 17},
Operation{1, 8}}
output, err := pool.ExecuteM(ops)
if err != nil {
t.Fatal(err)
}
//wg.Wait()
for r := range output {
log.Println(fmt.Sprintf("Result => %v", r))
}
if pool.GetQueuedJobs() != 0 {
panic("Invalid jobs in the queue")
}
pool.Close()
}