1
- using ConcurrentUtilities, Test
1
+ using ConcurrentUtilities. Pools , Test
2
2
3
3
@testset " Pools" begin
4
+ pool_size = length∘ Pools. values
4
5
@testset " nonkeyed and pool basics" begin
5
6
pool = Pool {Int} (3 )
7
+ @test keytype (pool) === Nothing
8
+ @test valtype (pool) === Int
9
+
10
+ @test Pools. limit (pool) == 3
11
+ @test Pools. in_use (pool) == 0
12
+ @test Pools. in_pool (pool) == 0
13
+
6
14
# acquire an object from the pool
7
15
x1 = acquire (() -> 1 , pool)
8
16
# no existing objects in the pool, so our function was called to create a new one
9
17
@test x1 == 1
18
+ @test Pools. limit (pool) == 3
19
+ @test Pools. in_use (pool) == 1
20
+ @test Pools. in_pool (pool) == 0
21
+
10
22
# release back to the pool for reuse
11
23
release (pool, x1)
24
+ @test Pools. in_use (pool) == 0
25
+ @test Pools. in_pool (pool) == 1
26
+
12
27
# acquire another object from the pool
13
28
x1 = acquire (() -> 2 , pool)
14
29
# this time, the pool had an existing object, so our function was not called
15
30
@test x1 == 1
31
+ @test Pools. in_use (pool) == 1
32
+ @test Pools. in_pool (pool) == 0
33
+
16
34
# but now there are no objects to reuse again, so the next acquire will call our function
17
35
x2 = acquire (() -> 2 , pool)
18
36
@test x2 == 2
37
+ @test Pools. in_use (pool) == 2
38
+ @test Pools. in_pool (pool) == 0
39
+
19
40
x3 = acquire (() -> 3 , pool)
20
41
@test x3 == 3
21
- # the pool is now at capacity, so the next acquire will block until an object is released
42
+ @test Pools. in_use (pool) == 3
43
+ @test Pools. in_pool (pool) == 0
44
+
45
+ # the pool is now at `Pools.limit`, so the next acquire will block until an object is released
46
+ @test Pools. in_use (pool) == Pools. limit (pool)
22
47
tsk = @async acquire (() -> 4 , pool; forcenew= true )
23
48
yield ()
24
49
@test ! istaskdone (tsk)
@@ -28,60 +53,110 @@ using ConcurrentUtilities, Test
28
53
x1 = fetch (tsk)
29
54
# even though we released 1 for reuse, we passed forcenew, so our function was called to create new
30
55
@test x1 == 4
56
+ @test Pools. in_use (pool) == 3
57
+ @test Pools. in_pool (pool) == 1
58
+
31
59
# error to try and provide a key to a non-keyed pool
32
60
@test_throws ArgumentError acquire (() -> 1 , pool, 1 )
61
+
33
62
# release objects back to the pool
34
63
release (pool, x1)
35
64
release (pool, x2)
36
65
release (pool, x3)
66
+ @test Pools. in_use (pool) == 0
67
+ @test Pools. in_pool (pool) == 4
68
+
37
69
# acquire an object, but checking isvalid
38
70
x1 = acquire (() -> 5 , pool; isvalid= x -> x == 1 )
39
71
@test x1 == 1
72
+ @test Pools. in_use (pool) == 1
73
+
40
74
# no valid objects, so our function was called to create a new one
41
75
x2 = acquire (() -> 6 , pool; isvalid= x -> x == 1 )
42
76
@test x2 == 6
43
- # we have one slot left in the pool, we now throw while creating new
77
+ @test Pools. in_use (pool) == 2
78
+
79
+ # we have one permit left, we now throw while creating a new object
44
80
# and we want to test that the permit isn't permanently lost for the pool
45
81
@test_throws ErrorException acquire (() -> error (" oops" ), pool; forcenew= true )
82
+ @test Pools. in_use (pool) == 2
83
+
46
84
# we can still acquire a new object
47
85
x3 = acquire (() -> 7 , pool; forcenew= true )
48
86
@test x3 == 7
87
+ @test Pools. in_use (pool) == 3
88
+
49
89
# release objects back to the pool
90
+ drain! (pool)
50
91
release (pool, x1)
51
92
release (pool, x2)
52
93
release (pool, x3)
94
+ @test Pools. in_use (pool) == 0
95
+ @test Pools. in_pool (pool) == 3
96
+
53
97
# try to do an invalid release
54
98
@test_throws ArgumentError release (pool, 10 )
99
+
55
100
# test that the invalid release didn't push the object to our pool for reuse
56
101
x1 = acquire (() -> 8 , pool)
57
102
@test x1 == 7
103
+ @test Pools. in_use (pool) == 1
104
+ @test Pools. in_pool (pool) == 2
58
105
# calling drain! removes all objects for reuse
59
106
drain! (pool)
107
+ @test Pools. in_use (pool) == 1
108
+ @test Pools. in_pool (pool) == 0
109
+
60
110
x2 = acquire (() -> 9 , pool)
61
111
@test x2 == 9
112
+ @test Pools. in_use (pool) == 2
113
+ @test Pools. in_pool (pool) == 0
62
114
end
63
115
64
116
@testset " keyed pool" begin
65
117
# now test a keyed pool
66
118
pool = Pool {String, Int} (3 )
119
+ @test keytype (pool) === String
120
+ @test valtype (pool) === Int
121
+
122
+ @test Pools. limit (pool) == 3
123
+ @test Pools. in_use (pool) == 0
124
+ @test Pools. in_pool (pool) == 0
125
+
67
126
# acquire an object from the pool
68
127
x1 = acquire (() -> 1 , pool, " a" )
69
128
# no existing objects in the pool, so our function was called to create a new one
70
129
@test x1 == 1
130
+ @test Pools. in_use (pool) == 1
131
+ @test Pools. in_pool (pool) == 0
132
+
71
133
# release back to the pool for reuse
72
134
release (pool, " a" , x1)
135
+ @test Pools. in_use (pool) == 0
136
+ @test Pools. in_pool (pool) == 1
137
+
73
138
# test for a different key
74
139
x2 = acquire (() -> 2 , pool, " b" )
75
140
# there's an existing object, but for a different key, so we don't reuse
76
141
@test x2 == 2
142
+ @test Pools. in_use (pool) == 1
143
+ @test Pools. in_pool (pool) == 1
144
+
77
145
# acquire another object from the pool
78
146
x1 = acquire (() -> 2 , pool, " a" )
79
147
# this time, the pool had an existing object, so our function was not called
80
148
@test x1 == 1
149
+ @test Pools. in_use (pool) == 2
150
+ @test Pools. in_pool (pool) == 0
151
+
81
152
x3 = acquire (() -> 3 , pool, " a" )
82
153
@test x3 == 3
154
+ @test Pools. in_use (pool) == 3
155
+ @test Pools. in_pool (pool) == 0
156
+
83
157
# the pool is now at capacity, so the next acquire will block until an object is released
84
158
# even though we've acquired using different keys, the capacity is shared across the pool
159
+ @test Pools. in_use (pool) == Pools. limit (pool)
85
160
tsk = @async acquire (() -> 4 , pool, " c" ; forcenew= true )
86
161
yield ()
87
162
@test ! istaskdone (tsk)
@@ -91,13 +166,27 @@ using ConcurrentUtilities, Test
91
166
x1 = fetch (tsk)
92
167
# even though we released 1 for reuse, we passed forcenew, so our function was called to create new
93
168
@test x1 == 4
169
+ @test Pools. in_use (pool) == 3
170
+ @test Pools. in_pool (pool) == 1
171
+
94
172
# error to try and provide an invalid key to a keyed pool
95
173
@test_throws ArgumentError acquire (() -> 1 , pool, 1 )
96
- # error to release an invalid key back to the pool
97
- @test_throws KeyError release (pool, " z" , 1 )
174
+ @test Pools. in_use (pool) == 3
175
+ @test Pools. in_pool (pool) == 1
176
+
98
177
# error to *not* provide a key to a keyed pool
99
178
@test_throws ArgumentError acquire (() -> 1 , pool)
179
+ @test Pools. in_use (pool) == 3
180
+ @test Pools. in_pool (pool) == 1
181
+
100
182
# error to *not* provide a key when releasing to a keyed pool
101
183
@test_throws ArgumentError release (pool)
184
+ @test Pools. in_use (pool) == 3
185
+ @test Pools. in_pool (pool) == 1
186
+
187
+ # error to release an invalid key back to the pool
188
+ @test_throws KeyError release (pool, " z" , 1 )
189
+ @test_broken Pools. in_use (pool) == 3
190
+ @test Pools. in_pool (pool) == 1
102
191
end
103
192
end
0 commit comments