gpt4 book ai didi

ScalatestRouteTest 测试一起运行时超时但单独运行时正常

转载 作者:行者123 更新时间:2023-11-28 20:14:02 27 4
gpt4 key购买 nike

我有一个 akka-http 路由规范,其中包含两个测试。我可以单独运行这些中的每一个就好了——但是当我运行整个规范(包括两个测试)时,第二个失败了

Request was neither completed nor rejected within 30 seconds

谁知道这是为什么?

我不知道它是否相关,但无论我单独运行还是一起运行,每个测试似乎都会记录事件两次。 (有时重复的事件在不同的调度程序中,有时它们在同一个调度程序中。)我两次在日志消息上设置断点,所以我假设事件确实被调用了两次。同样,不确定它是否相关,但也许是一条线索。

我也在模拟依赖关系,但我认为这不是问题所在。

我测试的简化版本:

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)
}
}
}
}
}

在此先感谢您的帮助。

最佳答案

您的连接池可能存在问题,导致连接出现超时问题。

尝试显式传递超时,看看是否可行隐式 def 默认值(隐式系统:ActorSystem)= RouteTestTimeout(50.second)

关于ScalatestRouteTest 测试一起运行时超时但单独运行时正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44336281/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com