using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using Tango.BL.Enumerations; using Tango.Logging; using Tango.Telemetry; namespace Tango.TelemetryTester.CLI { class Program { static void Main(string[] args) { Run().GetAwaiter().GetResult(); } static async Task Run() { Logger.LogTitle("Telemetry Pipeline Test Starting..."); DatabaseHelper.ClearTelemetryDatabase(); LogManager.Default.NewLog += (x, log) => { switch (log.Category) { case LogCategory.Info: case LogCategory.Debug: Logger.LogVerboseByLogManager(log.Message); break; case LogCategory.Warning: Logger.LogWarning(log.Message); break; case LogCategory.Error: case LogCategory.Critical: Logger.LogError(log.Message); break; } }; var config = new TelemetryPublisherConfiguration { SerialNumber = "TEST-MACHINE", MachineType = MachineTypes.TS1800, Environment = "DEV", PendingStorageCheckInterval = TimeSpan.FromSeconds(5), HistorySourcesRequestInterval = TimeSpan.FromSeconds(5) }; var recoveryClient = new MockCheckpointsRecoveryClient(); var publisher = new TelemetryPublisher(config, recoveryClient); var mockStreamingSource = new MockStreamingSource("MockStreamingSource1"); var mockHistorySource = new MockHistorySource("MockHistorySource1"); var destination1 = new MockDestinationWithFailure("MockDestination", failCount: 2); var destination2 = new MockDestinationWithFailure("FastDestination", failCount: 0); var destination3 = new MockDestinationWithFailure("HistoryOnlyDestination", failCount: 0); publisher.RegisterSource(mockStreamingSource); publisher.RegisterSource(mockHistorySource); publisher.RegisterDestination(destination1); publisher.RegisterDestination(destination2); publisher.RegisterDestination(destination3); var results = new List(); var publishCount = 0; var failureCount = 0; publisher.PackagePublished += (s, e) => { publishCount++; var msg = $"SUCCESS: published telemetry to {e.Destination.Name}"; results.Add(msg); Logger.LogSuccess(msg); }; publisher.PublishPackageFailed += (s, e) => { failureCount++; var msg = $"FAILURE: failed to publish telemetry to {e.Destination.Name} - {e.Exception.Message}"; results.Add(msg); Logger.LogError(msg); }; await publisher.Start(); await Task.Delay(7000); await publisher.Stop(); int pendingCount = publisher.StorageManager.GetPendingTelemetriesCount(); Logger.LogTitle("TEST REPORT"); Logger.LogInfo($"Total telemetry generated by streaming source: {mockStreamingSource.EmittedCount}"); Logger.LogInfo($"Total telemetry generated by history source: {mockHistorySource.ProvidedCount}"); Logger.LogInfo($"Total failures (intentional): {failureCount}"); Logger.LogInfo($"Total payloads received by MockDestination: {destination1.ReceivedPayloads.Count}"); Logger.LogInfo($"Total payloads received by FastDestination: {destination2.ReceivedPayloads.Count}"); Logger.LogInfo($"Total payloads received by HistoryOnlyDestination: {destination3.ReceivedPayloads.Count}"); Logger.LogInfo($"Total telemetries published: {publishCount}"); int totalExpectedPublishes = (mockStreamingSource.EmittedCount + mockHistorySource.ProvidedCount) * 3; bool allEmittedPublished = publishCount == totalExpectedPublishes; bool allPublishedReceived = publishCount == destination1.ReceivedPayloads.Count + destination2.ReceivedPayloads.Count + destination3.ReceivedPayloads.Count; bool retriesOccurred = destination1.TotalAttempts > destination1.ReceivedPayloads.Count; bool noPendingLeft = pendingCount == 0; Logger.LogTitle("TEST VERDICT"); Logger.LogInfo(allEmittedPublished ? "PASS: Emitted telemetry all published" : "FAIL: Emitted telemetry not fully published"); Logger.LogInfo(allPublishedReceived ? "PASS: All published telemetry received by destinations" : "FAIL: Some published telemetry was not received"); Logger.LogInfo(retriesOccurred ? "PASS: Retry logic triggered and succeeded" : "FAIL: Retry logic did not activate as expected"); Logger.LogInfo(noPendingLeft ? "PASS: Pending storage clean after test" : $"FAIL: {pendingCount} telemetry still pending in storage"); Logger.LogTitle("DETAILED EVENTS"); foreach (var line in results) Logger.LogInfo(line); Logger.LogSuccess("\nTest complete."); Console.ReadKey(); } } }