2014-04-03 4 views
0

Play Framework 2.1에서 WebSocket 코드를 테스트하고 있습니다. 내 접근 방식은 실제 웹 소켓에 사용되는 iterator/enumerator 쌍을 가져 와서 데이터를 밀어 넣고 데이터를 꺼내는 테스트뿐입니다.Play 2.1 : 열거 자의 결과 기다림

불행히도, 난 그냥 열거 자의 데이터를 가져 오는 방법을 알아낼 수 없습니다. 지금 내 코드는 대략 다음과 같습니다

val (in, out) = createClient(FakeRequest("GET", "/myendpoint")) 

in.feed(Input.El("My input here")) 
in.feed(Input.EOF) 

//no idea how to get data from "out" 

를 지금까지 내가 말할 수있는 열거에서 데이터를 얻을 수있는 유일한 방법은 iteratee하는 것입니다. 하지만 열거 자의 전체 목록을 얻을 때까지 기다리는 방법을 알 수는 없습니다. 내가 원하는 것은 List[String]이고 Future[Iteratee[A,String]] 또는 Expectable[Iteratee[String]]이 아니거나 Iteratee[String]입니다. 설명서는 기껏해야 혼란 스럽습니다.

어떻게하면됩니까?

+0

당신은'createClient' 방법의 (간체) 내용을 추가 할 수 있습니다 다음 Enumerator 생산 값에 Iteratee 결과를 먹이>Enumerator 체인 - Iteratee에 대한

더 정교한 예? – EECOLOR

답변

-1

이 같은 Enumerator을 소비 할 수 있습니다 : 당신이 Future 두 번 때문에 EnumeratorIteratee 제어를 각각 생산과 가치 소비의 속도를 기다리고 할 필요가

val out = Enumerator("one", "two") 

    val consumer = Iteratee.getChunks[String] 

    val appliedEnumeratorFuture = out.apply(consumer) 

    val appliedEnumerator = Await.result(appliedEnumeratorFuture, 1.seconds) 

    val result = Await.result(appliedEnumerator.run, 1.seconds) 

    println(result) // List("one", "two") 

참고.

// create an enumerator to which messages can be pushed 
    // using a channel 
    val (out, channel) = Concurrent.broadcast[String] 

    // create the input stream. When it receives an string, it 
    // will push the info into the channel 
    val in = 
    Iteratee.foreach[String] { s => 
     channel.push(s) 
    }.map(_ => channel.eofAndEnd()) 

    // Instead of using the complex `feed` method, we just create 
    // an enumerator that we can use to feed the input stream 
    val producer = Enumerator("one", "two").andThen(Enumerator.eof) 

    // Apply the input stream to the producer (feed the input stream) 
    val producedElementsFuture = producer.apply(in) 

    // Create a consumer for the output stream 
    val consumer = Iteratee.getChunks[String] 

    // Apply the consumer to the output stream (consume the output stream) 
    val consumedOutputStreamFuture = out.apply(consumer) 

    // Await the construction of the input stream chain 
    Await.result(producedElementsFuture, 1.second) 
    // Await the construction of the output stream chain 
    val consumedOutputStream = Await.result(consumedOutputStreamFuture, 1.second) 
    // Await consuming the output stream 
    val result = Await.result(consumedOutputStream.run, 1.second) 

    println(result) // List("one", "two") 
관련 문제