public async Task TestLockFunctions()
    {
        // Create a locker and use a 10ms to 20ms delay
        var locker = new ConcurrentDictionaryExclusiveAccessLock();

        // Try the check function which will return false initially
        const string lockName = "my_lock";
        var          result   = await locker.IsLockAcquiredAsync(lockName, CancellationToken.None);

        Assert.That(result, Is.False);

        // Get the lock once
        result = await locker.AcquireLockAsync(lockName, CancellationToken.None);

        Assert.That(result, Is.True);

        // Try to get it again, and it should fail
        result = await locker.AcquireLockAsync(lockName, CancellationToken.None);

        Assert.That(result, Is.False);

        // Try the check function
        result = await locker.IsLockAcquiredAsync(lockName, CancellationToken.None);

        Assert.That(result, Is.True);

        // Now release the lock
        await locker.ReleaseLockAsync(lockName);

        // Try the check function and it should be false now
        result = await locker.IsLockAcquiredAsync(lockName, CancellationToken.None);

        Assert.That(result, Is.False);
    }
Ejemplo n.º 2
0
    static IEnumerable <IIncomingStep> GetSteps()
    {
        var handler    = new ConcurrentDictionaryExclusiveAccessLock();
        var lockPrefix = "lock_";

        yield return(new EnforceExclusiveSagaAccessIncomingStep(handler, 10, lockPrefix, CancellationToken.None));

        yield return(new SemaphoreSlimExclusiveSagaAccessIncomingStep(10, CancellationToken.None));

        yield return(new EnforceExclusiveSagaAccessIncomingStep(handler, 20, lockPrefix, CancellationToken.None));

        yield return(new SemaphoreSlimExclusiveSagaAccessIncomingStep(20, CancellationToken.None));

        yield return(new EnforceExclusiveSagaAccessIncomingStep(handler, 50, lockPrefix, CancellationToken.None));

        yield return(new SemaphoreSlimExclusiveSagaAccessIncomingStep(50, CancellationToken.None));

        yield return(new EnforceExclusiveSagaAccessIncomingStep(handler, 100, lockPrefix, CancellationToken.None));

        yield return(new SemaphoreSlimExclusiveSagaAccessIncomingStep(100, CancellationToken.None));
    }