-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Ibis.MutexLeaderElection.Tests/Ibis.MutexLeaderElection.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
Ibis.MutexLeaderElection.Tests/Properties/serviceDependencies.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"dependencies": { | ||
"storage1": { | ||
"type": "storage", | ||
"connectionId": "storage" | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
Ibis.MutexLeaderElection.Tests/Properties/serviceDependencies.local.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"dependencies": { | ||
"storage1": { | ||
"secretStore": null, | ||
"type": "storage.emulator", | ||
"connectionId": "storage" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters