16
16
17
17
package com .github .mauricio .async .db .postgresql .pool
18
18
19
- import com .github .mauricio .async .db .pool .{SingleThreadedAsyncObjectPool , PoolExhaustedException , PoolConfiguration }
19
+ import com .github .mauricio .async .db .pool .{AsyncObjectPool , PoolConfiguration , PoolExhaustedException , SingleThreadedAsyncObjectPool }
20
20
import com .github .mauricio .async .db .postgresql .{DatabaseTestHelper , PostgreSQLConnection }
21
21
import java .nio .channels .ClosedChannelException
22
22
import java .util .concurrent .TimeUnit
23
+
23
24
import org .specs2 .mutable .Specification
24
- import scala .concurrent .Await
25
+
26
+ import scala .concurrent .{Await , Future }
25
27
import scala .concurrent .duration ._
26
28
import scala .language .postfixOps
27
29
import com .github .mauricio .async .db .exceptions .ConnectionStillRunningQueryException
@@ -47,23 +49,36 @@ class SingleThreadedAsyncObjectPoolSpec extends Specification with DatabaseTestH
47
49
pool =>
48
50
49
51
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)
51
53
52
54
pool.availables.size === 0
53
55
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
+
54
67
pool.queued.size === 3
55
68
56
69
executeTest(connection)
57
70
58
71
pool.giveBack(connection)
59
72
60
- promises.foreach {
73
+ val pools : List [ Future [ AsyncObjectPool [ PostgreSQLConnection ]]] = promises.map {
61
74
promise =>
62
75
val connection = Await .result(promise, Duration (5 , TimeUnit .SECONDS ))
63
76
executeTest(connection)
64
77
pool.giveBack(connection)
65
78
}
66
79
80
+ Await .ready(pools.last, Duration (5 , TimeUnit .SECONDS ))
81
+
67
82
pool.availables.size === 1
68
83
pool.inUse.size === 0
69
84
pool.queued.size === 0
0 commit comments