spray and actor non deterministic tests

382 views Asked by At

Helo, at the beginning i wold like to apologize for my english :)

akka=2.3.6
spray=1.3.2
scalatest=2.2.1

I encountered strange behavior of teting routes, which asks actors in handleWith directive,
I've route with handleWith directive

pathPrefix("firstPath") {
  pathEnd {
    get(complete("Hello from this api")) ~
    post(handleWith { (data: Data) =>{ println("receiving data") 
      (dataCalculator ? data).collect {
        case Success(_) =>
          Right(Created -> "")
        case throwable: MyInternalValidatationException =>
          Left(BadRequest -> s"""{"${throwable.subject}" : "${throwable.cause}"}""")
      }
    }})
  }
}


and simple actor wchich always responds when receive object Data and has own receive block wrapped in LoggingReceive, so I should see logs when message is receiving by actor

and i test it using (I think simple code)

class SampleStarngeTest extends WordSpec with ThisAppTestBase with OneInstancePerTest
with routeTestingSugar {
  val url = "/firstPath/"

  implicit val routeTestTimeout = RouteTestTimeout(5 seconds)

  def postTest(data: String) = Post(url).withJson(data) ~> routes

  "posting" should {
    "pass" when {
      "data is valid and comes from the identified user" in {
        postTest(correctData.copy(createdAt = System.currentTimeMillis()).asJson) ~> check {
          print(entity)
          status shouldBe Created
        }
      }
      "report is valid and comes from the anonymous" in {
        postTest(correctData.copy(createdAt = System.currentTimeMillis(), adid = "anonymous").asJson) ~> check {
          status shouldBe Created
        }
      }
    }
  }
}


and behavior:
When I run either all tests in package (using Intellij Idea 14 Ultimate) or sbt test I encounter the same results
one execution -> all tests pass
and next one -> not all pass, this which not pass I can see:
1. fail becouse Request was neither completed nor rejected within X seconds ( X up tp 60)
2. system console output from route from line post(handleWith { (data: Data) =>{ println("receiving data"), so code in handleWith was executed
3. ask timeout exception from route code, but not always (among failed tests)
4. no logs from actor LoggingReceive, so actor hasn't chance to respond
5. when I rerun teststhe results are even different from the previous

Is there problem with threading? or test modules, thread blocking inside libraries? or sth else? I've no idea why it isn't work :(

0

There are 0 answers