public static void openClientWithRetry(InternalClient client) throws InterruptedException { //Check again int count = 0; boolean clientOpenSucceeded = false; long startTime = System.currentTimeMillis(); while (!clientOpenSucceeded) { if (System.currentTimeMillis() - startTime > OPEN_RETRY_TIMEOUT) { Assert.fail(buildExceptionMessage("Timed out trying to open the client " + count, client)); } try { count++; client.open(); clientOpenSucceeded = true; } catch (IOException e) { //ignore and try again System.out.println("Encountered exception while opening device client, retrying...: " + Tools.getStackTraceFromThrowable(e)); Thread.sleep(400); } } }
/** * Ends the DeviceClient connection and destroy the thread. * @throws IOException if the DeviceClient cannot close the connection with the IoTHub. */ void stop() throws IOException { if (this.client != null) { this.client.closeNow(); } stopDevice = true; }
@Override public void run() { clientArrayList.get(indexJ).sendEventAsync(new com.microsoft.azure.sdk.iot.device.Message(messageStates[indexI].messageBody), new FileUploadCallback(), messageStates[indexI]); } });
public static void sendMessagesExpectingUnrecoverableConnectionLossAndTimeout(InternalClient client, IotHubClientProtocol protocol, Message errorInjectionMessage, AuthenticationType authType) throws IOException, InterruptedException { final List<Pair<IotHubConnectionStatus, Throwable>> statusUpdates = new ArrayList<>(); client.registerConnectionStatusChangeCallback(new IotHubConnectionStatusChangeCallback() { @Override public void execute(IotHubConnectionStatus status, IotHubConnectionStatusChangeReason statusChangeReason, Throwable throwable, Object callbackContext) { statusUpdates.add(new Pair<>(status, throwable)); } }, new Object()); openClientWithRetry(client); client.sendEventAsync(errorInjectionMessage, new EventCallback(null), new Success()); long startTime = System.currentTimeMillis(); while (!(actualStatusUpdatesContainsStatus(statusUpdates, IotHubConnectionStatus.DISCONNECTED_RETRYING) && actualStatusUpdatesContainsStatus(statusUpdates, IotHubConnectionStatus.DISCONNECTED))) { Thread.sleep(500); if (System.currentTimeMillis() - startTime > 30 * 1000) { break; } } Assert.assertTrue(buildExceptionMessage(protocol + ", " + authType + ": Expected notification about disconnected but retrying.", client), actualStatusUpdatesContainsStatus(statusUpdates, IotHubConnectionStatus.DISCONNECTED_RETRYING)); Assert.assertTrue(buildExceptionMessage(protocol + ", " + authType + ": Expected notification about disconnected.", client), actualStatusUpdatesContainsStatus(statusUpdates, IotHubConnectionStatus.DISCONNECTED)); client.closeNow(); }
testInstance.client.setOption("SetSASTokenExpiryTime", SECONDS_FOR_SAS_TOKEN_TO_LIVE_BEFORE_RENEWAL); IotHubServicesCommon.openClientWithRetry(testInstance.client); testInstance.client.sendEventAsync(new Message("some message body"), callback, messageSent);
client.sendEventAsync(expiredMessage, new EventCallback(IotHubStatusCode.MESSAGE_EXPIRED), messageSentExpiredCallback); client.closeNow(); client.closeNow(); Assert.fail(buildExceptionMessage("Sending expired message over " + protocol + " protocol failed: Exception encountered while sending message and waiting for MESSAGE_EXPIRED callback: " + e.getMessage(), client));
client.setRetryPolicy(new NoRetry()); this.testInstance.protocol); client.closeNow();
@Override public void run() { try { deviceUnderTest.dCDeviceForTwin.updateExistingReportedProperty(index); internalClient.sendReportedProperties(deviceUnderTest.dCDeviceForTwin.getReportedProp()); waitAndVerifyTwinStatusBecomesSuccess(); } catch (IOException | InterruptedException e) { fail(buildExceptionMessage("Unexpected exception occurred during sending reported properties: " + e.getMessage(), internalClient)); } } });
super.setOption(optionName, value);
protected void setConnectionStatusCallBack(final List<Pair<IotHubConnectionStatus, Throwable>> actualStatusUpdates) { IotHubConnectionStatusChangeCallback connectionStatusUpdateCallback = new IotHubConnectionStatusChangeCallback() { @Override public void execute(IotHubConnectionStatus status, IotHubConnectionStatusChangeReason statusChangeReason, Throwable throwable, Object callbackContext) { actualStatusUpdates.add(new Pair<>(status, throwable)); } }; this.testInstance.deviceTestManager.client.registerConnectionStatusChangeCallback(connectionStatusUpdateCallback, null); }
InternalClient(IotHubConnectionString iotHubConnectionString, IotHubClientProtocol protocol, String publicKeyCertificate, boolean isCertificatePath, String privateKey, boolean isPrivateKeyPath, long sendPeriodMillis, long receivePeriodMillis) throws URISyntaxException { // Codes_SRS_INTERNALCLIENT_34_078: [If the connection string or protocol is null, this function shall throw an IllegalArgumentException.] commonConstructorVerification(iotHubConnectionString, protocol); // Codes_SRS_INTERNALCLIENT_34_079: [This function shall save a new config using the provided connection string, and x509 certificate information.] this.config = new DeviceClientConfig(iotHubConnectionString, publicKeyCertificate, isCertificatePath, privateKey, isPrivateKeyPath); this.config.setProtocol(protocol); // Codes_SRS_INTERNALCLIENT_34_080: [This function shall save a new DeviceIO instance using the created config and the provided send/receive periods.] this.deviceIO = new DeviceIO(this.config, sendPeriodMillis, receivePeriodMillis); this.logger = new CustomLogger(this.getClass()); }
public void errorInjectionTestFlow(com.microsoft.azure.sdk.iot.device.Message errorInjectionMessage) throws IOException, IotHubException, InterruptedException testInstance.client.registerConnectionStatusChangeCallback(new IotHubConnectionStatusChangeCallback() testInstance.client.sendEventAsync(errorInjectionMessage, new EventCallback(null), null); testInstance.client.closeNow();
@Test public void testUpdateReportedProperties() throws IOException, InterruptedException, IotHubException { // arrange // send max_prop RP all at once deviceUnderTest.dCDeviceForTwin.createNewReportedProperties(MAX_PROPERTIES_TO_TEST); internalClient.sendReportedProperties(deviceUnderTest.dCDeviceForTwin.getReportedProp()); Thread.sleep(DELAY_BETWEEN_OPERATIONS); // act // Update RP deviceUnderTest.dCDeviceForTwin.updateAllExistingReportedProperties(); internalClient.sendReportedProperties(deviceUnderTest.dCDeviceForTwin.getReportedProp()); // assert waitAndVerifyTwinStatusBecomesSuccess(); // verify if they are received by SC readReportedPropertiesAndVerify(deviceUnderTest, PROPERTY_KEY, PROPERTY_VALUE_UPDATE, MAX_PROPERTIES_TO_TEST.intValue()); }
public static String buildExceptionMessage(String baseMessage, InternalClient client) { if (client == null || client.getConfig() == null) { throw new IllegalArgumentException("client and config must not be null"); } return buildExceptionMessage( baseMessage, client.getConfig().getDeviceId(), client.getConfig().getProtocol().toString(), client.getConfig().getIotHubHostname(), client.getConfig().getModuleId()); }
protected void setConnectionStatusCallBack(final List<com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair<IotHubConnectionStatus, Throwable>> actualStatusUpdates) { IotHubConnectionStatusChangeCallback connectionStatusUpdateCallback = new IotHubConnectionStatusChangeCallback() { @Override public void execute(IotHubConnectionStatus status, IotHubConnectionStatusChangeReason statusChangeReason, Throwable throwable, Object callbackContext) { actualStatusUpdates.add(new com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair<>(status, throwable)); } }; this.internalClient.registerConnectionStatusChangeCallback(connectionStatusUpdateCallback, null); }
InternalClient(IotHubConnectionString iotHubConnectionString, IotHubClientProtocol protocol, long sendPeriodMillis, long receivePeriodMillis) { /* Codes_SRS_INTERNALCLIENT_21_004: [If the connection string is null or empty, the function shall throw an IllegalArgumentException.] */ commonConstructorVerification(iotHubConnectionString, protocol); this.config = new DeviceClientConfig(iotHubConnectionString); this.config.setProtocol(protocol); this.deviceIO = new DeviceIO(this.config, sendPeriodMillis, receivePeriodMillis); this.logger = new CustomLogger(this.getClass()); }
client.open(); clientOpenSucceeded = true;