本文整理汇总了C#中Client.Semaphore方法的典型用法代码示例。如果您正苦于以下问题:C# Client.Semaphore方法的具体用法?C# Client.Semaphore怎么用?C# Client.Semaphore使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Client
的用法示例。
在下文中一共展示了Client.Semaphore方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Semaphore_BadLimit
public void Semaphore_BadLimit()
{
var client = new Client();
const string keyName = "test/semaphore/badlimit";
try
{
client.Semaphore(keyName, 0);
}
catch (ArgumentOutOfRangeException ex)
{
Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException));
}
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.IsInstanceOfType(ex, typeof(SemaphoreLimitConflictException));
Assert.AreEqual(1, ex.RemoteLimit);
Assert.AreEqual(2, ex.LocalLimit);
}
try
{
semaphore1.Release();
semaphore1.Destroy();
}
catch (SemaphoreNotHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(SemaphoreNotHeldException));
}
Assert.IsFalse(semaphore1.IsHeld);
}
示例2: Semaphore_AcquireRelease
public void Semaphore_AcquireRelease()
{
var client = new Client();
const string keyName = "test/semaphore/acquirerelease";
var semaphore = client.Semaphore(keyName, 2);
try
{
semaphore.Release();
}
catch (SemaphoreNotHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(SemaphoreNotHeldException));
}
semaphore.Acquire(CancellationToken.None);
Assert.IsTrue(semaphore.IsHeld);
try
{
semaphore.Acquire(CancellationToken.None);
}
catch (SemaphoreHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(SemaphoreHeldException));
}
Assert.IsTrue(semaphore.IsHeld);
semaphore.Release();
try
{
semaphore.Release();
}
catch (SemaphoreNotHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(SemaphoreNotHeldException));
}
Assert.IsFalse(semaphore.IsHeld);
}
示例3: Lock_SemaphoreConflict
public void Lock_SemaphoreConflict()
{
var client = new Client();
const string keyName = "test/lock/semaphoreconflict";
var semaphore = client.Semaphore(keyName, 2);
semaphore.Acquire(CancellationToken.None);
Assert.IsTrue(semaphore.IsHeld);
var lockKey = client.CreateLock(keyName + "/.lock");
try
{
lockKey.Acquire(CancellationToken.None);
}
catch (LockConflictException ex)
{
Assert.IsInstanceOfType(ex, typeof(LockConflictException));
}
try
{
lockKey.Destroy();
}
catch (LockConflictException ex)
{
Assert.IsInstanceOfType(ex, typeof(LockConflictException));
}
semaphore.Release();
semaphore.Destroy();
}
示例4: Semaphore_DeleteKey
public void Semaphore_DeleteKey()
{
var client = new Client();
const string keyName = "test/semaphore/deletekey";
var semaphore = (Semaphore)client.Semaphore(keyName, 2);
try
{
semaphore.Acquire(CancellationToken.None);
Assert.IsTrue(semaphore.IsHeld);
var checker = Task.Run(() =>
{
while (semaphore.IsHeld)
{
Thread.Sleep(10);
}
Assert.IsFalse(semaphore.IsHeld);
});
Task.WaitAny(new[] { checker }, 1000);
var req = client.KV.DeleteTree(semaphore.Opts.Prefix);
Assert.IsTrue(req.Response);
}
finally
{
try
{
semaphore.Release();
}
catch (SemaphoreNotHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(SemaphoreNotHeldException));
}
}
}
示例5: Semaphore_ForceInvalidate
public void Semaphore_ForceInvalidate()
{
var client = new Client();
const string keyName = "test/semaphore/forceinvalidate";
var semaphore = (Semaphore)client.Semaphore(keyName, 2);
try
{
semaphore.Acquire(CancellationToken.None);
Assert.IsTrue(semaphore.IsHeld);
var checker = Task.Run(() =>
{
while (semaphore.IsHeld)
{
Thread.Sleep(10);
}
Assert.IsFalse(semaphore.IsHeld);
});
Task.WaitAny(new[] { checker }, 1000);
client.Session.Destroy(semaphore.LockSession);
}
finally
{
try
{
semaphore.Release();
semaphore.Destroy();
}
catch (SemaphoreNotHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(SemaphoreNotHeldException));
}
}
}
示例6: Semaphore_Destroy
public void Semaphore_Destroy()
{
var c = new Client();
const string keyName = "test/semaphore/destroy";
var semaphore1 = c.Semaphore(keyName, 2);
var semaphore2 = c.Semaphore(keyName, 2);
try
{
semaphore1.Acquire(CancellationToken.None);
Assert.IsTrue(semaphore1.IsHeld);
semaphore2.Acquire(CancellationToken.None);
Assert.IsTrue(semaphore2.IsHeld);
try
{
semaphore1.Destroy();
Assert.Fail();
}
catch (SemaphoreHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(SemaphoreHeldException));
}
semaphore1.Release();
Assert.IsFalse(semaphore1.IsHeld);
try
{
semaphore1.Destroy();
Assert.Fail();
}
catch (SemaphoreInUseException ex)
{
Assert.IsInstanceOfType(ex, typeof(SemaphoreInUseException));
}
semaphore2.Release();
Assert.IsFalse(semaphore2.IsHeld);
semaphore1.Destroy();
semaphore2.Destroy();
}
finally
{
try
{
semaphore1.Release();
}
catch (SemaphoreNotHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(SemaphoreNotHeldException));
}
try
{
semaphore2.Release();
}
catch (SemaphoreNotHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(SemaphoreNotHeldException));
}
}
}
示例7: Semaphore_ContendFast
public void Semaphore_ContendFast()
{
var client = new Client();
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.IsTrue(acquired[i]);
}
else
{
Assert.Fail("Contender " + i.ToString() + " did not acquire the lock");
}
}
}
示例8: Semaphore_AcquireWaitRelease
public void Semaphore_AcquireWaitRelease()
{
var client = new Client();
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.IsTrue(semaphore.IsHeld);
// Wait for multiple renewal cycles to ensure the semaphore session stays renewed.
Task.Delay(TimeSpan.FromSeconds(60)).Wait();
Assert.IsTrue(semaphore.IsHeld);
semaphore.Release();
Assert.IsFalse(semaphore.IsHeld);
semaphore.Destroy();
}