2018-10-07 07:34:50 +00:00
|
|
|
# encoding: UTF-8
|
|
|
|
import json
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from Promise import Promise
|
2018-10-07 07:38:36 +00:00
|
|
|
from vnpy.restful.WebSocketClient import WebsocketClient
|
2018-10-07 07:34:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestWebsocketClient(WebsocketClient):
|
|
|
|
|
|
|
|
def __init__(self):
|
2018-10-07 08:19:48 +00:00
|
|
|
host = 'wss://echo.websocket.org'
|
|
|
|
super(TestWebsocketClient, self).__init__()
|
|
|
|
self.init(host)
|
2018-10-07 07:34:50 +00:00
|
|
|
self.p = Promise()
|
|
|
|
|
|
|
|
def onMessage(self, packet):
|
|
|
|
self.p.set_result(packet)
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onConnect(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onError(self, exceptionType, exceptionValue, tb):
|
|
|
|
self.p.set_exception(exceptionValue)
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class WebsocketClientTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.c = TestWebsocketClient()
|
|
|
|
self.c.start()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.c.stop()
|
|
|
|
|
|
|
|
def test_sendReq(self):
|
|
|
|
req = {
|
|
|
|
'name': 'val'
|
|
|
|
}
|
|
|
|
self.c.sendReq(req)
|
|
|
|
res = self.c.p.get(3)
|
|
|
|
|
|
|
|
self.assertEqual(res, req)
|