Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix off-by-one error in maxWaitQueueLimit #2231

Merged
merged 2 commits into from Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/src/main/scala/org/http4s/client/PoolManager.scala
Expand Up @@ -111,7 +111,7 @@ private final class PoolManager[F[_], A <: Connection[F]](
}

private def addToWaitQueue(key: RequestKey, callback: Callback[NextConnection]): Unit =
if (waitQueue.length <= maxWaitQueueLimit) {
if (waitQueue.length < maxWaitQueueLimit) {
waitQueue.enqueue(Waiting(key, callback, Instant.now()))
} else {
logger.error(s"Max wait length reached, not scheduling.")
Expand Down
82 changes: 82 additions & 0 deletions client/src/test/scala/org/http4s/client/PoolManagerSpec.scala
@@ -0,0 +1,82 @@
package org.http4s
package client

import cats.effect._
import fs2.Stream
import scala.concurrent.duration._

class PoolManagerSpec(name: String) extends Http4sSpec {
val key = RequestKey(Uri.Scheme.http, Uri.Authority(host = Uri.IPv4("127.0.0.1")))
class TestConnection extends Connection[IO] {
def runRequest(req: Request[IO]) = IO.never
def isClosed = false
def isRecyclable = true
def requestKey = key
def shutdown() = ()
}

def mkPool(
maxTotal: Int = 1,
maxWaitQueueLimit: Int = 2
) =
IO(
ConnectionManager.pool(
builder = _ => IO(new TestConnection()),
maxTotal = maxTotal,
maxWaitQueueLimit = maxWaitQueueLimit,
maxConnectionsPerRequestKey = _ => 5,
responseHeaderTimeout = Duration.Inf,
requestTimeout = Duration.Inf,
executionContext = testExecutionContext
))

"A pool manager" should {
"wait up to maxWaitQueueLimit" in {
(for {
pool <- mkPool(maxTotal = 1, maxWaitQueueLimit = 2)
_ <- pool.borrow(key)
att <- Stream(Stream.eval(pool.borrow(key))).repeat
.take(2)
.covary[IO]
.joinUnbounded
.compile
.toList
.attempt
} yield att).unsafeRunTimed(2.seconds) must_== None
}

"throw at maxWaitQueueLimit" in {
(for {
pool <- mkPool(maxTotal = 1, maxWaitQueueLimit = 2)
_ <- pool.borrow(key)
att <- Stream(Stream.eval(pool.borrow(key))).repeat
.take(3)
.covary[IO]
.joinUnbounded
.compile
.toList
.attempt
} yield att).unsafeRunTimed(2.seconds) must_== Some(Left(WaitQueueFullFailure()))
}

"wake up a waiting connection on release" in {
(for {
pool <- mkPool(maxTotal = 1, maxWaitQueueLimit = 1)
conn <- pool.borrow(key)
fiber <- pool.borrow(key).start // Should be one waiting
_ <- pool.release(conn.connection)
_ <- fiber.join
} yield ()).unsafeRunTimed(2.seconds) must_== Some(())
}

"wake up a waiting connection on invalidate" in {
(for {
pool <- mkPool(maxTotal = 1, maxWaitQueueLimit = 1)
conn <- pool.borrow(key)
fiber <- pool.borrow(key).start // Should be one waiting
_ <- pool.invalidate(conn.connection)
_ <- fiber.join
} yield ()).unsafeRunTimed(2.seconds) must_== Some(())
}
}
}