1- using ConcurrentUtilities, Test
1+ using ConcurrentUtilities. Pools , Test
22
33@testset " Pools" begin
4+ pool_size = length∘ Pools. values
45 @testset " nonkeyed and pool basics" begin
56 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+
614 # acquire an object from the pool
715 x1 = acquire (() -> 1 , pool)
816 # no existing objects in the pool, so our function was called to create a new one
917 @test x1 == 1
18+ @test Pools. limit (pool) == 3
19+ @test Pools. in_use (pool) == 1
20+ @test Pools. in_pool (pool) == 0
21+
1022 # release back to the pool for reuse
1123 release (pool, x1)
24+ @test Pools. in_use (pool) == 0
25+ @test Pools. in_pool (pool) == 1
26+
1227 # acquire another object from the pool
1328 x1 = acquire (() -> 2 , pool)
1429 # this time, the pool had an existing object, so our function was not called
1530 @test x1 == 1
31+ @test Pools. in_use (pool) == 1
32+ @test Pools. in_pool (pool) == 0
33+
1634 # but now there are no objects to reuse again, so the next acquire will call our function
1735 x2 = acquire (() -> 2 , pool)
1836 @test x2 == 2
37+ @test Pools. in_use (pool) == 2
38+ @test Pools. in_pool (pool) == 0
39+
1940 x3 = acquire (() -> 3 , pool)
2041 @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)
2247 tsk = @async acquire (() -> 4 , pool; forcenew= true )
2348 yield ()
2449 @test ! istaskdone (tsk)
@@ -28,60 +53,110 @@ using ConcurrentUtilities, Test
2853 x1 = fetch (tsk)
2954 # even though we released 1 for reuse, we passed forcenew, so our function was called to create new
3055 @test x1 == 4
56+ @test Pools. in_use (pool) == 3
57+ @test Pools. in_pool (pool) == 1
58+
3159 # error to try and provide a key to a non-keyed pool
3260 @test_throws ArgumentError acquire (() -> 1 , pool, 1 )
61+
3362 # release objects back to the pool
3463 release (pool, x1)
3564 release (pool, x2)
3665 release (pool, x3)
66+ @test Pools. in_use (pool) == 0
67+ @test Pools. in_pool (pool) == 4
68+
3769 # acquire an object, but checking isvalid
3870 x1 = acquire (() -> 5 , pool; isvalid= x -> x == 1 )
3971 @test x1 == 1
72+ @test Pools. in_use (pool) == 1
73+
4074 # no valid objects, so our function was called to create a new one
4175 x2 = acquire (() -> 6 , pool; isvalid= x -> x == 1 )
4276 @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
4480 # and we want to test that the permit isn't permanently lost for the pool
4581 @test_throws ErrorException acquire (() -> error (" oops" ), pool; forcenew= true )
82+ @test Pools. in_use (pool) == 2
83+
4684 # we can still acquire a new object
4785 x3 = acquire (() -> 7 , pool; forcenew= true )
4886 @test x3 == 7
87+ @test Pools. in_use (pool) == 3
88+
4989 # release objects back to the pool
90+ drain! (pool)
5091 release (pool, x1)
5192 release (pool, x2)
5293 release (pool, x3)
94+ @test Pools. in_use (pool) == 0
95+ @test Pools. in_pool (pool) == 3
96+
5397 # try to do an invalid release
5498 @test_throws ArgumentError release (pool, 10 )
99+
55100 # test that the invalid release didn't push the object to our pool for reuse
56101 x1 = acquire (() -> 8 , pool)
57102 @test x1 == 7
103+ @test Pools. in_use (pool) == 1
104+ @test Pools. in_pool (pool) == 2
58105 # calling drain! removes all objects for reuse
59106 drain! (pool)
107+ @test Pools. in_use (pool) == 1
108+ @test Pools. in_pool (pool) == 0
109+
60110 x2 = acquire (() -> 9 , pool)
61111 @test x2 == 9
112+ @test Pools. in_use (pool) == 2
113+ @test Pools. in_pool (pool) == 0
62114 end
63115
64116 @testset " keyed pool" begin
65117 # now test a keyed pool
66118 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+
67126 # acquire an object from the pool
68127 x1 = acquire (() -> 1 , pool, " a" )
69128 # no existing objects in the pool, so our function was called to create a new one
70129 @test x1 == 1
130+ @test Pools. in_use (pool) == 1
131+ @test Pools. in_pool (pool) == 0
132+
71133 # release back to the pool for reuse
72134 release (pool, " a" , x1)
135+ @test Pools. in_use (pool) == 0
136+ @test Pools. in_pool (pool) == 1
137+
73138 # test for a different key
74139 x2 = acquire (() -> 2 , pool, " b" )
75140 # there's an existing object, but for a different key, so we don't reuse
76141 @test x2 == 2
142+ @test Pools. in_use (pool) == 1
143+ @test Pools. in_pool (pool) == 1
144+
77145 # acquire another object from the pool
78146 x1 = acquire (() -> 2 , pool, " a" )
79147 # this time, the pool had an existing object, so our function was not called
80148 @test x1 == 1
149+ @test Pools. in_use (pool) == 2
150+ @test Pools. in_pool (pool) == 0
151+
81152 x3 = acquire (() -> 3 , pool, " a" )
82153 @test x3 == 3
154+ @test Pools. in_use (pool) == 3
155+ @test Pools. in_pool (pool) == 0
156+
83157 # the pool is now at capacity, so the next acquire will block until an object is released
84158 # even though we've acquired using different keys, the capacity is shared across the pool
159+ @test Pools. in_use (pool) == Pools. limit (pool)
85160 tsk = @async acquire (() -> 4 , pool, " c" ; forcenew= true )
86161 yield ()
87162 @test ! istaskdone (tsk)
@@ -91,13 +166,27 @@ using ConcurrentUtilities, Test
91166 x1 = fetch (tsk)
92167 # even though we released 1 for reuse, we passed forcenew, so our function was called to create new
93168 @test x1 == 4
169+ @test Pools. in_use (pool) == 3
170+ @test Pools. in_pool (pool) == 1
171+
94172 # error to try and provide an invalid key to a keyed pool
95173 @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+
98177 # error to *not* provide a key to a keyed pool
99178 @test_throws ArgumentError acquire (() -> 1 , pool)
179+ @test Pools. in_use (pool) == 3
180+ @test Pools. in_pool (pool) == 1
181+
100182 # error to *not* provide a key when releasing to a keyed pool
101183 @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
102191 end
103192end
0 commit comments