本文整理汇总了C#中ConsulClient.Semaphore方法的典型用法代码示例。如果您正苦于以下问题:C# ConsulClient.Semaphore方法的具体用法?C# ConsulClient.Semaphore怎么用?C# ConsulClient.Semaphore使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConsulClient
的用法示例。
在下文中一共展示了ConsulClient.Semaphore方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Semaphore_BadLimit
public void Semaphore_BadLimit()
{
var client = new ConsulClient();
const string keyName = "test/semaphore/badlimit";
try
{
client.Semaphore(keyName, 0);
}
catch (ArgumentOutOfRangeException ex)
{
Assert.IsType<ArgumentOutOfRangeException>(ex);
}
var semaphore1 = client.Semaphore(keyName, 1);
semaphore1.Acquire(CancellationToken.None);
try
{
var semaphore2 = client.Semaphore(keyName, 2);
semaphore2.Acquire(CancellationToken.None);
}
catch (SemaphoreLimitConflictException ex)
{
Assert.IsType<SemaphoreLimitConflictException>(ex);
Assert.Equal(1, ex.RemoteLimit);
Assert.Equal(2, ex.LocalLimit);
}
try
{
semaphore1.Release();
semaphore1.Destroy();
}
catch (SemaphoreNotHeldException ex)
{
Assert.IsType<SemaphoreNotHeldException>(ex);
}
Assert.False(semaphore1.IsHeld);
}
示例2: Semaphore_AcquireRelease
public void Semaphore_AcquireRelease()
{
var client = new ConsulClient();
const string keyName = "test/semaphore/acquirerelease";
var semaphore = client.Semaphore(keyName, 2);
try
{
semaphore.Release();
}
catch (SemaphoreNotHeldException ex)
{
Assert.IsType<SemaphoreNotHeldException>(ex);
}
semaphore.Acquire(CancellationToken.None);
Assert.True(semaphore.IsHeld);
try
{
semaphore.Acquire(CancellationToken.None);
}
catch (SemaphoreHeldException ex)
{
Assert.IsType<SemaphoreHeldException>(ex);
}
Assert.True(semaphore.IsHeld);
semaphore.Release();
try
{
semaphore.Release();
}
catch (SemaphoreNotHeldException ex)
{
Assert.IsType<SemaphoreNotHeldException>(ex);
}
Assert.False(semaphore.IsHeld);
}
示例3: Semaphore_Conflict
public void Semaphore_Conflict()
{
var client = new ConsulClient();
const string keyName = "test/semaphore/conflict";
var semaphoreLock = client.CreateLock(keyName + "/.lock");
semaphoreLock.Acquire(CancellationToken.None);
Assert.True(semaphoreLock.IsHeld);
var semaphore = client.Semaphore(keyName, 2);
try
{
semaphore.Acquire(CancellationToken.None);
}
catch (SemaphoreConflictException ex)
{
Assert.IsType<SemaphoreConflictException>(ex);
}
try
{
semaphore.Destroy();
}
catch (SemaphoreConflictException ex)
{
Assert.IsType<SemaphoreConflictException>(ex);
}
semaphoreLock.Release();
Assert.False(semaphoreLock.IsHeld);
semaphoreLock.Destroy();
}
示例4: Semaphore_DeleteKey
public async Task Semaphore_DeleteKey()
{
var client = new ConsulClient();
const string keyName = "test/semaphore/deletekey";
var semaphore = (Semaphore)client.Semaphore(keyName, 2);
try
{
semaphore.Acquire(CancellationToken.None);
Assert.True(semaphore.IsHeld);
var checker = Task.Run(() =>
{
while (semaphore.IsHeld)
{
Thread.Sleep(10);
}
Assert.False(semaphore.IsHeld);
});
Task.WaitAny(new[] { checker }, 1000);
var req = await client.KV.DeleteTree(semaphore.Opts.Prefix);
Assert.True(req.Response);
}
finally
{
try
{
semaphore.Release();
}
catch (SemaphoreNotHeldException ex)
{
Assert.IsType<SemaphoreNotHeldException>(ex);
}
}
}
示例5: Semaphore_ForceInvalidate
public void Semaphore_ForceInvalidate()
{
var client = new ConsulClient();
const string keyName = "test/semaphore/forceinvalidate";
var semaphore = (Semaphore)client.Semaphore(keyName, 2);
try
{
semaphore.Acquire(CancellationToken.None);
Assert.True(semaphore.IsHeld);
var checker = Task.Run(() =>
{
while (semaphore.IsHeld)
{
Thread.Sleep(10);
}
Assert.False(semaphore.IsHeld);
});
Task.WaitAny(new[] { checker }, 1000);
client.Session.Destroy(semaphore.LockSession);
}
finally
{
try
{
semaphore.Release();
semaphore.Destroy();
}
catch (SemaphoreNotHeldException ex)
{
Assert.IsType<SemaphoreNotHeldException>(ex);
}
}
}
示例6: Semaphore_Destroy
public void Semaphore_Destroy()
{
var c = new ConsulClient();
const string keyName = "test/semaphore/destroy";
var semaphore1 = c.Semaphore(keyName, 2);
var semaphore2 = c.Semaphore(keyName, 2);
try
{
semaphore1.Acquire(CancellationToken.None);
Assert.True(semaphore1.IsHeld);
semaphore2.Acquire(CancellationToken.None);
Assert.True(semaphore2.IsHeld);
try
{
semaphore1.Destroy();
Assert.True(false);
}
catch (SemaphoreHeldException ex)
{
Assert.IsType<SemaphoreHeldException>(ex);
}
semaphore1.Release();
Assert.False(semaphore1.IsHeld);
try
{
semaphore1.Destroy();
Assert.True(false);
}
catch (SemaphoreInUseException ex)
{
Assert.IsType<SemaphoreInUseException>(ex);
}
semaphore2.Release();
Assert.False(semaphore2.IsHeld);
semaphore1.Destroy();
semaphore2.Destroy();
}
finally
{
try
{
semaphore1.Release();
}
catch (SemaphoreNotHeldException ex)
{
Assert.IsType<SemaphoreNotHeldException>(ex);
}
try
{
semaphore2.Release();
}
catch (SemaphoreNotHeldException ex)
{
Assert.IsType<SemaphoreNotHeldException>(ex);
}
}
}
示例7: Semaphore_ContendFast
public void Semaphore_ContendFast()
{
var client = new ConsulClient();
const string keyName = "test/semaphore/contend";
const int contenderPool = 15;
var acquired = new System.Collections.Concurrent.ConcurrentDictionary<int, bool>();
using (var cts = new CancellationTokenSource())
{
cts.CancelAfter((contenderPool - 1) * (int)Semaphore.DefaultSemaphoreWaitTime.TotalMilliseconds);
Parallel.For(0, contenderPool, new ParallelOptions { MaxDegreeOfParallelism = contenderPool, CancellationToken = cts.Token }, (v) =>
{
var semaphore = client.Semaphore(keyName, 2);
semaphore.Acquire(CancellationToken.None);
acquired[v] = semaphore.IsHeld;
semaphore.Release();
});
}
for (var i = 0; i < contenderPool; i++)
{
if (acquired[i])
{
Assert.True(acquired[i]);
}
else
{
Assert.True(false, "Contender " + i.ToString() + " did not acquire the lock");
}
}
}
示例8: Semaphore_AcquireWaitRelease
public void Semaphore_AcquireWaitRelease()
{
var client = new ConsulClient();
const string keyName = "test/semaphore/acquirewaitrelease";
var semaphoreOptions = new SemaphoreOptions(keyName, 1)
{
SessionName = "test_semaphoresession",
SessionTTL = TimeSpan.FromSeconds(10)
};
var semaphore = client.Semaphore(semaphoreOptions);
semaphore.Acquire(CancellationToken.None);
Assert.True(semaphore.IsHeld);
// Wait for multiple renewal cycles to ensure the semaphore session stays renewed.
Task.Delay(TimeSpan.FromSeconds(60)).Wait();
Assert.True(semaphore.IsHeld);
semaphore.Release();
Assert.False(semaphore.IsHeld);
semaphore.Destroy();
}
示例9: Semaphore_OneShot
public void Semaphore_OneShot()
{
var client = new ConsulClient();
const string keyName = "test/semaphore/oneshot";
var semaphoreOptions = new SemaphoreOptions(keyName, 2)
{
SemaphoreTryOnce = true
};
Assert.Equal(Semaphore.DefaultSemaphoreWaitTime, semaphoreOptions.SemaphoreWaitTime);
semaphoreOptions.SemaphoreWaitTime = TimeSpan.FromMilliseconds(1000);
var semaphorekey = client.Semaphore(semaphoreOptions);
semaphorekey.Acquire(CancellationToken.None);
var another = client.Semaphore(new SemaphoreOptions(keyName, 2)
{
SemaphoreTryOnce = true,
SemaphoreWaitTime = TimeSpan.FromMilliseconds(1000)
});
another.Acquire();
var contender = client.Semaphore(new SemaphoreOptions(keyName, 2)
{
SemaphoreTryOnce = true,
SemaphoreWaitTime = TimeSpan.FromMilliseconds(1000)
});
var stopwatch = Stopwatch.StartNew();
Exception didExcept = null;
Task.WaitAny(
Task.Run(() =>
{
// Needed because async asserts don't work in sync methods!
try
{
contender.Acquire();
}
catch (Exception e)
{
didExcept = e;
}
}),
Task.Delay((int)(2 * semaphoreOptions.SemaphoreWaitTime.TotalMilliseconds)).ContinueWith((t) => Assert.True(false, "Took too long"))
);
Assert.False(contender.IsHeld, "Contender should have failed to acquire");
Assert.False(stopwatch.ElapsedMilliseconds < semaphoreOptions.SemaphoreWaitTime.TotalMilliseconds);
Assert.IsType<SemaphoreMaxAttemptsReachedException>(didExcept);
semaphorekey.Release();
another.Release();
contender.Destroy();
}
示例10: Lock_SemaphoreConflict
public void Lock_SemaphoreConflict()
{
var client = new ConsulClient();
const string keyName = "test/lock/semaphoreconflict";
var semaphore = client.Semaphore(keyName, 2);
semaphore.Acquire(CancellationToken.None);
Assert.True(semaphore.IsHeld);
var lockKey = client.CreateLock(keyName + "/.lock");
try
{
lockKey.Acquire(CancellationToken.None);
}
catch (LockConflictException ex)
{
Assert.IsType<LockConflictException>(ex);
}
try
{
lockKey.Destroy();
}
catch (LockConflictException ex)
{
Assert.IsType<LockConflictException>(ex);
}
semaphore.Release();
semaphore.Destroy();
}
示例11: Semaphore_OneShot
public void Semaphore_OneShot()
{
var client = new ConsulClient();
const string keyName = "test/semaphore/oneshot";
var semaphoreOptions = new SemaphoreOptions(keyName, 2)
{
SemaphoreTryOnce = true
};
Assert.Equal(Semaphore.DefaultSemaphoreWaitTime, semaphoreOptions.SemaphoreWaitTime);
semaphoreOptions.SemaphoreWaitTime = TimeSpan.FromMilliseconds(250);
var semaphorekey = client.Semaphore(semaphoreOptions);
semaphorekey.Acquire(CancellationToken.None);
var another = client.Semaphore(new SemaphoreOptions(keyName, 2)
{
SemaphoreTryOnce = true,
SemaphoreWaitTime = TimeSpan.FromMilliseconds(250)
});
another.Acquire();
var contender = client.Semaphore(new SemaphoreOptions(keyName, 2)
{
SemaphoreTryOnce = true,
SemaphoreWaitTime = TimeSpan.FromMilliseconds(250)
});
Task.WaitAny(Task.Run(() =>
{
Assert.Throws<LockMaxAttemptsReachedException>(() =>
contender.Acquire()
);
}),
Task.Delay(2 * semaphoreOptions.SemaphoreWaitTime.Milliseconds).ContinueWith((t) => Assert.True(false, "Took too long"))
);
semaphorekey.Release();
contender.Acquire();
contender.Release();
another.Release();
contender.Destroy();
}