Skip to content

Commit fe9041a

Browse files
committed
bugfix pool fail to validate item on return do not create new
also fixed tests of partitioned pool
1 parent 5fb4ad8 commit fe9041a

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

‎db-async-common/src/test/java/com/github/jasync/sql/db/pool/PartitionedAsyncObjectPoolSpec.kt‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,32 @@ import java.util.concurrent.atomic.AtomicInteger
1616

1717
class PartitionedAsyncObjectPoolSpec {
1818

19-
val config = PoolConfiguration(100, Long.MAX_VALUE, 100, 5000)
20-
val factory = PartitionedAsyncObjectPoolSpec.ForTestingObjectFactory()
19+
private val config = PoolConfiguration(100, Long.MAX_VALUE, 100, 5000)
20+
private val factory = PartitionedAsyncObjectPoolSpec.ForTestingObjectFactory()
2121

2222
private var tested = ActorBasedObjectPool<MyPooledObject>(factory, config, testItemsPeriodically = false)
2323

24-
val pool = tested
24+
private val pool = tested
2525
val maxObjects = config.maxObjects
2626
//val maxIdle = config.maxIdle / 2
2727
val maxQueueSize = config.maxQueueSize
2828

2929

3030

3131

32-
class ForTestingObjectFactory : ObjectFactory<MyPooledObject> {
32+
private class ForTestingObjectFactory : ObjectFactory<MyPooledObject> {
3333
val reject = HashSet<MyPooledObject>()
3434
var failCreate = false
3535
val current = AtomicInteger(0)
36+
val createdObjects = mutableListOf<MyPooledObject>()
3637

3738
override fun create(): CompletableFuture<MyPooledObject> =
3839
if (failCreate) {
39-
FP.failed(IllegalStateException(""))
40+
FP.failed(IllegalStateException("failed to create item (it is intentional)"))
4041
} else {
41-
FP.successful(MyPooledObject(current.incrementAndGet()))
42+
val created = MyPooledObject(current.incrementAndGet())
43+
createdObjects.add(created)
44+
FP.successful(created)
4245
}
4346

4447

@@ -188,12 +191,14 @@ class PartitionedAsyncObjectPoolSpec {
188191

189192
pool.giveBack(MyPooledObject(1)).get()
190193

191-
assertThat(taking.get()).isEqualTo(1)
194+
assertThat(taking.get()).isEqualTo(1.toPoolObject)
192195
await.untilCallTo { pool.usedItems.size } matches { it == maxObjects }
193196
assertThat(pool.waitingForItem.size).isEqualTo(0)
194197
assertThat(pool.availableItems.size).isEqualTo(0)
195198
}
196199

200+
private val Int.toPoolObject: MyPooledObject get() = MyPooledObject(this)
201+
197202
@Test
198203
fun `pool contents - after exceed maxObjects, before exceed maxQueueSize - "one take queued and receive one invalid item back"`() {
199204
takeAndWait(maxObjects)
@@ -204,7 +209,7 @@ class PartitionedAsyncObjectPoolSpec {
204209
pool.giveBack(MyPooledObject(1)).get()
205210
}
206211

207-
await.untilCallTo { pool.usedItems.size }.matches { it == maxObjects}
212+
await.untilCallTo { pool.usedItems.size }.matches { it == maxObjects }
208213
assertThat(pool.waitingForItem.size).isEqualTo(0)
209214
assertThat(pool.availableItems.size).isEqualTo(0)
210215
}
@@ -229,7 +234,7 @@ class PartitionedAsyncObjectPoolSpec {
229234

230235
pool.giveBack(MyPooledObject(10)).get()
231236

232-
assertThat((taking).get()).isEqualTo(10)
237+
assertThat((taking).get()).isEqualTo(10.toPoolObject)
233238
await.untilCallTo { pool.usedItems.size } matches { it == maxObjects }
234239
assertThat(pool.waitingForItem.size).isEqualTo(maxQueueSize - 1)
235240
assertThat(pool.availableItems.size).isEqualTo(0)
@@ -245,8 +250,8 @@ class PartitionedAsyncObjectPoolSpec {
245250
pool.giveBack(MyPooledObject(11)).get()
246251
}
247252

248-
assertThat(pool.usedItems.size).isEqualTo(maxObjects - 1)
249-
assertThat(pool.waitingForItem.size).isEqualTo(maxQueueSize)
253+
await.untilCallTo { pool.usedItems.size } matches { it == maxObjects }
254+
assertThat(pool.waitingForItem.size).isEqualTo(maxQueueSize - 1)
250255
assertThat(pool.availableItems.size).isEqualTo(0)
251256

252257
}
@@ -289,8 +294,8 @@ class PartitionedAsyncObjectPoolSpec {
289294
pool.giveBack(MyPooledObject(1)).get()
290295
}
291296

292-
assertThat(pool.usedItems.size).isEqualTo(maxObjects - 1)
293-
assertThat(pool.waitingForItem.size).isEqualTo(maxQueueSize)
297+
await.untilCallTo { pool.usedItems.size } matches { it == maxObjects }
298+
assertThat(pool.waitingForItem.size).isEqualTo(maxQueueSize - 1)
294299
assertThat(pool.availableItems.size).isEqualTo(0)
295300
}
296301

@@ -315,14 +320,14 @@ class PartitionedAsyncObjectPoolSpec {
315320
takeQueued(maxQueueSize)
316321

317322

318-
for (i in 1..maxQueueSize) {
323+
for (i in 1..maxObjects) {
319324
factory.reject += MyPooledObject(i)
320325
verifyException(ExecutionException::class.java, IllegalStateException::class.java) {
321326
pool.giveBack(MyPooledObject(i)).get()
322327
}
323328
}
324-
await.untilCallTo { pool.usedItems.size } matches { it == maxObjects - maxQueueSize }
325-
assertThat(pool.waitingForItem.size).isEqualTo(maxQueueSize)
329+
await.untilCallTo { pool.usedItems.size } matches { it == maxObjects }
330+
assertThat(pool.waitingForItem.size).isEqualTo(0)
326331
assertThat(pool.availableItems.size).isEqualTo(0)
327332
}
328333

@@ -385,7 +390,7 @@ class PartitionedAsyncObjectPoolSpec {
385390

386391
}
387392

388-
inline class MyPooledObject(val i: Int): PooledObject {
393+
private data class MyPooledObject(val i: Int): PooledObject {
389394
override val id: String get() = "$i"
390395
}
391396

0 commit comments

Comments
 (0)