aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.TelemetryTester.CLI/Program.cs
blob: 1b985e35bf2d937c0b4b9f52d254e854948845f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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<string>();
            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();
        }
    }
}