1616
1717package com .github .mauricio .async .db .postgresql .pool
1818
19- import com .github .mauricio .async .db .pool .{SingleThreadedAsyncObjectPool , PoolExhaustedException , PoolConfiguration }
19+ import com .github .mauricio .async .db .pool .{AsyncObjectPool , PoolConfiguration , PoolExhaustedException , SingleThreadedAsyncObjectPool }
2020import com .github .mauricio .async .db .postgresql .{DatabaseTestHelper , PostgreSQLConnection }
2121import java .nio .channels .ClosedChannelException
2222import java .util .concurrent .TimeUnit
23+
2324import org .specs2 .mutable .Specification
24- import scala .concurrent .Await
25+
26+ import scala .concurrent .{Await , Future }
2527import scala .concurrent .duration ._
2628import scala .language .postfixOps
2729import com .github .mauricio .async .db .exceptions .ConnectionStillRunningQueryException
@@ -47,23 +49,36 @@ class SingleThreadedAsyncObjectPoolSpec extends Specification with DatabaseTestH
4749 pool =>
4850
4951 val connection = get(pool)
50- val promises = List (pool.take, pool.take, pool.take)
52+ val promises : List [ Future [ PostgreSQLConnection ]] = List (pool.take, pool.take, pool.take)
5153
5254 pool.availables.size === 0
5355 pool.inUse.size === 1
56+ pool.queued.size must be_<= (3 )
57+
58+ /* pool.take call checkout that call this.mainPool.action,
59+ so enqueuePromise called in executorService,
60+ so there is no guaranties that all promises in queue at that moment
61+ */
62+ val deadline = 5 .seconds.fromNow
63+ while (pool.queued.size < 3 || deadline.hasTimeLeft) {
64+ Thread .sleep(50 )
65+ }
66+
5467 pool.queued.size === 3
5568
5669 executeTest(connection)
5770
5871 pool.giveBack(connection)
5972
60- promises.foreach {
73+ val pools : List [ Future [ AsyncObjectPool [ PostgreSQLConnection ]]] = promises.map {
6174 promise =>
6275 val connection = Await .result(promise, Duration (5 , TimeUnit .SECONDS ))
6376 executeTest(connection)
6477 pool.giveBack(connection)
6578 }
6679
80+ Await .ready(pools.last, Duration (5 , TimeUnit .SECONDS ))
81+
6782 pool.availables.size === 1
6883 pool.inUse.size === 0
6984 pool.queued.size === 0
0 commit comments