2018-10-07 07:34:50 +00:00
|
|
|
# encoding: UTF-8
|
2018-10-31 02:50:26 +00:00
|
|
|
import sys
|
2018-10-07 07:34:50 +00:00
|
|
|
import unittest
|
2018-10-31 02:50:26 +00:00
|
|
|
import uuid
|
2018-10-07 07:34:50 +00:00
|
|
|
|
|
|
|
from Promise import Promise
|
2018-10-24 01:29:20 +00:00
|
|
|
from vnpy.api.websocket import WebsocketClient
|
2018-10-07 07:34:50 +00:00
|
|
|
|
|
|
|
|
2018-10-24 01:29:20 +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-31 02:50:26 +00:00
|
|
|
self.cp = Promise()
|
2018-10-07 07:34:50 +00:00
|
|
|
|
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-31 02:50:26 +00:00
|
|
|
self.cp.set_result(True)
|
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()
|
2018-10-31 02:50:26 +00:00
|
|
|
self.c.cp.get(3) # wait for connected
|
2018-10-07 07:34:50 +00:00
|
|
|
|
|
|
|
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)
|
2018-10-31 02:50:26 +00:00
|
|
|
|
|
|
|
def test_parseError(self):
|
|
|
|
class CustomException(Exception): pass
|
|
|
|
|
|
|
|
def onPacket(packet):
|
|
|
|
raise CustomException("Just a exception")
|
|
|
|
|
|
|
|
self.c.onPacket = onPacket
|
|
|
|
req = {
|
|
|
|
'name': 'val'
|
|
|
|
}
|
|
|
|
self.c.sendPacket(req)
|
|
|
|
|
|
|
|
with self.assertRaises(CustomException):
|
|
|
|
self.c.p.get(3)
|
|
|
|
|
|
|
|
|
|
|
|
class WebsocketClientErrorHandleTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
|
|
|
self.c = TestWebsocketClient()
|
|
|
|
self.c.start()
|
|
|
|
self.c.cp.get(3) # wait for connected
|
|
|
|
|
|
|
|
self.org_sys_hook = sys.excepthook
|
|
|
|
self.org_sys_stderr_write = sys.stderr.write
|
|
|
|
|
|
|
|
sys.excepthook = self.nop
|
|
|
|
sys.stderr.write = self.nop
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def nop(*args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.c.stop()
|
|
|
|
sys.excepthook = self.org_sys_hook
|
|
|
|
sys.stderr.write = self.org_sys_stderr_write
|
|
|
|
|
|
|
|
def test_onError(self):
|
|
|
|
target= uuid.uuid4()
|
|
|
|
"""这个测试保证onError内不会再抛Exception"""
|
|
|
|
class CustomException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onPacket(packet):
|
|
|
|
raise CustomException("Just a exception")
|
|
|
|
|
|
|
|
def onError(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
super(TestWebsocketClient, self.c).onError(*args, **kwargs)
|
|
|
|
self.c.p.set_result(target)
|
|
|
|
except:
|
|
|
|
self.c.p.set_result(False)
|
|
|
|
|
|
|
|
self.c.onPacket = onPacket
|
|
|
|
self.c.onError = onError
|
|
|
|
|
|
|
|
req = {
|
|
|
|
'name': 'val'
|
|
|
|
}
|
|
|
|
self.c.sendPacket(req)
|
|
|
|
|
|
|
|
res = self.c.p.get(3)
|
|
|
|
self.assertEqual(target, res)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|