Skip to content

Commit

Permalink
refactor(slimfaas): add unit test for logging exceptions
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
guillaume-chervet committed Jun 22, 2023
1 parent ef9de19 commit cd79850
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 2 deletions.
25 changes: 24 additions & 1 deletion tests/SlimFaas.Tests/HistorySynchronizationWorkerShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace SlimFaas.Tests;
public class HistorySynchronizationWorkerShould
{
[Fact]
public async Task SynLastTicksBetweenDatabaseAndMemory()
public async Task SyncLastTicksBetweenDatabaseAndMemory()
{
var logger = new Mock<ILogger<HistorySynchronizationWorker>>();
var redisMockService = new RedisMockService();
Expand Down Expand Up @@ -42,4 +42,27 @@ public async Task SynLastTicksBetweenDatabaseAndMemory()

Assert.True(task.IsCompleted);
}

[Fact]
public async Task LogErrorWhenExceptionIsThrown()
{
var logger = new Mock<ILogger<HistorySynchronizationWorker>>();
var redisMockService = new RedisMockService();
var historyHttpRedisService = new HistoryHttpRedisService(redisMockService);
var historyHttpMemoryService = new HistoryHttpMemoryService();
var replicasService = new Mock<IReplicasService>();
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<EventId>(),
It.IsAny<It.IsAnyType>(),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>) It.IsAny<object>()), Times.AtLeastOnce);
Assert.True(task.IsCompleted);
}
}
22 changes: 21 additions & 1 deletion tests/SlimFaas.Tests/MasterWorkerShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace SlimFaas.Tests;

public class MasterWorkerShould
{
[Fact]
[Fact]
public async Task BecomeTheMaster()
{
var logger = new Mock<ILogger<MasterWorker>>();
Expand All @@ -19,4 +19,24 @@ public async Task BecomeTheMaster()

Assert.True(task.IsCompleted);
}

[Fact]
public async Task LogErrorWhenExceptionIsThrown()
{
var logger = new Mock<ILogger<MasterWorker>>();
var redisMockService = new Mock<IMasterService>();
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<EventId>(),
It.IsAny<It.IsAnyType>(),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>) It.IsAny<object>()), Times.AtLeastOnce);
Assert.True(task.IsCompleted);
}
}
26 changes: 26 additions & 0 deletions tests/SlimFaas.Tests/ReplicasScaleWorkerShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,30 @@ public async Task ScaleFunctionUpAndDown(DeploymentsInformations deploymentsInfo

Assert.True(task.IsCompleted);
}

[Fact]
public async Task LogErrorWhenExceptionIsThrown()
{
var logger = new Mock<ILogger<ScaleReplicasWorker>>();
var masterService = new Mock<IMasterService>();
masterService.Setup(ms => ms.IsMaster).Returns(true);
var replicaService = new Mock<IReplicasService>();
replicaService.Setup(r => r.CheckScaleAsync(It.IsAny<string>())).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<EventId>(),
It.IsAny<It.IsAnyType>(),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>) It.IsAny<object>()), Times.AtLeastOnce);

Assert.True(task.IsCompleted);
}
}
25 changes: 25 additions & 0 deletions tests/SlimFaas.Tests/ReplicasSynchronizationWorkerShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,29 @@ public async Task SynchroniseDeployments(DeploymentsInformations deploymentsInfo
kubernetesService.Verify(v => v.ListFunctionsAsync(It.IsAny<string>()));
}

[Fact]
public async Task LogErrorWhenExceptionIsThrown()
{
var logger = new Mock<ILogger<ReplicasSynchronizationWorker>>();
var kubernetesService = new Mock<IKubernetesService>();
kubernetesService.Setup(k => k.ListFunctionsAsync(It.IsAny<string>())).Throws(new Exception());
var masterService = new Mock<IMasterService>();
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<EventId>(),
It.IsAny<It.IsAnyType>(),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>) It.IsAny<object>()), Times.AtLeastOnce);

Assert.True(task.IsCompleted);
}

}
29 changes: 29 additions & 0 deletions tests/SlimFaas.Tests/SlimWorkerShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,33 @@ public async Task OnlyCallOneFunctionAsync()
Assert.True(task.IsCompleted);
sendClientMock.Verify(v => v.SendHttpRequestAsync(It.IsAny<CustomRequest>(), It.IsAny<HttpContext>()), Times.Once());
}

[Fact]
public async Task LogErrorWhenExceptionIsThrown()
{
var serviceProvider = new Mock<IServiceProvider>();
var replicasService = new Mock<IReplicasService>();
replicasService.Setup(rs => rs.Deployments).Throws(new Exception());
var historyHttpService = new HistoryHttpMemoryService();
var logger = new Mock<ILogger<SlimWorker>>();
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<EventId>(),
It.IsAny<It.IsAnyType>(),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>) It.IsAny<object>()), Times.AtLeastOnce);

Assert.True(task.IsCompleted);
}
}

0 comments on commit cd79850

Please sign in to comment.