2013-05-03 10 views
10

मेरे पास मेरे Play एप्लिकेशन में एक वेबसाकेट है और मैं इसके लिए एक परीक्षण लिखना चाहता हूं, लेकिन मुझे इस तरह के परीक्षण को लिखने के बारे में कोई उदाहरण नहीं मिला। मुझे play-framework Google समूह में एक चर्चा मिली लेकिन हाल ही में कोई गतिविधि नहीं हुई है।PlayFramework में टेस्ट वेबसॉकेट

तो, क्या जावा टेस्ट में WebSocket की जांच करने के तरीके पर कोई विचार है?

उत्तर

3

आप Iteratee, गणनाकार अंतर्निहित प्राप्त कर सकते हैं और उन्हें सीधे परीक्षण । इस तरह आपको ब्राउज़र का उपयोग करने की आवश्यकता नहीं है। यद्यपि इटेटेट्स की एसिंक्रोनस प्रकृति से निपटने के लिए आपको अक्का-टेस्टकिट की आवश्यकता है।

एक स्काला उदाहरण:

object WebSocket extends Controller { 
    def websocket = WebSocket.async[JsValue] { request => 
    Future.successful(Iteratee.ignore[JsValue] -> Enumerator.apply[JsValue](Json.obj("type" -> "error"))) 
    } 
} 

class WebSocketSpec extends PlaySpecification {  
    "WebSocket" should { 
    "respond with error packet" in new WithApplication { 
     val request = FakeRequest() 

     var message: JsValue = null 
     val iteratee = Iteratee.foreach[JsValue](chunk => message = chunk)(Akka.system.dispatcher) 

     Controller.websocket().f(request)(Enumerator.empty[JsValue],iteratee) 

     TestKit.awaitCond(message == Json.obj("type" -> "error"), 1 second) 
    } 
    } 
} 
0

मान लें कि आप एक 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 विधि से आउटगोइंग संदेशों की पूरी सूची मिलती है।

1

क्रोम वेबस्केट सेवा का परीक्षण करने के लिए plugin प्रदान करता है।

संपादित

तो प्लगइन का उपयोग कर (जैसा कि नीचे चित्र में दिखाया गया है) आप WebSocket यूआरएल और अनुरोध डेटा उपलब्ध कराने और सेवा के लिए संदेश भेज सकते हैं। और संदेश लॉग क्लाइंट से भेजे गए संदेश और सेवा प्रतिक्रिया भी दिखाता है।

enter image description here

+1

इस लिंक सवाल का जवाब कर सकते हैं, यह जवाब के आवश्यक हिस्से में शामिल हैं और संदर्भ के लिए लिंक प्रदान करने के लिए बेहतर है। लिंक किए गए पृष्ठ में परिवर्तन होने पर लिंक-केवल उत्तर अमान्य हो सकते हैं। –

+0

@ लॉरेंसएइलो आपसे सहमत हैं, मेरा बुरा यह मानना ​​है कि यह काफी छोटा था। मैंने अधिक जानकारी अपडेट की है। –

संबंधित मुद्दे