Beispiel #1
0
    def test_on_message(self):
        client = BcexClient([Symbol.ALGOUSD])
        client._check_message_seqnum = Mock()
        channels = [
            "dummy_channel",
            Channel.TRADES,
            Channel.TICKER,
            Channel.PRICES,
            Channel.AUTH,
            Channel.BALANCES,
            Channel.HEARTBEAT,
            Channel.SYMBOLS,
            Channel.L2,
            Channel.L3,
        ]

        # set up the mocks
        mocks = [Mock() for _ in range(len(channels))]
        client._on_message_unsupported = mocks[0]
        client._on_market_trade_updates = mocks[1]
        client._on_ticker_updates = mocks[2]
        client._on_price_updates = mocks[3]
        client._on_auth_updates = mocks[4]
        client._on_balance_updates = mocks[5]
        client._on_heartbeat_updates = mocks[6]
        client._on_symbols_updates = mocks[7]
        client._on_l2_updates = mocks[8]
        client._on_l3_updates = mocks[9]

        # checks
        msg = {}
        for i, ch in enumerate(channels):
            msg.update({"channel": ch})
            client.on_message(json.dumps(msg))
            assert client._check_message_seqnum.call_count == i + 1
            for j in range(i):
                assert mocks[j].call_count == 1
            for j in range(i + 1, len(channels)):
                assert mocks[j].call_count == 0
            assert mocks[i].call_args[0][0] == msg

        client.on_message(None)
Beispiel #2
0
    def test_seqnum(self):
        # first message should be 0
        client = BcexClient([Symbol.ALGOUSD])
        client.exit = Mock()
        client._on_heartbeat_updates = Mock()
        assert client._seqnum == -1

        client.on_message(
            json.dumps({
                "seqnum": 0,
                "channel": Channel.HEARTBEAT
            }))
        assert client.exit.call_count == 0

        # if first message is not 0 it exits
        client = BcexClient([Symbol.ALGOUSD])
        client.exit = Mock()
        client._on_heartbeat_updates = Mock()

        client.on_message(
            json.dumps({
                "seqnum": 1,
                "channel": Channel.HEARTBEAT
            }))
        assert client.exit.call_count == 1

        # if one message has not been received it exits
        client = BcexClient([Symbol.ALGOUSD])
        client.exit = Mock()
        client._on_heartbeat_updates = Mock()

        client.on_message(
            json.dumps({
                "seqnum": 0,
                "channel": Channel.HEARTBEAT
            }))
        client.on_message(
            json.dumps({
                "seqnum": 1,
                "channel": Channel.HEARTBEAT
            }))
        client.on_message(
            json.dumps({
                "seqnum": 2,
                "channel": Channel.HEARTBEAT
            }))
        client.on_message(
            json.dumps({
                "seqnum": 3,
                "channel": Channel.HEARTBEAT
            }))
        assert client.exit.call_count == 0
        client.on_message(
            json.dumps({
                "seqnum": 5,
                "channel": Channel.HEARTBEAT
            }))
        assert client.exit.call_count == 1