我的akka-http路由有一个规范,里面有两个测试。我可以单独运行每一个 - 但是当我运行整个规范(包括两个测试)时,第二个失败了
请求未在30秒内完成或拒绝
有谁知道这是为什么?
我不知道它是否相关,但无论我是单独运行还是一起运行,每次测试似乎都记录了两次事件。 (有时重复的事件在不同的调度程序中,有时它们在同一个调度程序中。)我在日志消息上点击两次断点,所以我认为事件实际上被调用了两次。再次,不确定它是否相关,但可能是一个线索。
我也嘲笑依赖关系,但我不认为这是问题所在。
我的测试版本黯然失色:
package com.mystuff
import akka.actor.ActorSystem
import com.mystuff._
import org.mockito.Mockito._
import akka.http.scaladsl.model.StatusCodes.{NotFound, OK}
import akka.http.scaladsl.server.Directives
import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest}
import org.scalatest.{BeforeAndAfterEach, FunSpec, Matchers}
import scala.concurrent.Future
import scala.concurrent.duration._
/**
* Created by bathalh on 6/2/17.
*/
class OAuthClentServiceASpec extends FunSpec with Matchers with BeforeAndAfterEach with Directives with ScalatestRouteTest
{
implicit def default(implicit system: ActorSystem) = RouteTestTimeout( 30 seconds )
private var mockDependencyFun: DependencyFun = _
private var testObject: MyService = _
override def beforeEach =
{
mockDependencyFun = mock(classOf[DependencyFun])
when( mockDependencyFun( anyString() ) ).thenReturn( Future successful "OK" )
testObject = new MyService() {
def getDepFunction = mockDependencyFun
}
}
describe( "OAuthClentService Application" )
{
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import com.mystuff.MyJsonProtocol._
describe( "Get a specific client" )
{
it( "Returns 404 with useful message when client does not exist" )
{
val clientId = "does not exist client"
Get( s"/clients/$clientId" ) ~> testObject.routes ~> check {
status should be (NotFound)
responseAs[ErrorResponse] should be (ErrorResponse(Set(s"Client not found: $clientId")))
}
}
it( "Returns client information when client exists" )
{
val clientId = "ValidClient"
Get( s"/clients/$clientId" ) ~> testObject.routes ~> check {
status should be (OK)
val clientInfo = responseAs[ClientResponse]
clientInfo.id should be (clientId)
}
}
}
}
}
提前感谢您的帮助。
答案 0 :(得分:0)
连接池可能存在问题,因为连接会导致超时问题。
尝试显式传递超时,看看它是否有效
implicit def default(implicit system: ActorSystem) = RouteTestTimeout(50.second)