async def send_test_message(i):
     print("sending message #" + str(i))
     msg = Message("test wind speed " + str(i))
     msg.message_id = uuid.uuid4()
     msg.correlation_id = "correlation-1234"
     msg.custom_properties["tornado-warning"] = "yes"
     await device_client.send_event(msg)
     print("done sending message #" + str(i))
 async def test_send_to_output_calls_transport(self, client, transport):
     message = Message("this is a message")
     output_name = "some_output"
     await client.send_to_output(message, output_name)
     assert transport.send_output_event.call_count == 1
     assert transport.send_output_event.call_args[0][0] == message
     assert message.output_name == output_name
    def test_send_event_sends_overlapped_events(self, device_transport):
        fake_msg_1 = create_fake_message()
        fake_msg_2 = Message(fake_event_2)

        mock_mqtt_provider = device_transport._mqtt_provider

        # connect
        device_transport.connect()
        mock_mqtt_provider.on_mqtt_connected()

        # send an event
        callback_1 = MagicMock()
        device_transport.send_event(fake_msg_1, callback_1)
        mock_mqtt_provider.publish.assert_called_once_with(
            encoded_fake_topic, fake_msg_1.data)

        # while we're waiting for that send to complete, send another event
        callback_2 = MagicMock()
        device_transport.send_event(fake_msg_2, callback_2)

        # verify that we've called publish twice and verify that neither send_event
        # has completed (because we didn't do anything here to complete it).
        assert mock_mqtt_provider.publish.call_count == 2
        callback_1.assert_not_called()
        callback_2.assert_not_called()
    def test_receive_c2d_message_returns_message_from_c2d_inbox(self, mocker, client):
        message = Message("this is a message")
        inbox_mock = mocker.MagicMock(autospec=SyncClientInbox)
        inbox_mock.get.return_value = message
        manager_get_inbox_mock = mocker.patch.object(
            client._inbox_manager, "get_c2d_message_inbox", return_value=inbox_mock
        )

        received_message = client.receive_c2d_message()
        assert manager_get_inbox_mock.call_count == 1
        assert inbox_mock.get.call_count == 1
        assert received_message is message
    def test_send_message_with_no_properties(self, device_transport):
        fake_msg = Message("Petrificus Totalus")

        mock_mqtt_provider = device_transport._mqtt_provider

        device_transport.connect()
        mock_mqtt_provider.on_mqtt_connected()
        device_transport.send_event(fake_msg)

        mock_mqtt_provider.connect.assert_called_once_with(
            device_transport._auth_provider.get_current_sas_token())
        mock_mqtt_provider.publish.assert_called_once_with(
            fake_topic, fake_msg.data)
    def test_send_message_with_output_name(self, module_transport):
        fake_msg = Message("Petrificus Totalus")
        fake_msg.custom_properties[
            custom_property_name] = custom_property_value
        fake_msg.output_name = "fake_output_name"

        fake_output_topic = ("devices/" + fake_device_id + "/modules/" +
                             fake_module_id + "/messages/events/" +
                             before_sys_key + "on" + after_sys_key +
                             "fake_output_name" + topic_separator +
                             custom_property_name + after_sys_key +
                             custom_property_value)

        mock_mqtt_provider = module_transport._mqtt_provider

        module_transport.connect()
        mock_mqtt_provider.on_mqtt_connected()
        module_transport.send_event(fake_msg)

        mock_mqtt_provider.connect.assert_called_once_with(
            module_transport._auth_provider.get_current_sas_token())
        mock_mqtt_provider.publish.assert_called_once_with(
            fake_output_topic, fake_msg.data)
    async def test_receive_input_message_returns_message_from_input_inbox(
            self, mocker, client):
        message = Message("this is a message")
        inbox_mock = mocker.MagicMock(autospec=AsyncClientInbox)
        inbox_mock.get.return_value = await create_completed_future(message)
        manager_get_inbox_mock = mocker.patch.object(client._inbox_manager,
                                                     "get_input_message_inbox",
                                                     return_value=inbox_mock)

        input_name = "some_input"
        received_message = await client.receive_input_message(input_name)
        assert manager_get_inbox_mock.call_count == 1
        assert manager_get_inbox_mock.call_args == mocker.call(input_name)
        assert inbox_mock.get.call_count == 1
        assert received_message is message
 def message(self):
     return Message("some message")
 async def test_send_event_calls_transport(self, client, transport):
     message = Message("this is a message")
     await client.send_event(message)
     assert transport.send_event.call_count == 1
     assert transport.send_event.call_args[0][0] == message
# The connection string for a device should never be stored in code. For the sake of simplicity we're using an environment variable here.
conn_str = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING")
# The "Authentication Provider" is the object in charge of creating authentication "tokens" for the device client.
auth_provider = auth.from_connection_string(conn_str)
# For now, the SDK only supports MQTT as a protocol. the client object is used to interact with your Azure IoT hub.
# It needs an Authentication Provider to secure the communication with the hub, using either tokens or x509 certificates
device_client = DeviceClient.from_authentication_provider(
    auth_provider, "mqtt")

# Connect the client.
device_client.connect()

# send 5 messages with a 1 second pause between each message
for i in range(0, 5):
    print("sending message #" + str(i))
    msg = Message("test wind speed " + str(i))
    msg.message_id = uuid.uuid4()
    msg.correlation_id = "correlation-1234"
    msg.custom_properties["tornado-warning"] = "yes"
    device_client.send_event(msg)
    time.sleep(1)

# send only string messages
for i in range(5, 10):
    print("sending message #" + str(i))
    device_client.send_event("test payload message " + str(i))
    time.sleep(1)

# finally, disconnect
device_client.disconnect()
def create_fake_output_message():
    msg = Message(fake_event)
    msg.message_id = fake_message_id
    msg.output_name = "fake_output_name"
    return msg
def create_fake_message():
    msg = Message(fake_event)
    msg.message_id = fake_message_id
    msg.custom_properties[custom_property_name] = custom_property_value
    return msg