Exemple #1
0
async def test_subscribe_options(test_case, mocker, protocol_gateway):
    test_topic = 'test'

    # pylint: disable=unused-argument
    async def mock_subscribe(topic, cb, subscribe_options=None):
        pass

    mocker.patch(
        'src.iotea.core.util.mqtt_client.MqttProtocolAdapter.subscribe',
        wraps=mock_subscribe)
    # test with None sub options
    await protocol_gateway.subscribe(test_topic,
                                     lambda: {},
                                     subscribe_options=None)
    test_case.assertEqual(
        1, protocol_gateway.adapters[0].instance.subscribe.call_count)
    protocol_gateway.adapters[0].instance.subscribe.assert_called_with(
        test_topic, unittest.mock.ANY, unittest.mock.ANY)
    args, _ = protocol_gateway.adapters[0].instance.subscribe.call_args
    subscribe_options = args[2]
    test_case.assertIsNotNone(subscribe_options)

    # test with custom options: subscribe with platform protocol adapter
    subscribe_options = ProtocolGateway.create_subscribe_options(True, None)
    await protocol_gateway.subscribe(test_topic,
                                     lambda: {},
                                     subscribe_options=subscribe_options)
    test_case.assertEqual(
        2, protocol_gateway.adapters[0].instance.subscribe.call_count)
    protocol_gateway.adapters[0].instance.subscribe.assert_called_with(
        test_topic, unittest.mock.ANY, subscribe_options)
Exemple #2
0
async def test_subscribe(test_case, mocker, protocol_gateway, test_message, force_wait):
    default_broker_url = 'mqtt://localhost:1883'
    test_topic = 'test'
    callback_invoked_event = asyncio.Event()

    # pylint: disable=unused-argument
    async def mock_subscribe(topic, cb, subscribe_options=None):
        if asyncio.iscoroutinefunction(cb):
            await cb(test_message, topic)
        else:
            cb(test_message, topic)

    mocker.patch('src.iotea.core.util.mqtt_client.MqttProtocolAdapter.subscribe', wraps=mock_subscribe)
    pg_subscribe_methods = [protocol_gateway.subscribe, protocol_gateway.subscribe_json]

    callbacks = [get_async_callback(test_case, callback_invoked_event, test_message, test_topic, default_broker_url),
                 get_sync_callback(test_case, callback_invoked_event, test_message, test_topic, default_broker_url)]

    for pg_subscribe in pg_subscribe_methods:
        for callback in callbacks:
            subscribe_options = ProtocolGateway.create_subscribe_options()

            await pg_subscribe(test_topic, callback, subscribe_options=subscribe_options, force_wait=force_wait)
            protocol_gateway.adapters[0].instance.subscribe.assert_called_with(test_topic, unittest.mock.ANY,
                                                                               subscribe_options)

            # the subscription is not awaited but sent to event loop, so wait to get the job done
            if not force_wait:
                await asyncio.wait_for(callback_invoked_event.wait(), TIMEOUT)
            # as Mock(async_callback) does not pass the test iscoroutingfunction,
            # we need this hack to manually check if the callback has been invoked
            test_case.assertEqual(True, callback_invoked_event.is_set(),
                                  f'Subscribe callback {callback} was not invoked from {pg_subscribe}!')
            callback_invoked_event.clear()