2017-06-02 56 views
1

我有一個對其中有兩個測試的akka​​-http路由的規範。我可以單獨就好運行其中的每個 - 但是當我運行完整的型號規格(包括測試),第二屆一個失敗ScalatestRouteTest在一起運行時測試超時,但單獨運行良好

請求既不完成也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

可能存在連接池問題,由於連接池出現超時問題。

嘗試明確地傳遞超時,看看它的作品了 implicit def default(implicit system: ActorSystem) = RouteTestTimeout(50.second)

+0

我將它設置爲30秒,這應該是足夠的。但是你可能對連接池是正確的;我會亂搞我的'application.conf'設置,看看我能否弄清楚發生了什麼。謝謝! –

相關問題