Esempio n. 1
0
    def test_sending_messages_increment_message_number(self, mock_ws):
        sent_messages = []
        throttler = AsyncThrottler(CONSTANTS.RATE_LIMITS)
        mock_ws.return_value = self.mocking_assistant.create_websocket_mock()
        mock_ws.return_value.send_json.side_effect = lambda sent_message: sent_messages.append(
            sent_message)

        adaptor = NdaxWebSocketAdaptor(throttler,
                                       websocket=mock_ws.return_value)
        payload = {}
        self.async_run_with_timeout(
            adaptor.send_request(endpoint_name=CONSTANTS.WS_PING_REQUEST,
                                 payload=payload,
                                 limit_id=CONSTANTS.WS_PING_ID))
        self.async_run_with_timeout(
            adaptor.send_request(endpoint_name=CONSTANTS.WS_PING_REQUEST,
                                 payload=payload,
                                 limit_id=CONSTANTS.WS_PING_ID))
        self.async_run_with_timeout(
            adaptor.send_request(endpoint_name=CONSTANTS.WS_ORDER_BOOK_CHANNEL,
                                 payload=payload))
        self.assertEqual(3, len(sent_messages))

        message = sent_messages[0]
        self.assertEqual(1, message.get('i'))
        message = sent_messages[1]
        self.assertEqual(2, message.get('i'))
        message = sent_messages[2]
        self.assertEqual(3, message.get('i'))
    def test_sending_messages_increment_message_number(self):
        sent_messages = []
        throttler = AsyncThrottler(CONSTANTS.RATE_LIMITS)
        ws = AsyncMock()
        ws.send.side_effect = lambda sent_message: sent_messages.append(sent_message)

        adaptor = NdaxWebSocketAdaptor(throttler, websocket=ws)
        payload = {}
        asyncio.get_event_loop().run_until_complete(adaptor.send_request(endpoint_name=CONSTANTS.WS_PING_REQUEST,
                                                                         payload=payload,
                                                                         limit_id=CONSTANTS.WS_PING_ID))
        asyncio.get_event_loop().run_until_complete(adaptor.send_request(endpoint_name=CONSTANTS.WS_PING_REQUEST,
                                                                         payload=payload,
                                                                         limit_id=CONSTANTS.WS_PING_ID))
        asyncio.get_event_loop().run_until_complete(adaptor.send_request(endpoint_name=CONSTANTS.WS_ORDER_BOOK_CHANNEL,
                                                                         payload=payload))
        self.assertEqual(3, len(sent_messages))

        message = json.loads(sent_messages[0])
        self.assertEqual(1, message.get('i'))
        message = json.loads(sent_messages[1])
        self.assertEqual(2, message.get('i'))
        message = json.loads(sent_messages[2])
        self.assertEqual(3, message.get('i'))
    def test_request_message_structure(self):
        sent_messages = []
        throttler = AsyncThrottler(CONSTANTS.RATE_LIMITS)
        ws = AsyncMock()
        ws.send.side_effect = lambda sent_message: sent_messages.append(sent_message)

        adaptor = NdaxWebSocketAdaptor(throttler, websocket=ws)
        payload = {"TestElement1": "Value1", "TestElement2": "Value2"}
        asyncio.get_event_loop().run_until_complete(adaptor.send_request(endpoint_name=CONSTANTS.WS_PING_REQUEST,
                                                                         payload=payload,
                                                                         limit_id=CONSTANTS.WS_PING_ID))

        self.assertEqual(1, len(sent_messages))
        message = json.loads(sent_messages[0])

        self.assertEqual(0, message.get('m'))
        self.assertEqual(1, message.get('i'))
        self.assertEqual(CONSTANTS.WS_PING_REQUEST, message.get('n'))
        message_payload = json.loads(message.get('o'))
        self.assertEqual(payload, message_payload)
Esempio n. 4
0
    def test_request_message_structure(self, mock_ws):
        sent_messages = []
        throttler = AsyncThrottler(CONSTANTS.RATE_LIMITS)
        mock_ws.return_value = self.mocking_assistant.create_websocket_mock()
        mock_ws.return_value.send_json.side_effect = lambda sent_message: sent_messages.append(
            sent_message)

        adaptor = NdaxWebSocketAdaptor(throttler,
                                       websocket=mock_ws.return_value)
        payload = {"TestElement1": "Value1", "TestElement2": "Value2"}
        self.async_run_with_timeout(
            adaptor.send_request(endpoint_name=CONSTANTS.WS_PING_REQUEST,
                                 payload=payload,
                                 limit_id=CONSTANTS.WS_PING_ID))

        self.assertEqual(1, len(sent_messages))
        message = sent_messages[0]

        self.assertEqual(0, message.get('m'))
        self.assertEqual(1, message.get('i'))
        self.assertEqual(CONSTANTS.WS_PING_REQUEST, message.get('n'))
        message_payload = json.loads(message.get('o'))
        self.assertEqual(payload, message_payload)