1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
21
21
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
22
22
import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
23
- import static org .assertj .core .api .Assertions .fail ;
24
23
25
24
import java .util .ArrayList ;
26
25
import java .util .HashSet ;
36
35
/**
37
36
* @author Gary Russell
38
37
* @author Sergey Bogatyrev
38
+ * @author Artem Bilan
39
+ *
39
40
* @since 2.2
40
41
*
41
42
*/
42
43
public class SimplePoolTests {
43
44
44
45
@ Test
45
46
public void testReuseAndStale () {
46
- final Set <String > strings = new HashSet <String >();
47
+ final Set <String > strings = new HashSet <>();
47
48
final AtomicBoolean stale = new AtomicBoolean ();
48
49
SimplePool <String > pool = stringPool (2 , strings , stale );
49
50
String s1 = pool .getItem ();
@@ -62,7 +63,7 @@ public void testReuseAndStale() {
62
63
63
64
@ Test
64
65
public void testOverCommitAndResize () {
65
- final Set <String > strings = new HashSet <String >();
66
+ final Set <String > strings = new HashSet <>();
66
67
final AtomicBoolean stale = new AtomicBoolean ();
67
68
SimplePool <String > pool = stringPool (2 , strings , stale );
68
69
String s1 = pool .getItem ();
@@ -83,13 +84,9 @@ public void testOverCommitAndResize() {
83
84
assertThat (pool .getIdleCount ()).isEqualTo (0 );
84
85
assertThat (pool .getActiveCount ()).isEqualTo (2 );
85
86
assertThat (pool .getAllocatedCount ()).isEqualTo (2 );
86
- try {
87
- pool .getItem ();
88
- fail ("Expected exception" );
89
- }
90
- catch (PoolItemNotAvailableException e ) {
91
87
92
- }
88
+ assertThatExceptionOfType (PoolItemNotAvailableException .class )
89
+ .isThrownBy (pool ::getItem );
93
90
94
91
// resize up
95
92
pool .setPoolSize (4 );
@@ -131,7 +128,7 @@ public void testOverCommitAndResize() {
131
128
132
129
@ Test
133
130
public void testForeignObject () {
134
- final Set <String > strings = new HashSet <String >();
131
+ final Set <String > strings = new HashSet <>();
135
132
final AtomicBoolean stale = new AtomicBoolean ();
136
133
SimplePool <String > pool = stringPool (2 , strings , stale );
137
134
pool .getItem ();
@@ -140,7 +137,7 @@ public void testForeignObject() {
140
137
141
138
@ Test
142
139
public void testDoubleReturn () {
143
- final Set <String > strings = new HashSet <String >();
140
+ final Set <String > strings = new HashSet <>();
144
141
final AtomicBoolean stale = new AtomicBoolean ();
145
142
SimplePool <String > pool = stringPool (2 , strings , stale );
146
143
Semaphore permits = TestUtils .getPropertyValue (pool , "permits" , Semaphore .class );
@@ -168,7 +165,27 @@ public void testSizeUpdateIfNotAllocated() {
168
165
assertThat (allocatedItems ).hasSize (5 );
169
166
170
167
// no more items can be allocated (indirect check of permits)
171
- assertThatExceptionOfType (PoolItemNotAvailableException .class ).isThrownBy (() -> pool .getItem ());
168
+ assertThatExceptionOfType (PoolItemNotAvailableException .class )
169
+ .isThrownBy (pool ::getItem );
170
+ }
171
+
172
+ @ Test
173
+ public void testMaxValueSizeUpdateIfNotAllocated () {
174
+ SimplePool <String > pool = stringPool (0 , new HashSet <>(), new AtomicBoolean ());
175
+ pool .setWaitTimeout (0 );
176
+ pool .setPoolSize (5 );
177
+ assertThat (pool .getPoolSize ()).isEqualTo (5 );
178
+
179
+ // allocating all available items to check permits
180
+ Set <String > allocatedItems = new HashSet <>();
181
+ for (int i = 0 ; i < 5 ; i ++) {
182
+ allocatedItems .add (pool .getItem ());
183
+ }
184
+ assertThat (allocatedItems ).hasSize (5 );
185
+
186
+ // no more items can be allocated (indirect check of permits)
187
+ assertThatExceptionOfType (PoolItemNotAvailableException .class )
188
+ .isThrownBy (pool ::getItem );
172
189
}
173
190
174
191
@ Test
@@ -207,7 +224,54 @@ public void testSizeUpdateIfPartiallyAllocated() {
207
224
assertThat (pool .getActiveCount ()).isEqualTo (5 );
208
225
209
226
// no more items can be allocated (indirect check of permits)
210
- assertThatExceptionOfType (PoolItemNotAvailableException .class ).isThrownBy (() -> pool .getItem ());
227
+ assertThatExceptionOfType (PoolItemNotAvailableException .class )
228
+ .isThrownBy (pool ::getItem );
229
+ }
230
+
231
+ @ Test
232
+ public void testMaxValueSizeUpdateIfPartiallyAllocated () {
233
+ SimplePool <String > pool = stringPool (0 , new HashSet <>(), new AtomicBoolean ());
234
+ pool .setWaitTimeout (0 );
235
+
236
+ List <String > allocated = new ArrayList <>();
237
+ for (int i = 0 ; i < 10 ; i ++) {
238
+ allocated .add (pool .getItem ());
239
+ }
240
+
241
+ // release only 2 items
242
+ for (int i = 0 ; i < 2 ; i ++) {
243
+ pool .releaseItem (allocated .get (i ));
244
+ }
245
+
246
+ // release only 2 items
247
+ for (int i = 0 ; i < 2 ; i ++) {
248
+ pool .releaseItem (allocated .get (i ));
249
+ }
250
+
251
+ // trying to reduce pool size
252
+ pool .setPoolSize (5 );
253
+
254
+ // at this moment the actual pool size can be reduced only partially, because
255
+ // only 2 items have been released, so 8 items are in use
256
+ assertThat (pool .getPoolSize ()).isEqualTo (8 );
257
+ assertThat (pool .getAllocatedCount ()).isEqualTo (8 );
258
+ assertThat (pool .getIdleCount ()).isEqualTo (0 );
259
+ assertThat (pool .getActiveCount ()).isEqualTo (8 );
260
+
261
+ // releasing 3 items
262
+ for (int i = 2 ; i < 5 ; i ++) {
263
+ pool .releaseItem (allocated .get (i ));
264
+ }
265
+
266
+ // now pool size should be reduced
267
+ assertThat (pool .getPoolSize ()).isEqualTo (5 );
268
+ assertThat (pool .getAllocatedCount ()).isEqualTo (5 );
269
+ assertThat (pool .getIdleCount ()).isEqualTo (0 );
270
+ assertThat (pool .getActiveCount ()).isEqualTo (5 );
271
+
272
+ // no more items can be allocated (indirect check of permits)
273
+ assertThatExceptionOfType (PoolItemNotAvailableException .class )
274
+ .isThrownBy (pool ::getItem );
211
275
}
212
276
213
277
@ Test
@@ -240,7 +304,8 @@ public void testSizeUpdateIfFullyAllocated() {
240
304
assertThat (pool .getActiveCount ()).isEqualTo (5 );
241
305
242
306
// no more items can be allocated (indirect check of permits)
243
- assertThatExceptionOfType (PoolItemNotAvailableException .class ).isThrownBy (() -> pool .getItem ());
307
+ assertThatExceptionOfType (PoolItemNotAvailableException .class )
308
+ .isThrownBy (pool ::getItem );
244
309
245
310
// releasing remaining items
246
311
for (int i = 5 ; i < 10 ; i ++) {
@@ -266,10 +331,9 @@ void testClose() {
266
331
assertThatIllegalStateException ().isThrownBy (pool ::getItem );
267
332
}
268
333
269
- private SimplePool <String > stringPool (int size , final Set <String > strings ,
270
- final AtomicBoolean stale ) {
334
+ private SimplePool <String > stringPool (int size , Set <String > strings , AtomicBoolean stale ) {
335
+ return new SimplePool < String >( size , new SimplePool . PoolItemCallback < String >( ) {
271
336
272
- SimplePool <String > pool = new SimplePool <String >(size , new SimplePool .PoolItemCallback <String >() {
273
337
private int i ;
274
338
275
339
@ Override
@@ -293,7 +357,6 @@ public void removedFromPool(String item) {
293
357
}
294
358
295
359
});
296
- return pool ;
297
360
}
298
361
299
362
}
0 commit comments