Skip to content

Commit

Permalink
Added basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Expecho committed Oct 22, 2024
1 parent 6270956 commit c3dd58f
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Ibis.MutexLeaderElection\Ibis.MutexLeaderElection.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
63 changes: 63 additions & 0 deletions Ibis.MutexLeaderElection.Tests/LeaderElectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Microsoft.Extensions.Logging.Abstractions;

namespace Ibis.MutexLeaderElection.Tests
{
public class LeaderElectionTests
{
[Fact]
public async Task SingleCandidateShouldBecomeElected()
{
var distributedLock = new DistributedLock(new NullLogger<DistributedLock>(), new DistributedLockOptions
{
StorageConnectionString = "UseDevelopmentStorage=true",
StorageBlobName = Guid.NewGuid().ToString()
});
var leaderElection = new LeaderElection(new NullLogger<LeaderElection>(), distributedLock);
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
var isSelectedLeader = false;

await leaderElection.RunTaskWhenElectedLeaderAsync(async _ =>
{
isSelectedLeader = true;
cts.Cancel();
}, cts.Token);

Assert.True(isSelectedLeader);
}

[Fact]
public async Task OnlyOneCandidateShouldBecomeElected()
{
var distributedLock = new DistributedLock(new NullLogger<DistributedLock>(), new DistributedLockOptions
{
StorageConnectionString = "UseDevelopmentStorage=true",
StorageBlobName = Guid.NewGuid().ToString()
});
var leaderElectionOne = new LeaderElection(new NullLogger<LeaderElection>(), distributedLock);
var leaderElectionTwo = new LeaderElection(new NullLogger<LeaderElection>(), distributedLock);

// Fail if no leader is elected in 2 seconds
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));

var leaderOneTask = leaderElectionOne.RunTaskWhenElectedLeaderAsync(EndlessLoop, cts.Token);
var leaderTwoTask = leaderElectionTwo.RunTaskWhenElectedLeaderAsync(EndlessLoop, cts.Token);

// Give the system some time to elect a leader
await Task.Delay(TimeSpan.FromMilliseconds(200));

Assert.True(leaderElectionOne.IsLeader ^ leaderElectionTwo.IsLeader);

cts.Cancel();

await Task.WhenAll(leaderOneTask, leaderTwoTask);

static async Task EndlessLoop(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies": {
"storage1": {
"type": "storage",
"connectionId": "storage"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"dependencies": {
"storage1": {
"secretStore": null,
"type": "storage.emulator",
"connectionId": "storage"
}
}
}
10 changes: 8 additions & 2 deletions Ibis.MutexLeaderElection.sln
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31424.327
# 17
VisualStudioVersion = 17.11.35327.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ibis.MutexLeaderElection.Sample", "Ibis.MutexLeaderElection.Sample\Ibis.MutexLeaderElection.Sample.csproj", "{58161D71-782E-4F0F-92E8-C98227E2708B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ibis.MutexLeaderElection", "Ibis.MutexLeaderElection\Ibis.MutexLeaderElection.csproj", "{27796B85-703E-47BA-9B36-E5D25B04241E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ibis.MutexLeaderElection.Tests", "Ibis.MutexLeaderElection.Tests\Ibis.MutexLeaderElection.Tests.csproj", "{2BD98A6E-3800-43FC-95CB-2E9D6F34764D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{27796B85-703E-47BA-9B36-E5D25B04241E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{27796B85-703E-47BA-9B36-E5D25B04241E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{27796B85-703E-47BA-9B36-E5D25B04241E}.Release|Any CPU.Build.0 = Release|Any CPU
{2BD98A6E-3800-43FC-95CB-2E9D6F34764D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2BD98A6E-3800-43FC-95CB-2E9D6F34764D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2BD98A6E-3800-43FC-95CB-2E9D6F34764D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2BD98A6E-3800-43FC-95CB-2E9D6F34764D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit c3dd58f

Please sign in to comment.