public NetworkStatusReporter(Uri testResultCoordinatorEndpoint, string moduleId, string trackingId) { this.trcClient = new TestResultCoordinatorClient() { BaseUrl = testResultCoordinatorEndpoint.AbsoluteUri }; this.moduleId = moduleId; this.trackingId = trackingId; }
public TwinEdgeOperationsResultHandler(Uri reporterUri, string moduleId, Option <string> trackingId) { this.trcClient = new TestResultCoordinatorClient() { BaseUrl = reporterUri.AbsoluteUri }; this.moduleId = moduleId; this.trackingId = trackingId.Expect(() => new ArgumentNullException(nameof(trackingId))); }
static async Task Main() { Logger.LogInformation($"Starting load gen with the following settings:\r\n{Settings.Current}"); ModuleClient moduleClient = null; try { (CancellationTokenSource cts, ManualResetEventSlim completed, Option <object> handler) = ShutdownHandler.Init(TimeSpan.FromSeconds(5), Logger); Guid batchId = Guid.NewGuid(); Logger.LogInformation($"Batch Id={batchId}"); moduleClient = await ModuleUtil.CreateModuleClientAsync( Settings.Current.TransportType, ModuleUtil.DefaultTimeoutErrorDetectionStrategy, ModuleUtil.DefaultTransientRetryStrategy, Logger); Logger.LogInformation($"Load gen delay start for {Settings.Current.TestStartDelay}."); await Task.Delay(Settings.Current.TestStartDelay); DateTime testStartAt = DateTime.UtcNow; long messageIdCounter = 1; while (!cts.IsCancellationRequested && (Settings.Current.TestDuration == TimeSpan.Zero || DateTime.UtcNow - testStartAt < Settings.Current.TestDuration)) { try { await SendEventAsync(moduleClient, batchId, Settings.Current.TrackingId, messageIdCounter); // Report sending message successfully to Test Result Coordinator await Settings.Current.TestResultCoordinatorUrl.ForEachAsync( async trcUrl => { Uri testResultCoordinatorUrl = new Uri( trcUrl, UriKind.Absolute); TestResultCoordinatorClient trcClient = new TestResultCoordinatorClient { BaseUrl = testResultCoordinatorUrl.AbsoluteUri }; await ModuleUtil.ReportStatus( trcClient, Logger, Settings.Current.ModuleId + ".send", ModuleUtil.FormatMessagesTestResultValue( Settings.Current.TrackingId, batchId.ToString(), messageIdCounter.ToString()), TestOperationResultType.Messages.ToString()); }); if (messageIdCounter % 1000 == 0) { Logger.LogInformation($"Sent {messageIdCounter} messages."); } await Task.Delay(Settings.Current.MessageFrequency); messageIdCounter++; } catch (Exception ex) { Logger.LogError(ex, $"[SendEventAsync] Sequence number {messageIdCounter}, BatchId: {batchId.ToString()};"); } } Logger.LogInformation("Finish sending messages."); await cts.Token.WhenCanceled(); completed.Set(); handler.ForEach(h => GC.KeepAlive(h)); } catch (Exception ex) { Logger.LogError(ex, "Error occurred during load gen."); } finally { Logger.LogInformation("Closing connection to Edge Hub."); moduleClient?.CloseAsync(); moduleClient?.Dispose(); } Logger.LogInformation("Load Gen complete. Exiting."); }
public static async Task <int> MainAsync() { Logger.LogInformation($"Starting DirectMethodSender with the following settings:\r\n{Settings.Current}"); (CancellationTokenSource cts, ManualResetEventSlim completed, Option <object> handler) = ShutdownHandler.Init(TimeSpan.FromSeconds(5), Logger); DirectMethodSenderBase directMethodClient = null; ModuleClient reportClient = null; Option <Uri> analyzerUrl = Settings.Current.AnalyzerUrl; Option <Uri> testReportCoordinatorUrl = Settings.Current.TestResultCoordinatorUrl; try { Guid batchId = Guid.NewGuid(); Logger.LogInformation($"Batch Id={batchId}"); directMethodClient = await CreateClientAsync(Settings.Current.InvocationSource); reportClient = await ModuleUtil.CreateModuleClientAsync( Settings.Current.TransportType, ModuleUtil.DefaultTimeoutErrorDetectionStrategy, ModuleUtil.DefaultTransientRetryStrategy, Logger); Logger.LogInformation($"Load gen delay start for {Settings.Current.TestStartDelay}."); await Task.Delay(Settings.Current.TestStartDelay, cts.Token); DateTime testStartAt = DateTime.UtcNow; while (!cts.Token.IsCancellationRequested && IsTestTimeUp(testStartAt)) { (HttpStatusCode result, long dmCounter) = await directMethodClient.InvokeDirectMethodAsync(cts); // TODO: Create an abstract class to handle the reporting client generation if (testReportCoordinatorUrl.HasValue) { await testReportCoordinatorUrl.ForEachAsync( async (Uri uri) => { TestResultCoordinatorClient trcClient = new TestResultCoordinatorClient { BaseUrl = uri.AbsoluteUri }; await ModuleUtil.ReportStatus( trcClient, Logger, Settings.Current.ModuleId + ".send", ModuleUtil.FormatDirectMethodTestResultValue( Settings.Current.TrackingId.Expect(() => new ArgumentException("TrackingId is empty")), batchId.ToString(), dmCounter.ToString(), result.ToString()), TestOperationResultType.DirectMethod.ToString()); }); } else { await analyzerUrl.ForEachAsync( async (Uri uri) => { AnalyzerClient analyzerClient = new AnalyzerClient { BaseUrl = uri.AbsoluteUri }; await ReportStatus(Settings.Current.TargetModuleId, result, analyzerClient); }, async() => { await reportClient.SendEventAsync("AnyOutput", new Message(Encoding.UTF8.GetBytes("Direct Method call succeeded."))); }); } await Task.Delay(Settings.Current.DirectMethodDelay, cts.Token); } await cts.Token.WhenCanceled(); } catch (Exception e) { Logger.LogError(e, "Error occurred during direct method sender test setup"); } finally { // Implicit CloseAsync() directMethodClient?.Dispose(); reportClient?.Dispose(); } completed.Set(); handler.ForEach(h => GC.KeepAlive(h)); Logger.LogInformation("DirectMethodSender Main() finished."); return(0); }
static async Task <MessageResponse> ProcessAndSendMessageAsync(Message message, object userContext) { Uri testResultCoordinatorUrl = Settings.Current.TestResultCoordinatorUrl; try { if (!(userContext is ModuleClient moduleClient)) { throw new InvalidOperationException("UserContext doesn't contain expected value"); } // Must make a new message instead of reusing the old message because of the way the SDK sends messages string trackingId = string.Empty; string batchId = string.Empty; string sequenceNumber = string.Empty; var messageProperties = new List <KeyValuePair <string, string> >(); foreach (KeyValuePair <string, string> prop in message.Properties) { switch (prop.Key) { case TestConstants.Message.TrackingIdPropertyName: trackingId = prop.Value ?? string.Empty; break; case TestConstants.Message.BatchIdPropertyName: batchId = prop.Value ?? string.Empty; break; case TestConstants.Message.SequenceNumberPropertyName: sequenceNumber = prop.Value ?? string.Empty; break; } messageProperties.Add(new KeyValuePair <string, string>(prop.Key, prop.Value)); } if (string.IsNullOrWhiteSpace(trackingId) || string.IsNullOrWhiteSpace(batchId) || string.IsNullOrWhiteSpace(sequenceNumber)) { Logger.LogWarning($"Received message missing info: trackingid={trackingId}, batchId={batchId}, sequenceNumber={sequenceNumber}"); return(MessageResponse.Completed); } // Report receiving message successfully to Test Result Coordinator TestResultCoordinatorClient trcClient = new TestResultCoordinatorClient { BaseUrl = testResultCoordinatorUrl.AbsoluteUri }; await ModuleUtil.ReportStatus( trcClient, Logger, Settings.Current.ModuleId + ".receive", ModuleUtil.FormatMessagesTestResultValue(trackingId, batchId, sequenceNumber), TestOperationResultType.Messages.ToString()); Logger.LogInformation($"Successfully received message: trackingid={trackingId}, batchId={batchId}, sequenceNumber={sequenceNumber}"); byte[] messageBytes = message.GetBytes(); var messageCopy = new Message(messageBytes); messageProperties.ForEach(kvp => messageCopy.Properties.Add(kvp)); await moduleClient.SendEventAsync(Settings.Current.OutputName, messageCopy); // Report sending message successfully to Test Result Coordinator await ModuleUtil.ReportStatus( trcClient, Logger, Settings.Current.ModuleId + ".send", ModuleUtil.FormatMessagesTestResultValue(trackingId, batchId, sequenceNumber), TestOperationResultType.Messages.ToString()); Logger.LogInformation($"Successfully sent message: trackingid={trackingId}, batchId={batchId}, sequenceNumber={sequenceNumber}"); } catch (Exception ex) { Logger.LogError(ex, "Error in ProcessAndSendMessageAsync"); } return(MessageResponse.Completed); }