diff --git a/Tests/CodexTests/UtilityTests/DiscordBotTests.cs b/Tests/CodexTests/UtilityTests/DiscordBotTests.cs index 66601c8..01c8fc5 100644 --- a/Tests/CodexTests/UtilityTests/DiscordBotTests.cs +++ b/Tests/CodexTests/UtilityTests/DiscordBotTests.cs @@ -7,6 +7,7 @@ using DiscordRewards; using DistTestCore; using GethPlugin; using KubernetesWorkflow.Types; +using Logging; using Newtonsoft.Json; using NUnit.Framework; using Utils; @@ -38,25 +39,37 @@ namespace CodexTests.UtilityTests StartHosts(geth, contracts); var client = StartClient(geth, contracts); - var events = ChainEvents.FromTimeRange(contracts, GetTestRunTimeRange()); - var chainState = new ChainState(GetTestLog(), contracts, new DoNothingChainEventHandler(), GetTestRunTimeRange().From); + //var chainState = new ChainState(GetTestLog(), contracts, new DoNothingChainEventHandler(), GetTestRunTimeRange().From); - var apiCalls = new RewardApiCalls(Ci, botContainer); + //var running = true; + //var task = Task.Run(() => + //{ + // while (running) + // { + // Thread.Sleep(TimeSpan.FromMinutes(1)); + // chainState.Update(); + // } + //}); + + var apiCalls = new RewardApiCalls(GetTestLog(), Ci, botContainer); apiCalls.Start(OnCommand); var purchaseContract = ClientPurchasesStorage(client); - chainState.Update(); - Assert.That(chainState.Requests.Length, Is.EqualTo(1)); + //chainState.Update(); + //Assert.That(chainState.Requests.Length, Is.EqualTo(1)); purchaseContract.WaitForStorageContractStarted(); - chainState.Update(); + //chainState.Update(); purchaseContract.WaitForStorageContractFinished(); Thread.Sleep(rewarderInterval * 3); - + + //running = false; + //task.Wait(); + apiCalls.Stop(); - chainState.Update(); + //chainState.Update(); foreach (var r in repo.Rewards) { @@ -74,9 +87,9 @@ namespace CodexTests.UtilityTests return $"({rewardId})'{reward.Message}'"; } - private void OnCommand(GiveRewardsCommand call) + private void OnCommand(string timestamp, GiveRewardsCommand call) { - Log($""); + Log($""); foreach (var a in call.Averages) { Log("\tAverage: " + JsonConvert.SerializeObject(a)); @@ -249,14 +262,13 @@ namespace CodexTests.UtilityTests public class RewardApiCalls { private readonly ContainerFileMonitor monitor; - private readonly Dictionary commands = new Dictionary(); - public RewardApiCalls(CoreInterface ci, RunningContainer botContainer) + public RewardApiCalls(ILog log, CoreInterface ci, RunningContainer botContainer) { - monitor = new ContainerFileMonitor(ci, botContainer, "/app/datapath/logs/discordbot.log"); + monitor = new ContainerFileMonitor(log, ci, botContainer, "/app/datapath/logs/discordbot.log"); } - public void Start(Action onCommand) + public void Start(Action onCommand) { monitor.Start(line => ParseLine(line, onCommand)); } @@ -266,19 +278,17 @@ namespace CodexTests.UtilityTests monitor.Stop(); } - private void ParseLine(string line, Action onCommand) + private void ParseLine(string line, Action onCommand) { try { var timestamp = line.Substring(0, 30); - if (commands.ContainsKey(timestamp)) return; var json = line.Substring(31); var cmd = JsonConvert.DeserializeObject(json); if (cmd != null) { - commands.Add(timestamp, cmd); - onCommand(cmd); + onCommand(timestamp, cmd); } } catch @@ -289,6 +299,7 @@ namespace CodexTests.UtilityTests public class ContainerFileMonitor { + private readonly ILog log; private readonly CoreInterface ci; private readonly RunningContainer botContainer; private readonly string filePath; @@ -297,8 +308,9 @@ namespace CodexTests.UtilityTests private Task worker = Task.CompletedTask; private Action onNewLine = c => { }; - public ContainerFileMonitor(CoreInterface ci, RunningContainer botContainer, string filePath) + public ContainerFileMonitor(ILog log, CoreInterface ci, RunningContainer botContainer, string filePath) { + this.log = log; this.ci = ci; this.botContainer = botContainer; this.filePath = filePath; @@ -316,6 +328,9 @@ namespace CodexTests.UtilityTests worker.Wait(); } + // did any container crash? that's why it repeats? + + private void Worker() { while (!cts.IsCancellationRequested) @@ -333,6 +348,8 @@ namespace CodexTests.UtilityTests var lines = botLog.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { + // log.Log("line: " + line); + if (!seenLines.Contains(line)) { seenLines.Add(line); diff --git a/Tools/TestNetRewarder/BufferLogger.cs b/Tools/TestNetRewarder/BufferLogger.cs index f7f9925..1f32332 100644 --- a/Tools/TestNetRewarder/BufferLogger.cs +++ b/Tools/TestNetRewarder/BufferLogger.cs @@ -19,19 +19,16 @@ namespace TestNetRewarder public void Debug(string message = "", int skipFrames = 0) { lines.Add(message); - Trim(); } public void Error(string message) { lines.Add($"Error: {message}"); - Trim(); } public void Log(string message) { lines.Add(message); - Trim(); } public string[] Get() @@ -40,13 +37,5 @@ namespace TestNetRewarder lines.Clear(); return result; } - - private void Trim() - { - if (lines.Count > 100) - { - lines.RemoveRange(0, 50); - } - } } } diff --git a/Tools/TestNetRewarder/Processor.cs b/Tools/TestNetRewarder/Processor.cs index b30ca79..f43a924 100644 --- a/Tools/TestNetRewarder/Processor.cs +++ b/Tools/TestNetRewarder/Processor.cs @@ -50,15 +50,18 @@ namespace TestNetRewarder } catch (Exception ex) { - log.Error("Exception processing time segment: " + ex); + var msg = "Exception processing time segment: " + ex; + log.Error(msg); + bufferLogger.Error(msg); throw; } } private string[] RemoveFirstLine(string[] lines) { - if (!lines.Any()) return Array.Empty(); - return lines.Skip(1).ToArray(); + //if (!lines.Any()) return Array.Empty(); + //return lines.Skip(1).ToArray(); + return lines; } } }