在下面的测试中,我尝试模拟超时,然后发送一个正常的请求。然而,我得到了spray.can.Http$ConnectionException:过早的连接关闭(服务器似乎不支持请求流水线)
class SprayCanTest extends ModuleTestKit("/SprayCanTest.conf") with FlatSpecLike with Matchers {
import system.dispatcher
var app = Actor.noSender
protected override def beforeAll(): Unit = {
super.beforeAll()
app = system.actorOf(Props(new MockServer))
}
override protected def afterAll(): Unit = {
system.stop(app)
super.afterAll()
}
"response time out" should "work" in {
val setup = Http.HostConnectorSetup("localhost", 9101, false)
connect(setup).onComplete {
case Success(conn) => {
conn ! HttpRequest(HttpMethods.GET, "/timeout")
}
}
expectMsgPF() {
case Status.Failure(t) =>
t shouldBe a[RequestTimeoutException]
}
}
"normal http response" should "work" in {
//Thread.sleep(5000)
val setup = Http.HostConnectorSetup("localhost", 9101, false)
connect(setup).onComplete {
case Success(conn) => {
conn ! HttpRequest(HttpMethods.GET, "/hello")
}
}
expectMsgPF() {
case HttpResponse(status, entity, _, _) =>
status should be(StatusCodes.OK)
entity should be(HttpEntity("Helloworld"))
}
}
def connect(setup: HostConnectorSetup)(implicit system: ActorSystem) = {
// for the actor 'asks'
import system.dispatcher
implicit val timeout: Timeout = Timeout(1 second)
(IO(Http) ? setup) map {
case Http.HostConnectorInfo(connector, _) => connector
}
}
class MockServer extends Actor {
//implicit val timeout: Timeout = 1.second
implicit val system = context.system
// Register connection service
IO(Http) ! Http.Bind(self, interface = "localhost", port = 9101)
def receive: Actor.Receive = {
case _: Http.Connected => sender ! Http.Register(self)
case HttpRequest(GET, Uri.Path("/timeout"), _, _, _) => {
Thread.sleep(3000)
sender ! HttpResponse(entity = HttpEntity("ok"))
}
case HttpRequest(GET, Uri.Path("/hello"), _, _, _) => {
sender ! HttpResponse(entity = HttpEntity("Helloworld"))
}
}
}
}我的测试配置:
spray {
can {
client {
response-chunk-aggregation-limit = 0
connecting-timeout = 1s
request-timeout = 1s
}
host-connector {
max-retries = 0
}
}
}我发现在这两种情况下,"conn“对象是相同的。所以我想当RequestTimeoutException发生时,喷射将康涅狄格器放回池(默认情况下是4?)下一种情况将使用相同的康涅狄格,但此时,该康涅狄格将保持活动状态,因此服务器将将其视为块请求。
如果我在第二种情况下睡一觉,它就会过去。因此,我想我必须关闭康涅狄格当得到RequestTimeoutException,并确保第二个案例使用一个新的新连接,对吗?
我该怎么办?有什么配置吗?
谢谢
里昂
发布于 2014-09-18 07:30:12
您不应该阻止一个Actor (您的MockServer)。当它被阻塞时,它无法响应任何消息。您可以将Thread.sleep和响应包装在未来。或者更好:使用阿克卡调度器。请确保将发送方分配给val,因为当您异步响应请求时,它可能会更改。这应该能起作用:
val savedSender = sender()
context.system.scheduler.scheduleOnce(3 seconds){
savedSender ! HttpResponse(entity = HttpEntity("ok"))
}https://stackoverflow.com/questions/25860344
复制相似问题