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

Route test never finishes waiting on CountDownLatch #1615

Closed
asarkar opened this issue Dec 11, 2017 · 7 comments

Comments

@asarkar
Copy link

@asarkar asarkar commented Dec 11, 2017

c.f. https://stackoverflow.com/questions/47636871/why-does-this-akka-http-route-test-never-complete-successfully

Some debugging shows that the following code in RouteTestResultComponent is causing the problem

private[testkit] def handleResult(rr: RouteResult): Unit =
 synchronized {
   if (result.isEmpty) {
     result = rr match {
       case RouteResult.Complete(response)   ⇒ Some(Right(response))
       case RouteResult.Rejected(rejections) ⇒ Some(Left(RejectionHandler.applyTransformations(rejections)))
     }
     latch.countDown()
   } else failTest("Route completed/rejected more than once")
 }

private[testkit] def awaitResult: this.type = {
 latch.await(timeout.toMillis, MILLISECONDS)
 this
}

I'm not sure yet why, but the latch is never decremented causing the request to eventually time out within awaitResult

@ktoso

This comment has been minimized.

Copy link
Member

@ktoso ktoso commented Dec 11, 2017

Please refrain from cross posting like this. Issues are for issues and not general questions.
The question was answered on stack overflow already: https://stackoverflow.com/questions/47636871/why-does-this-akka-http-route-test-never-complete-successfully

You should use probes for each of your tests which were explained in the Stack Overflow answewr

@ktoso ktoso closed this Dec 11, 2017
@asarkar

This comment has been minimized.

Copy link
Author

@asarkar asarkar commented Dec 11, 2017

@ktoso If you looked at the SO thread, you'd see that I clearly mentioned probes are not the issue. Just because someone made an attempt doesn't mean it's the right answer. And as far as cross-posting goes, I did put the SO link, didn't I?
I believe this is an issue, hence I'm raising it here. Can you disprove that?

@jonas

This comment has been minimized.

Copy link
Member

@jonas jonas commented Dec 11, 2017

I don't know if it is related but when I run the complete Akka HTTP test suite I never get back the sbt prompt. I'm assuming the tests hang somewhere but I didn't investigate.

@asarkar

This comment has been minimized.

Copy link
Author

@asarkar asarkar commented Dec 11, 2017

@jonas I didn’t find any tests for completeWith directive. Do you’ve any?

@jonas

This comment has been minimized.

Copy link
Member

@jonas jonas commented Dec 12, 2017

There are several test in the documentation examples under docs/src/test.

@asarkar

This comment has been minimized.

Copy link
Author

@asarkar asarkar commented Dec 12, 2017

I searched the whole codebase, and there isn't a single instance of completeWith used with an Actor. Not in main code, or in tests.

@asarkar

This comment has been minimized.

Copy link
Author

@asarkar asarkar commented Dec 12, 2017

@jonas I got it working myself and have a theory that I'll appreciate if you can comment on.

"Routes" should "respond to get apps request" in {
  testProbe.setAutoPilot((_: ActorRef, msg: Any) => {
    msg match {
      case GetAppsRequest(callback) => callback(List("test")); TestActor.KeepRunning
      case _ => TestActor.NoAutoPilot
    }
  })

  Get("/apps") ~> handler ~> check {
    entityAs[List[String]] should contain("test")
  }
}

My theory is that putting TestProbe inside check hangs forever probably because it creates a deadlock; test waits for callback to be called, and callback isn't called until the body is executed.

Using autopilot sets up an "expectation" that can be fulfilled later thus avoiding the deadlock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.