Last active
February 28, 2021 08:09
-
-
Save ankitvijay/6d8fbc5f887489b10a5553a09d800798 to your computer and use it in GitHub Desktop.
Distributed Lock TestsDistributed Lock Tests
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
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.Extensions.Logging.Abstractions; | |
using Xunit; | |
using Xunit.Abstractions; | |
namespace PostgreSQLDistributedLock.Tests | |
{ | |
public class DistributedLockTests | |
{ | |
private readonly ITestOutputHelper _testOutputHelper; | |
private readonly string _connectionString; | |
public DistributedLockTests(ITestOutputHelper testOutputHelper) | |
{ | |
_testOutputHelper = testOutputHelper; | |
_connectionString = "Host=localhost;Username=dbUser;Password='password';Database=distributed-lock-db;"; | |
} | |
[Fact] | |
public void DistributedLockIsAcquiredSuccessfully() | |
{ | |
async Task ExclusiveLockTask(int node) | |
{ | |
_testOutputHelper.WriteLine($"Executing a long running task on Node {node}"); | |
// Add 5 second delay | |
await Task.Delay(5000); | |
} | |
const long lockId = 50000; | |
// Simulate with 5 nodes | |
var nodes = Enumerable.Range(1, 5).ToList(); | |
Parallel.ForEach(nodes, async node => | |
{ | |
// Act and Arrange | |
_testOutputHelper.WriteLine($"Trying to acquire session lock and run task for Node {node}"); | |
using var distributedLock = new DistributedLock(_connectionString, NullLogger<DistributedLock>.Instance); | |
if (!await distributedLock.TryExecuteInDistributedLock(lockId, () => ExclusiveLockTask(node))) | |
{ | |
_testOutputHelper.WriteLine($"Node {node} could not acquire lock"); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment