Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何真正测试akka流?_Scala_Akka_Akka Stream_Scalatest - Fatal编程技术网

Scala 如何真正测试akka流?

Scala 如何真正测试akka流?,scala,akka,akka-stream,scalatest,Scala,Akka,Akka Stream,Scalatest,我正在使用并希望根据收到的消息进行测试。我选择了这个框架,我知道测试是异步进行的 websocket客户端如下所示: import akka.{Done} import akka.http.scaladsl.Http import akka.stream.scaladsl._ import akka.http.scaladsl.model.ws._ import io.circe.syntax._ import scala.concurrent.Future object WsClient

我正在使用并希望根据收到的消息进行测试。我选择了这个框架,我知道测试是异步进行的

websocket客户端如下所示:

import akka.{Done}
import akka.http.scaladsl.Http
import akka.stream.scaladsl._
import akka.http.scaladsl.model.ws._
import io.circe.syntax._

import scala.concurrent.Future


object WsClient {

  import Trigger._

  private val convertJson: PreMsg => String = msg =>
    msg.asJson.noSpaces

  val send: PreMsg => (String => Unit) => RunnableGraph[Future[Done]] = msg => fn =>
    Source.single(convertJson(msg))
      .map(TextMessage(_))
      .via(Http().webSocketClientFlow(WebSocketRequest(s"ws://{$Config.host}:{$Config.port}/saprs")))
      .map(_.asTextMessage.getStrictText)
      .toMat(Sink.foreach(fn))(Keep.right)


}  
以及测试:

  feature("Process incoming messages") {
    info("As a user, I want that incoming messages is going to process appropriately.")
    info("A message should contain the following properties: `sap_id`, `sap_event`, `payload`")

    scenario("Message is not intended for the server") {
      Given("A message with `sap_id:unknown`")
      val msg = PreMsg("unknown", "unvalid", "{}")
      When("the message gets validated")
      val ws = WsClient.send(msg)
      Then("it should has the `status: REJECT` in the response content")
      ws { msg =>
        //Would like test against the msg here
      }.run()
        .map(_ => assert(1 == 1))

    }  
我想测试一下
msg
的内容,但我不知道怎么做

我跟着

他们使用WebSocketClient作为助手,请参见

然后,测试如下所示:

Helpers.running(TestServer(port, app)) {
    val myPublicAddress = s"localhost:$port"
    val serverURL = s"ws://$myPublicAddress/ws"

    val asyncHttpClient: AsyncHttpClient = client.underlying[AsyncHttpClient]
    val webSocketClient = new WebSocketClient(asyncHttpClient)
    val queue = new ArrayBlockingQueue[String](10)
    val origin = serverURL
    val consumer: Consumer[String] = new Consumer[String] {
      override def accept(message: String): Unit = queue.put(message)
    }
    val listener = new WebSocketClient.LoggingListener(consumer)
    val completionStage = webSocketClient.call(serverURL, origin, listener)
    val f = FutureConverters.toScala(completionStage)

    // Test we can get good output from the websocket
    whenReady(f, timeout = Timeout(1.second)) { webSocket =>
      val condition: Callable[java.lang.Boolean] = new Callable[java.lang.Boolean] {
        override def call(): java.lang.Boolean = webSocket.isOpen && queue.peek() != null
      }
      await().until(condition)
      val input: String = queue.take()
      val json:JsValue = Json.parse(input)
      val symbol = (json \ "symbol").as[String]
      List(symbol) must contain oneOf("AAPL", "GOOG", "ORCL")
    }
  }
}
请看这里: