static void SendEvent(IOTHUB_PROVISIONED_DEVICE* deviceToUse) { // arrange IOTHUB_CLIENT_HANDLE iotHubClientHandle; IOTHUB_MESSAGE_HANDLE msgHandle; EXPECTED_SEND_DATA* sendData = EventData_Create(); ASSERT_IS_NOT_NULL(sendData, "Failure creating data to be sent"); // Send the Event { IOTHUB_CLIENT_RESULT result; // Create the IoT Hub Data iotHubClientHandle = IoTHubClient_CreateFromConnectionString(deviceToUse->connectionString, HTTP_Protocol); ASSERT_IS_NOT_NULL(iotHubClientHandle, "Failure creating IothubClient handle"); msgHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)sendData->expectedString, strlen(sendData->expectedString)); ASSERT_IS_NOT_NULL(msgHandle, "Failure to create message handle"); if (deviceToUse->howToCreate == IOTHUB_ACCOUNT_AUTH_X509) { result = IoTHubClient_SetOption(iotHubClientHandle, OPTION_X509_CERT, deviceToUse->certificate); ASSERT_ARE_EQUAL(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_OK, result, "Could not set the device x509 certificate"); result = IoTHubClient_SetOption(iotHubClientHandle, OPTION_X509_PRIVATE_KEY, deviceToUse->primaryAuthentication); ASSERT_ARE_EQUAL(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_OK, result, "Could not set the device x509 privateKey"); } // act result = IoTHubClient_SendEventAsync(iotHubClientHandle, msgHandle, ReceiveConfirmationCallback, sendData); ASSERT_ARE_EQUAL(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_OK, result, "Failure calling IoTHubClient_SendEventAsync"); } time_t beginOperation, nowTime; beginOperation = time(NULL); while ( (nowTime = time(NULL)), (difftime(nowTime, beginOperation) < MAX_CLOUD_TRAVEL_TIME) // time box ) { if (Lock(sendData->lock) != LOCK_OK) { ASSERT_FAIL("unable to lock"); } else { if (sendData->dataWasRecv) { Unlock(sendData->lock); break; } Unlock(sendData->lock); } ThreadAPI_Sleep(100); } if (Lock(sendData->lock) != LOCK_OK) { ASSERT_FAIL("unable to lock"); } else { ASSERT_IS_TRUE(sendData->dataWasRecv, "Failure sending data to IotHub"); // was found is written by the callback... (void)Unlock(sendData->lock); } { IOTHUB_TEST_HANDLE iotHubTestHandle = IoTHubTest_Initialize(IoTHubAccount_GetEventHubConnectionString(g_iothubAcctInfo1), IoTHubAccount_GetIoTHubConnString(g_iothubAcctInfo1), deviceToUse->deviceId, IoTHubAccount_GetEventhubListenName(g_iothubAcctInfo1), IoTHubAccount_GetEventhubAccessKey(g_iothubAcctInfo1), IoTHubAccount_GetSharedAccessSignature(g_iothubAcctInfo1), IoTHubAccount_GetEventhubConsumerGroup(g_iothubAcctInfo1)); ASSERT_IS_NOT_NULL(iotHubTestHandle); IOTHUB_TEST_CLIENT_RESULT result = IoTHubTest_ListenForEventForMaxDrainTime(iotHubTestHandle, IoTHubCallback, IoTHubAccount_GetIoTHubPartitionCount(g_iothubAcctInfo1), sendData); ASSERT_ARE_EQUAL(IOTHUB_TEST_CLIENT_RESULT, IOTHUB_TEST_CLIENT_OK, result); IoTHubTest_Deinit(iotHubTestHandle); } // assert ASSERT_IS_TRUE(sendData->wasFound, "Failure receiving data from eventhub"); // was found is written by the callback...*/ // cleanup IoTHubMessage_Destroy(msgHandle); IoTHubClient_Destroy(iotHubClientHandle); EventData_Destroy(sendData); }
static int sendEventLoop(IOTHUB_CLIENT_HANDLE iotHubClientHandle, LONGHAUL_SEND_TEST_STATE* test_state) { int result = 0; #ifndef MBED_BUILD_TIMESTAMP IOTHUB_TEST_HANDLE iotHubTestHandle; if ((iotHubTestHandle = IoTHubTest_Initialize(IoTHubAccount_GetEventHubConnectionString(g_iothubAcctInfo), IoTHubAccount_GetIoTHubConnString(g_iothubAcctInfo), IoTHubAccount_GetDeviceId(g_iothubAcctInfo), IoTHubAccount_GetDeviceKey(g_iothubAcctInfo), IoTHubAccount_GetEventhubListenName(g_iothubAcctInfo), IoTHubAccount_GetEventhubAccessKey(g_iothubAcctInfo), IoTHubAccount_GetSharedAccessSignature(g_iothubAcctInfo), IoTHubAccount_GetEventhubConsumerGroup(g_iothubAcctInfo))) == NULL) { LogError("Failed initializing the Event Hub test client."); result = __LINE__; } else { initializeSendStatistics(&test_state->statistics); #endif time_t testInitialTime; if ((testInitialTime = time(NULL)) == INDEFINITE_TIME) { LogError("Failed setting the initial time of the test (time(NULL) failed)."); result = __LINE__; } else { time_t loopIterationStartTimeInSeconds, loopIterationEndTimeInSeconds; double loopIterationTotalTime; time_t testCurrentTime; while (result == 0) { if ((testCurrentTime = time(NULL)) == INDEFINITE_TIME) { LogError("Failed setting the current time of the test (time(NULL) failed)"); result = __LINE__; break; } else if (difftime(testCurrentTime, testInitialTime) > test_state->profile->totalRunTimeInSeconds) { LogInfo("Test run for the expected duration (%d seconds)", test_state->profile->totalRunTimeInSeconds); break; } if ((loopIterationStartTimeInSeconds = time(NULL)) == INDEFINITE_TIME) { LogError("Failed setting the initial time of the send/receive loop (time(NULL) failed)"); result = __LINE__; break; } if (test_state->timeUntilNextSendEventInSeconds <= 0.0) { EXPECTED_SEND_DATA* sendData; IOTHUB_MESSAGE_HANDLE msgHandle; if ((sendData = EventData_Create()) == NULL) { LogError("Failed creating EXPECTED_SEND_DATA."); result = __LINE__; } else { if ((msgHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)sendData->expectedString, strlen(sendData->expectedString))) == NULL) { LogError("Failed creating IOTHUB_MESSAGE_HANDLE."); result = __LINE__; } else { if (IoTHubClient_SendEventAsync(iotHubClientHandle, msgHandle, SendConfirmationCallback, sendData) != IOTHUB_CLIENT_OK) { LogError("Call to IoTHubClient_SendEventAsync failed."); result = __LINE__; } else { bool dataWasSent = false; time_t beginOperation, nowTime; if ((beginOperation = time(NULL)) == INDEFINITE_TIME) { LogError("Failed setting beginOperation (time(NULL) failed)."); result = __LINE__; } else { do { if (Lock(sendData->lock) != LOCK_OK) { LogError("Unable to lock to flag event sent."); break; } else { if (sendData->dataWasSent) { dataWasSent = true; Unlock(sendData->lock); break; } Unlock(sendData->lock); } ThreadAPI_Sleep(100); if ((nowTime = time(NULL)) == INDEFINITE_TIME) { LogError("Failed setting nowTime (time(NULL) failed)."); result = __LINE__; break; } } while (difftime(nowTime, beginOperation) < MAX_CLOUD_TRAVEL_TIME); if (!dataWasSent) { LogError("Failure sending data to IotHub"); result = __LINE__; } else { #ifdef MBED_BUILD_TIMESTAMP if (verifyEventReceivedByHub(sendData) != 0) { result = __LINE__; } else { #else if (verifyEventReceivedByHub(sendData, iotHubTestHandle) != 0) { result = __LINE__; } else { computeSendStatistics(&test_state->statistics, sendData); #endif test_state->timeUntilNextSendEventInSeconds = test_state->profile->eventFrequencyInSecs[test_state->sendFrequencyIndex]; if ((test_state->sendFrequencyIndex + 1) < test_state->profile->numberOfEventFrequencyVariations) test_state->sendFrequencyIndex++; } } } } IoTHubMessage_Destroy(msgHandle); } EventData_Destroy(sendData); } } ThreadAPI_Sleep(500); if ((loopIterationEndTimeInSeconds = time(NULL)) == INDEFINITE_TIME) { LogError("Failed setting the end time of the send loop iteration (time(NULL) failed)"); result = __LINE__; } else { loopIterationTotalTime = difftime(loopIterationEndTimeInSeconds, loopIterationStartTimeInSeconds); test_state->timeUntilNextSendEventInSeconds -= loopIterationTotalTime; } } // While loop } #ifndef MBED_BUILD_TIMESTAMP printSendStatistics(&test_state->statistics); IoTHubTest_Deinit(iotHubTestHandle); } #endif return result; } static int receiveMessageLoop(IOTHUB_CLIENT_HANDLE iotHubClientHandle, LONGHAUL_RECEIVE_TEST_STATE* test_state) { int result = 0; #ifndef MBED_BUILD_TIMESTAMP IOTHUB_TEST_HANDLE iotHubTestHandle; if ((iotHubTestHandle = IoTHubTest_Initialize(IoTHubAccount_GetEventHubConnectionString(g_iothubAcctInfo), IoTHubAccount_GetIoTHubConnString(g_iothubAcctInfo), IoTHubAccount_GetDeviceId(g_iothubAcctInfo), IoTHubAccount_GetDeviceKey(g_iothubAcctInfo), IoTHubAccount_GetEventhubListenName(g_iothubAcctInfo), IoTHubAccount_GetEventhubAccessKey(g_iothubAcctInfo), IoTHubAccount_GetSharedAccessSignature(g_iothubAcctInfo), IoTHubAccount_GetEventhubConsumerGroup(g_iothubAcctInfo))) == NULL) { LogError("Failed initializing the Event Hub test client."); result = __LINE__; } else { initializeReceiveStatistics(&test_state->statistics); #endif time_t testInitialTime; if ((testInitialTime = time(NULL)) == INDEFINITE_TIME) { LogError("Failed setting the initial time of the test (time(NULL) failed)."); result = __LINE__; } else { time_t loopIterationStartTimeInSeconds, loopIterationEndTimeInSeconds; double loopIterationTotalTime; time_t testCurrentTime; while (result == 0) { if ((testCurrentTime = time(NULL)) == INDEFINITE_TIME) { LogError("Failed setting the current time of the test (time(NULL) failed)"); result = __LINE__; break; } else if (difftime(testCurrentTime, testInitialTime) > test_state->profile->totalRunTimeInSeconds) { LogInfo("Test run for the expected duration (%d seconds)", test_state->profile->totalRunTimeInSeconds); break; } if ((loopIterationStartTimeInSeconds = time(NULL)) == INDEFINITE_TIME) { LogError("Failed setting the initial time of the receive loop iteration (time(NULL) failed)"); result = __LINE__; break; } if (test_state->timeUntilNextReceiveMessageInSeconds <= 0.0) { EXPECTED_RECEIVE_DATA* receiveData; if ((receiveData = MessageData_Create()) == NULL) { LogError("Failed creating EXPECTED_RECEIVE_DATA."); result = __LINE__; } else { IOTHUB_TEST_CLIENT_RESULT sendResult; if (IoTHubClient_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, receiveData) != IOTHUB_CLIENT_OK) { LogError("Call to IoTHubClient_SetMessageCallback failed."); result = __LINE__; } else if ((sendResult = IoTHubTest_SendMessage(iotHubTestHandle, (const unsigned char*)receiveData->data, receiveData->dataSize)) != IOTHUB_TEST_CLIENT_OK) { LogError("Call to IoTHubTest_SendMessage failed (%i).", sendResult); result = __LINE__; } else { if ((receiveData->timeSent = time(NULL)) == INDEFINITE_TIME) { LogError("Failed setting receiveData->timeSent (time(NULL) failed)"); } time_t beginOperation, nowTime; if ((beginOperation = time(NULL)) == INDEFINITE_TIME) { LogError("Failed setting beginOperation (time(NULL) failed)."); result = __LINE__; } else { do { if (Lock(receiveData->lock) != LOCK_OK) { LogError("Unable to lock to verify if C2D message has been received."); result = __LINE__; break; } else { if (receiveData->receivedByClient) { (void)Unlock(receiveData->lock); break; } (void)Unlock(receiveData->lock); } ThreadAPI_Sleep(100); if ((nowTime = time(NULL)) == INDEFINITE_TIME) { LogError("Failed setting nowTime (time(NULL) failed)."); result = __LINE__; break; } } while (difftime(nowTime, beginOperation) < MAX_CLOUD_TRAVEL_TIME); if (result == 0) { if (!receiveData->receivedByClient) { LogError("Failure retrieving data from C2D"); result = __LINE__; } else { #ifndef MBED_BUILD_TIMESTAMP computeReceiveStatistics(&test_state->statistics, receiveData); #endif test_state->timeUntilNextReceiveMessageInSeconds = test_state->profile->messageFrequencyInSecs[test_state->receiveFrequencyIndex]; if ((test_state->receiveFrequencyIndex + 1) < test_state->profile->numberOfMessageFrequencyVariations) test_state->receiveFrequencyIndex++; } } } } MessageData_Destroy(receiveData); } } ThreadAPI_Sleep(500); if ((loopIterationEndTimeInSeconds = time(NULL)) == INDEFINITE_TIME) { LogError("Failed setting the end time of the receive loop iteration (time(NULL) failed)"); result = __LINE__; } else { loopIterationTotalTime = difftime(loopIterationEndTimeInSeconds, loopIterationStartTimeInSeconds); test_state->timeUntilNextReceiveMessageInSeconds -= loopIterationTotalTime; } } // While loop } #ifndef MBED_BUILD_TIMESTAMP printReceiveStatistics(&test_state->statistics); IoTHubTest_Deinit(iotHubTestHandle); } #endif return result; }