2018-10-07 07:34:50 +00:00
|
|
|
# encoding: UTF-8
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from Promise import Promise
|
2018-10-15 08:45:41 +00:00
|
|
|
from vnpy.api.websocket import WebSocketClient
|
2018-10-07 07:34:50 +00:00
|
|
|
|
|
|
|
|
2018-10-15 08:45:41 +00:00
|
|
|
class TestWebsocketClient(WebSocketClient):
|
2018-10-07 07:34:50 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2018-10-15 08:29:39 +00:00
|
|
|
def onPacket(self, packet):
|
2018-10-07 07:34:50 +00:00
|
|
|
self.p.set_result(packet)
|
|
|
|
pass
|
|
|
|
|
2018-10-15 08:35:38 +00:00
|
|
|
def onConnected(self):
|
2018-10-07 07:34:50 +00:00
|
|
|
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'
|
|
|
|
}
|
2018-10-15 08:37:43 +00:00
|
|
|
self.c.sendPacket(req)
|
2018-10-07 07:34:50 +00:00
|
|
|
res = self.c.p.get(3)
|
|
|
|
|
|
|
|
self.assertEqual(res, req)
|