From cd798501e84ca8b440f16be4441641c28ec746d5 Mon Sep 17 00:00:00 2001 From: Guillaume Chervet Date: Thu, 22 Jun 2023 17:50:30 +0200 Subject: [PATCH] refactor(slimfaas): add unit test for logging exceptions fix --- .../HistorySynchronizationWorkerShould.cs | 25 +++++++++++++++- tests/SlimFaas.Tests/MasterWorkerShould.cs | 22 +++++++++++++- .../ReplicasScaleWorkerShould.cs | 26 +++++++++++++++++ .../ReplicasSynchronizationWorkerShould.cs | 25 ++++++++++++++++ tests/SlimFaas.Tests/SlimWorkerShould.cs | 29 +++++++++++++++++++ 5 files changed, 125 insertions(+), 2 deletions(-) diff --git a/tests/SlimFaas.Tests/HistorySynchronizationWorkerShould.cs b/tests/SlimFaas.Tests/HistorySynchronizationWorkerShould.cs index 9792a674..2dbace7e 100644 --- a/tests/SlimFaas.Tests/HistorySynchronizationWorkerShould.cs +++ b/tests/SlimFaas.Tests/HistorySynchronizationWorkerShould.cs @@ -6,7 +6,7 @@ namespace SlimFaas.Tests; public class HistorySynchronizationWorkerShould { [Fact] - public async Task SynLastTicksBetweenDatabaseAndMemory() + public async Task SyncLastTicksBetweenDatabaseAndMemory() { var logger = new Mock>(); var redisMockService = new RedisMockService(); @@ -42,4 +42,27 @@ public async Task SynLastTicksBetweenDatabaseAndMemory() Assert.True(task.IsCompleted); } + + [Fact] + public async Task LogErrorWhenExceptionIsThrown() + { + var logger = new Mock>(); + var redisMockService = new RedisMockService(); + var historyHttpRedisService = new HistoryHttpRedisService(redisMockService); + var historyHttpMemoryService = new HistoryHttpMemoryService(); + var replicasService = new Mock(); + replicasService.Setup(r => r.Deployments).Throws(new Exception()); + var service = new HistorySynchronizationWorker(replicasService.Object, historyHttpMemoryService, historyHttpRedisService, logger.Object, 10); + + var task = service.StartAsync(CancellationToken.None); + await Task.Delay(100); + + logger.Verify(l => l.Log( + LogLevel.Error, + It.IsAny(), + It.IsAny(), + It.IsAny(), + (Func) It.IsAny()), Times.AtLeastOnce); + Assert.True(task.IsCompleted); + } } \ No newline at end of file diff --git a/tests/SlimFaas.Tests/MasterWorkerShould.cs b/tests/SlimFaas.Tests/MasterWorkerShould.cs index 4d02c6ed..ac39d919 100644 --- a/tests/SlimFaas.Tests/MasterWorkerShould.cs +++ b/tests/SlimFaas.Tests/MasterWorkerShould.cs @@ -5,7 +5,7 @@ namespace SlimFaas.Tests; public class MasterWorkerShould { - [Fact] + [Fact] public async Task BecomeTheMaster() { var logger = new Mock>(); @@ -19,4 +19,24 @@ public async Task BecomeTheMaster() Assert.True(task.IsCompleted); } + + [Fact] + public async Task LogErrorWhenExceptionIsThrown() + { + var logger = new Mock>(); + var redisMockService = new Mock(); + redisMockService.Setup(r => r.CheckAsync()).Throws(new Exception()); + var service = new MasterWorker(redisMockService.Object, logger.Object, 10); + + var task = service.StartAsync(CancellationToken.None); + + await Task.Delay(100); + logger.Verify(l => l.Log( + LogLevel.Error, + It.IsAny(), + It.IsAny(), + It.IsAny(), + (Func) It.IsAny()), Times.AtLeastOnce); + Assert.True(task.IsCompleted); + } } \ No newline at end of file diff --git a/tests/SlimFaas.Tests/ReplicasScaleWorkerShould.cs b/tests/SlimFaas.Tests/ReplicasScaleWorkerShould.cs index 5f918fce..1858af94 100644 --- a/tests/SlimFaas.Tests/ReplicasScaleWorkerShould.cs +++ b/tests/SlimFaas.Tests/ReplicasScaleWorkerShould.cs @@ -32,4 +32,30 @@ public async Task ScaleFunctionUpAndDown(DeploymentsInformations deploymentsInfo Assert.True(task.IsCompleted); } + + [Fact] + public async Task LogErrorWhenExceptionIsThrown() + { + var logger = new Mock>(); + var masterService = new Mock(); + masterService.Setup(ms => ms.IsMaster).Returns(true); + var replicaService = new Mock(); + replicaService.Setup(r => r.CheckScaleAsync(It.IsAny())).Throws(new Exception()); + + var historyHttpService = new HistoryHttpMemoryService(); + historyHttpService.SetTickLastCall("fibonacci2", DateTime.Now.Ticks); + + var service = new ScaleReplicasWorker(replicaService.Object, masterService.Object, logger.Object, 10); + var task = service.StartAsync(CancellationToken.None); + await Task.Delay(100); + + logger.Verify(l => l.Log( + LogLevel.Error, + It.IsAny(), + It.IsAny(), + It.IsAny(), + (Func) It.IsAny()), Times.AtLeastOnce); + + Assert.True(task.IsCompleted); + } } \ No newline at end of file diff --git a/tests/SlimFaas.Tests/ReplicasSynchronizationWorkerShould.cs b/tests/SlimFaas.Tests/ReplicasSynchronizationWorkerShould.cs index 588bd5f5..c50d9b10 100644 --- a/tests/SlimFaas.Tests/ReplicasSynchronizationWorkerShould.cs +++ b/tests/SlimFaas.Tests/ReplicasSynchronizationWorkerShould.cs @@ -47,4 +47,29 @@ public async Task SynchroniseDeployments(DeploymentsInformations deploymentsInfo kubernetesService.Verify(v => v.ListFunctionsAsync(It.IsAny())); } + [Fact] + public async Task LogErrorWhenExceptionIsThrown() + { + var logger = new Mock>(); + var kubernetesService = new Mock(); + kubernetesService.Setup(k => k.ListFunctionsAsync(It.IsAny())).Throws(new Exception()); + var masterService = new Mock(); + var historyHttpService = new HistoryHttpMemoryService(); + var replicasService = new ReplicasService(kubernetesService.Object, historyHttpService); + masterService.Setup(ms => ms.IsMaster).Returns(true); + + var service = new ReplicasSynchronizationWorker(replicasService, logger.Object, 10); + var task = service.StartAsync(CancellationToken.None); + await Task.Delay(100); + + logger.Verify(l => l.Log( + LogLevel.Error, + It.IsAny(), + It.IsAny(), + It.IsAny(), + (Func) It.IsAny()), Times.AtLeastOnce); + + Assert.True(task.IsCompleted); + } + } \ No newline at end of file diff --git a/tests/SlimFaas.Tests/SlimWorkerShould.cs b/tests/SlimFaas.Tests/SlimWorkerShould.cs index 031fe5d5..426c8643 100644 --- a/tests/SlimFaas.Tests/SlimWorkerShould.cs +++ b/tests/SlimFaas.Tests/SlimWorkerShould.cs @@ -80,4 +80,33 @@ public async Task OnlyCallOneFunctionAsync() Assert.True(task.IsCompleted); sendClientMock.Verify(v => v.SendHttpRequestAsync(It.IsAny(), It.IsAny()), Times.Once()); } + + [Fact] + public async Task LogErrorWhenExceptionIsThrown() + { + var serviceProvider = new Mock(); + var replicasService = new Mock(); + replicasService.Setup(rs => rs.Deployments).Throws(new Exception()); + var historyHttpService = new HistoryHttpMemoryService(); + var logger = new Mock>(); + var redisQueue = new RedisQueue(new RedisMockService()); + + var service = new SlimWorker(redisQueue, + replicasService.Object, + historyHttpService, + logger.Object, + serviceProvider.Object); + + var task = service.StartAsync(CancellationToken.None); + + await Task.Delay(100); + logger.Verify(l => l.Log( + LogLevel.Error, + It.IsAny(), + It.IsAny(), + It.IsAny(), + (Func) It.IsAny()), Times.AtLeastOnce); + + Assert.True(task.IsCompleted); + } } \ No newline at end of file