本文整理汇总了C#中Client.CreateLock方法的典型用法代码示例。如果您正苦于以下问题:C# Client.CreateLock方法的具体用法?C# Client.CreateLock怎么用?C# Client.CreateLock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Client
的用法示例。
在下文中一共展示了Client.CreateLock方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Lock_AcquireRelease
public void Lock_AcquireRelease()
{
var client = new Client();
const string keyName = "test/lock/acquirerelease";
var lockKey = client.CreateLock(keyName);
try
{
lockKey.Release();
}
catch (LockNotHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(LockNotHeldException));
}
lockKey.Acquire(CancellationToken.None);
try
{
lockKey.Acquire(CancellationToken.None);
}
catch (LockHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(LockHeldException));
}
Assert.IsTrue(lockKey.IsHeld);
lockKey.Release();
try
{
lockKey.Release();
}
catch (LockNotHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(LockNotHeldException));
}
Assert.IsFalse(lockKey.IsHeld);
}
示例2: Lock_DeleteKey
public void Lock_DeleteKey()
{
var client = new Client();
const string keyName = "test/lock/deletekey";
var lockKey = (Lock)client.CreateLock(keyName);
try
{
lockKey.Acquire(CancellationToken.None);
Assert.IsTrue(lockKey.IsHeld);
var checker = Task.Run(() =>
{
while (lockKey.IsHeld)
{
Thread.Sleep(10);
}
Assert.IsFalse(lockKey.IsHeld);
});
Task.WaitAny(new[] { checker }, 1000);
client.KV.Delete(lockKey.Opts.Key);
}
finally
{
try
{
lockKey.Release();
lockKey.Destroy();
}
catch (LockNotHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(LockNotHeldException));
}
}
}
示例3: Lock_ForceInvalidate
public void Lock_ForceInvalidate()
{
var client = new Client();
const string keyName = "test/lock/forceinvalidate";
var lockKey = (Lock)client.CreateLock(keyName);
try
{
lockKey.Acquire(CancellationToken.None);
Assert.IsTrue(lockKey.IsHeld);
var checker = Task.Run(() =>
{
while (lockKey.IsHeld)
{
Task.Delay(10).Wait();
}
Assert.IsFalse(lockKey.IsHeld);
});
Task.Run(() => { client.Session.Destroy(lockKey.LockSession); });
Task.WaitAny(new[] { checker }, 1000);
}
finally
{
try
{
lockKey.Release();
lockKey.Destroy();
}
catch (LockNotHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(LockNotHeldException));
}
}
}
示例4: 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();
}
示例5: Lock_ReclaimLock
public void Lock_ReclaimLock()
{
var client = new Client();
const string keyName = "test/lock/reclaim";
var sessionRequest = client.Session.Create();
var sessionId = sessionRequest.Response;
try
{
var lock1 = client.CreateLock(new LockOptions(keyName)
{
Session = sessionId
});
var lock2 = client.CreateLock(new LockOptions(keyName)
{
Session = sessionId
});
try
{
lock1.Acquire(CancellationToken.None);
Assert.IsTrue(lock1.IsHeld);
if (lock1.IsHeld)
{
Task.WaitAny(new[] { Task.Run(() =>
{
lock2.Acquire(CancellationToken.None);
Assert.IsTrue(lock2.IsHeld);
}) }, 1000);
}
}
finally
{
lock1.Release();
}
var lockCheck = new[]
{
Task.Run(() =>
{
while (lock1.IsHeld)
{
Thread.Sleep(10);
}
}),
Task.Run(() =>
{
while (lock2.IsHeld)
{
Thread.Sleep(10);
}
})
};
Task.WaitAll(lockCheck, 1000);
Assert.IsFalse(lock1.IsHeld);
Assert.IsFalse(lock2.IsHeld);
}
finally
{
Assert.IsTrue(client.Session.Destroy(sessionId).Response);
}
}
示例6: Lock_Destroy
public void Lock_Destroy()
{
var client = new Client();
const string keyName = "test/lock/contendlockdelay";
var lockKey = client.CreateLock(keyName);
try
{
lockKey.Acquire(CancellationToken.None);
Assert.IsTrue(lockKey.IsHeld);
try
{
lockKey.Destroy();
Assert.Fail();
}
catch (LockHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(LockHeldException));
}
lockKey.Release();
Assert.IsFalse(lockKey.IsHeld);
var lockKey2 = client.CreateLock(keyName);
lockKey2.Acquire(CancellationToken.None);
Assert.IsTrue(lockKey2.IsHeld);
try
{
lockKey.Destroy();
Assert.Fail();
}
catch (LockInUseException ex)
{
Assert.IsInstanceOfType(ex, typeof(LockInUseException));
}
lockKey2.Release();
Assert.IsFalse(lockKey2.IsHeld);
lockKey.Destroy();
lockKey2.Destroy();
}
finally
{
try
{
lockKey.Release();
}
catch (LockNotHeldException ex)
{
Assert.IsInstanceOfType(ex, typeof(LockNotHeldException));
}
}
}
示例7: Lock_Contend_LockDelay
public void Lock_Contend_LockDelay()
{
var client = new Client();
const string keyName = "test/lock/contendlockdelay";
const int contenderPool = 3;
var acquired = new System.Collections.Concurrent.ConcurrentDictionary<int, bool>();
using (var cts = new CancellationTokenSource())
{
cts.CancelAfter((contenderPool + 1) * (int)Lock.DefaultLockWaitTime.TotalMilliseconds);
Parallel.For(0, contenderPool, new ParallelOptions { MaxDegreeOfParallelism = contenderPool, CancellationToken = cts.Token }, (v) =>
{
var lockKey = (Lock)client.CreateLock(keyName);
lockKey.Acquire(CancellationToken.None);
if (lockKey.IsHeld)
{
Assert.IsTrue(acquired.TryAdd(v, lockKey.IsHeld));
client.Session.Destroy(lockKey.LockSession);
}
});
}
for (var i = 0; i < contenderPool; i++)
{
bool didContend = false;
if (acquired.TryGetValue(i, out didContend))
{
Assert.IsTrue(didContend);
}
else
{
Assert.Fail("Contender " + i.ToString() + " did not acquire the lock");
}
}
}
示例8: Lock_ContendFast
public void Lock_ContendFast()
{
var client = new Client();
const string keyName = "test/lock/contend";
const int contenderPool = 10;
var acquired = new System.Collections.Concurrent.ConcurrentDictionary<int, bool>();
using (var cts = new CancellationTokenSource())
{
cts.CancelAfter(contenderPool * (int)Lock.DefaultLockWaitTime.TotalMilliseconds);
Parallel.For(0, contenderPool, new ParallelOptions { MaxDegreeOfParallelism = contenderPool, CancellationToken = cts.Token }, (v) =>
{
var lockKey = client.CreateLock(keyName);
lockKey.Acquire(CancellationToken.None);
Assert.IsTrue(acquired.TryAdd(v, lockKey.IsHeld));
if (lockKey.IsHeld)
{
lockKey.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");
}
}
}
示例9: Lock_AcquireWaitRelease
public void Lock_AcquireWaitRelease()
{
var client = new Client();
const string keyName = "test/lock/acquirewaitrelease";
var lockOptions = new LockOptions(keyName)
{
SessionName = "test_locksession",
SessionTTL = TimeSpan.FromSeconds(10)
};
var l = client.CreateLock(lockOptions);
l.Acquire(CancellationToken.None);
Assert.IsTrue(l.IsHeld);
// Wait for multiple renewal cycles to ensure the lock session stays renewed.
Task.Delay(TimeSpan.FromSeconds(60)).Wait();
Assert.IsTrue(l.IsHeld);
l.Release();
Assert.IsFalse(l.IsHeld);
l.Destroy();
}