Ejemplo n.º 1
0
        internal override async Task ReportStatusAsync(TestResultBase testResult)
        {
            switch (testResult.GetType().Name)
            {
            case nameof(LegacyDirectMethodTestResult):
                LegacyDirectMethodTestResult shadowReport = testResult as LegacyDirectMethodTestResult;
                // The Old End-to-End test framework checks if there is an event in the eventHub for a particular module
                // to determine if the test case is passed/failed. Hence, this function need to check if the testResult status is Http.OK
                // before sending an event.
                if (shadowReport.Result == HttpStatusCode.OK.ToString())
                {
                    await this.ModuleClient.SendEventAsync("AnyOutput", new Message(Encoding.UTF8.GetBytes($"Source:{shadowReport.Source} CreatedAt:{shadowReport.CreatedAt}.")));
                }

                break;

            default:
                throw new NotSupportedException($"{this.GetType().Name} does not have an implementation for {testResult.GetType().Name} type");
            }
        }
Ejemplo n.º 2
0
        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) =>
                        {
                            var testResult = new DirectMethodTestResult(Settings.Current.ModuleId + ".send", DateTime.UtcNow)
                            {
                                TrackingId     = Settings.Current.TrackingId.Expect(() => new ArgumentException("TrackingId is empty")),
                                BatchId        = batchId.ToString(),
                                SequenceNumber = dmCounter.ToString(),
                                Result         = result.ToString()
                            };

                            var testResultReportingClient = new TestResultReportingClient {
                                BaseUrl = uri.AbsoluteUri
                            };
                            await ModuleUtil.ReportTestResultAsync(testResultReportingClient, Logger, testResult);
                        });
                    }
                    else
                    {
                        await analyzerUrl.ForEachAsync(
                            async (Uri uri) =>
                        {
                            var testResult = new LegacyDirectMethodTestResult(Settings.Current.TargetModuleId, DateTime.UtcNow)
                            {
                                Result = result.ToString()
                            };

                            var testResultReportingClient = new TestResultReportingClient {
                                BaseUrl = uri.AbsoluteUri
                            };
                            await ModuleUtil.ReportTestResultAsync(testResultReportingClient, Logger, testResult);
                        },
                            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);
        }