मान लें कि आप एक WebSocket पुस्तकालय है कि रिटर्न भविष्य है कि [Itearatee [JsValue, यूनिट], गणनाकार [JsValue]] अपने नियंत्रक का उपयोग करता है
trait WSLib {
def connect: Future[Itearatee[JsValue, Unit], Enumerator[JsValue]]
}
और आप इस पुस्तकालय का परीक्षण करना चाहते हैं।
यहाँ एक संदर्भ है आप का उपयोग कर सकते हैं:
trait WebSocketContext extends WithApplication {
val aSecond = FiniteDuration(1, TimeUnit.SECONDS)
case class Incoming(iteratee: Iteratee[JsValue, Unit]) {
def feed(message: JsValue) = {
iteratee.feed(Input.El(message))
}
def end(wait: Long = 100) = {
Thread.sleep(wait) //wait until all previous fed messages are handled
iteratee.feed(Input.EOF)
}
}
case class OutGoing(enum: Enumerator[JsValue]) {
val messages = enum(Iteratee.fold(List[JsValue]()) {
(l, jsValue) => jsValue :: l
}).flatMap(_.run)
def get: List[JsValue] = {
Await.result(messages, aSecond)
}
}
def wrapConnection(connection: => Future[Iteratee[JsValue, Unit], Enumerator[JsValue]]): (Incoming, OutGoing) = {
val (iteratee, enumerator) = Await.result(conn, aSecond)
(Incoming(iteratee), OutGoing(enumerator))
}
}
फिर अपने परीक्षण लिखा जा सकता है के रूप में
"return all subscribers when asked for info" in new WebSocketContext {
val (incoming, outgoing) = wrapConnection(myWSLib.connect)
incoming.feed(JsObject("message" => "hello"))
incoming.end() //this closes the connection
val responseMessages = outgoing.get //you only call this "get" after the connection is closed
responseMessages.size must equalTo(1)
responseMessages must contain(JsObject("reply" => "Hey"))
}
आवक संदेश क्लाइंट साइड से आ रही प्रतिनिधित्व करते हैं, जबकि निवर्तमान भेजे गए संदेश का प्रतिनिधित्व करता है सर्वर से परीक्षण लिखने के लिए, आप पहले आने वाले संदेशों में आने वाले संदेशों में फ़ीड करते हैं और फिर आने वाले.एंड को कॉल करके कनेक्शन बंद कर देते हैं, फिर आपको आउटगोइंग.get विधि से आउटगोइंग संदेशों की पूरी सूची मिलती है।
इस लिंक सवाल का जवाब कर सकते हैं, यह जवाब के आवश्यक हिस्से में शामिल हैं और संदर्भ के लिए लिंक प्रदान करने के लिए बेहतर है। लिंक किए गए पृष्ठ में परिवर्तन होने पर लिंक-केवल उत्तर अमान्य हो सकते हैं। –
@ लॉरेंसएइलो आपसे सहमत हैं, मेरा बुरा यह मानना है कि यह काफी छोटा था। मैंने अधिक जानकारी अपडेट की है। –