8000 Merge pull request #202 from golem131/fix_tests · kimmking/postgresql-async@d6c103e · GitHub
[go: up one dir, main page]

Skip to content

Commit d6c103e

Browse files
authored
Merge pull request mauricio#202 from golem131/fix_tests
Wait until connection return to pool
2 parents c597318 + 4b6c380 commit d6c103e

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

postgresql-async/src/test/scala/com/github/mauricio/async/db/postgresql/pool/SingleThreadedAsyncObjectPoolSpec.scala

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
package 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}
2020
import com.github.mauricio.async.db.postgresql.{DatabaseTestHelper, PostgreSQLConnection}
2121
import java.nio.channels.ClosedChannelException
2222
import java.util.concurrent.TimeUnit
23+
2324
import org.specs2.mutable.Specification
24-
import scala.concurrent.Await
25+
26+
import scala.concurrent.{Await, Future}
2527
import scala.concurrent.duration._
2628
import scala.language.postfixOps
2729
import 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

Comments
 (0)
0