Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
Add test for max waiters and open timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Raghavendra Prabhu committed Mar 29, 2012
1 parent 7999187 commit a58d62f
Showing 1 changed file with 31 additions and 3 deletions.
Expand Up @@ -3,7 +3,8 @@ package com.twitter.querulous.unit
import org.specs.Specification
import java.sql.Connection
import java.util.concurrent.atomic._
import com.twitter.util.Future
import java.util.concurrent.RejectedExecutionException
import com.twitter.util.{Future, TimeoutException}
import com.twitter.querulous.database._
import com.twitter.querulous.query._
import com.twitter.querulous.async._
Expand Down Expand Up @@ -34,8 +35,9 @@ class BlockingDatabaseWrapperSpec extends Specification {
}

val numThreads = 2
val wrapper = new BlockingDatabaseWrapper(numThreads, Int.MaxValue, database)
// TODO: Test max waiters.
val maxWaiters = 1000
val wrapper = new BlockingDatabaseWrapper(numThreads, maxWaiters, database)

doBefore { database.reset() }

"withConnection should follow connection lifecycle" in {
Expand Down Expand Up @@ -88,6 +90,32 @@ class BlockingDatabaseWrapperSpec extends Specification {
println("Completed: "+ completed.size)
println("Cached: "+ database.openConns.get)


database.totalOpens.get mustEqual numThreads
database.openConns.get mustEqual numThreads
}

"withConnection should respect open timeout and max waiters" in {
val futures = for (i <- 1 to 10000) yield {
wrapper.withConnection { _ =>
Thread.sleep(database.openTimeout * 2)
"Done"
} handle {
case e: RejectedExecutionException => "Rejected"
case e: TimeoutException => "Timeout"
case _ => "Failed"
}
}

val results = Future.collect(futures).apply()
val completed = results partition { _ == "Done" } _1
val rejected = results partition { _ == "Rejected" } _1
val timedout = results partition { _ == "Timeout" } _1

completed.size mustEqual numThreads
timedout.size mustEqual maxWaiters
rejected.size mustEqual (results.size - completed.size - timedout.size)

database.totalOpens.get mustEqual numThreads
database.openConns.get mustEqual numThreads
}
Expand Down

0 comments on commit a58d62f

Please sign in to comment.