示例#1
0
    def test_public_subscriptions(self):
        client = BcexClient(
            [Symbol.ETHBTC, Symbol.ALGOUSD],
            channels=[Channel.HEARTBEAT, Channel.SYMBOLS],
            channel_kwargs={
                Channel.SYMBOLS: {
                    "dummy_arg": "dummy_val"
                },
                Channel.HEARTBEAT: {
                    "t": 5
                },
            },
        )
        client._wait_for_confirmation = Mock()  # will test separately

        ws_mock = Mock()
        ws_mock.send = Mock()
        client.ws = ws_mock

        client._public_subscription()
        assert ws_mock.send.call_count == 3  # 2 for the 2 symbols and 1 for heartbeat
        subscriptions = [args[0][0] for args in ws_mock.send.call_args_list]
        assert set(subscriptions) == {
            json.dumps({
                "action": Action.SUBSCRIBE,
                "channel": Channel.SYMBOLS,
                "symbol": Symbol.ALGOUSD,
                "dummy_arg": "dummy_val",
            }),
            json.dumps({
                "action": Action.SUBSCRIBE,
                "channel": Channel.SYMBOLS,
                "symbol": Symbol.ETHBTC,
                "dummy_arg": "dummy_val",
            }),
            json.dumps({
                "action": Action.SUBSCRIBE,
                "channel": Channel.HEARTBEAT,
                "t": 5
            }),
        }

        assert (client.channel_status[Channel.HEARTBEAT] ==
                ChannelStatus.WAITING_CONFIRMATION)
        assert (client.channel_status[Channel.SYMBOLS][Symbol.ETHBTC] ==
                ChannelStatus.WAITING_CONFIRMATION)
        assert (client.channel_status[Channel.SYMBOLS][Symbol.ALGOUSD] ==
                ChannelStatus.WAITING_CONFIRMATION)

        # make sure we will wait for the correct channels
        assert client._wait_for_confirmation.call_count == 1
        assert set(client._wait_for_confirmation.call_args[0][0]) == {
            (Channel.HEARTBEAT, None),
            (Channel.SYMBOLS, Symbol.ETHBTC),
            (Channel.SYMBOLS, Symbol.ALGOUSD),
        }
示例#2
0
    def test_private_subscriptions(self):
        client = BcexClient(
            [Symbol.ETHBTC, Symbol.ALGOUSD],
            channels=[Channel.TRADING],
            api_secret="my_api_tokennn",
        )
        client._wait_for_confirmation = Mock()  # will test separately
        client._wait_for_authentication = Mock()  # will test separately

        ws_mock = Mock()
        ws_mock.send = Mock()
        client.ws = ws_mock

        client._private_subscription()
        assert ws_mock.send.call_count == 2  # one for authentication, one for channel

        # auth message sent
        auth = ws_mock.send.call_args_list[0][0][0]
        assert auth == json.dumps({
            "channel": Channel.AUTH,
            "action": Action.SUBSCRIBE,
            "token": "my_api_tokennn",
        })

        # subscription channel message sent
        subscriptions = ws_mock.send.call_args_list[1][0][0]
        assert subscriptions == json.dumps({
            "action": Action.SUBSCRIBE,
            "channel": Channel.TRADING
        })

        assert (client.channel_status[Channel.TRADING] ==
                ChannelStatus.WAITING_CONFIRMATION)

        # make sure we will wait for the correct channels
        assert client._wait_for_confirmation.call_count == 1
        assert client._wait_for_authentication.call_count == 1

        assert set(client._wait_for_confirmation.call_args[0][0]) == {
            (Channel.TRADING, None)
        }