Skip to content

Commit

Permalink
Modify lock functionality
Browse files Browse the repository at this point in the history
Fixes hashicorp#3 because the session was not properly deleted, causing sessions to not
delete keys with SessionBehavior as Delete.

Fixes hashicorp#6
Fixes hashicorp#5
  • Loading branch information
Tiru Srikantha committed May 21, 2015
1 parent 5d53415 commit 4c969ae
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 102 deletions.
38 changes: 26 additions & 12 deletions Consul.Test/LockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class LockTest
public void Lock_AcquireRelease()
{
var c = ClientTest.MakeClient();
var lockKey = c.Lock("test/lock");
var lockKey = c.CreateLock("test/lock");

try
{
Expand Down Expand Up @@ -81,6 +81,16 @@ public void Lock_AcquireRelease()
Assert.IsFalse(lockKey.IsHeld);
}

[TestMethod]
public void Lock_EphemeralAcquireRelease()
{
var c = ClientTest.MakeClient();
using (var l = c.AcquireLock(new LockOptions("test/ephemerallock") { SessionBehavior = SessionBehavior.Delete }))
{
Assert.IsTrue(l.IsHeld);
}
}

[TestMethod]
public void Lock_Contend()
{
Expand All @@ -96,7 +106,7 @@ public void Lock_Contend()
var v = i;
acquireTasks[i] = new Task(() =>
{
var lockKey = c.Lock(key);
var lockKey = c.CreateLock(key);
lockKey.Acquire(CancellationToken.None);
acquired[v] = lockKey.IsHeld;
if (lockKey.IsHeld)
Expand All @@ -108,7 +118,7 @@ public void Lock_Contend()
acquireTasks[i].Start();
}

Task.WaitAll(acquireTasks, (int)(3 * Lock.DefaultLockRetryTime.TotalMilliseconds));
Task.WaitAll(acquireTasks, (int)(10 * Lock.DefaultLockRetryTime.TotalMilliseconds));

foreach (var item in acquired)
{
Expand All @@ -121,7 +131,7 @@ public void Lock_Destroy()
{
var c = ClientTest.MakeClient();
var key = "test/lock";
var lockKey = c.Lock(key);
var lockKey = c.CreateLock(key);
try
{
lockKey.Acquire(CancellationToken.None);
Expand All @@ -142,7 +152,7 @@ public void Lock_Destroy()

Assert.IsFalse(lockKey.IsHeld);

var lockKey2 = c.Lock(key);
var lockKey2 = c.CreateLock(key);

lockKey2.Acquire(CancellationToken.None);

Expand Down Expand Up @@ -216,7 +226,7 @@ public void Lock_SemaphoreConflict()

Assert.IsTrue(sema.IsHeld);

var lockKey = c.Lock("test/lock/.lock");
var lockKey = c.CreateLock("test/lock/.lock");
try
{
lockKey.Acquire(CancellationToken.None);
Expand Down Expand Up @@ -251,12 +261,12 @@ public void Lock_ReclaimLock()
sessReq.Wait();
var sess = sessReq.Result.Response;

var lockOpts = c.Lock(new LockOptions("test/lock")
var lockOpts = c.CreateLock(new LockOptions("test/lock")
{
Session = sess
});

var lockKey2 = c.Lock(new LockOptions("test/lock")
var lockKey2 = c.CreateLock(new LockOptions("test/lock")
{
Session = sess
});
Expand All @@ -267,7 +277,11 @@ public void Lock_ReclaimLock()

Assert.IsTrue(lockOpts.IsHeld);

Task.Run(() => lockKey2.Acquire(CancellationToken.None));
Task.Run(() =>
{
Debugger.Break();
lockKey2.Acquire(CancellationToken.None);
});

var lock2Hold = new Task(() =>
{
Expand All @@ -278,7 +292,7 @@ public void Lock_ReclaimLock()
});
lock2Hold.Start();

Task.WaitAny(new[] { lock2Hold }, 1000);
Task.WaitAny(new[] { lock2Hold }, 100000);

Assert.IsTrue(lockKey2.IsHeld);
}
Expand Down Expand Up @@ -328,7 +342,7 @@ public void Lock_ReclaimLock()
public void Lock_ForceInvalidate()
{
var c = ClientTest.MakeClient();
var lockKey = c.Lock("test/lock");
var lockKey = c.CreateLock("test/lock");
try
{
lockKey.Acquire(CancellationToken.None);
Expand Down Expand Up @@ -372,7 +386,7 @@ public void Lock_ForceInvalidate()
public void Lock_DeleteKey()
{
var c = ClientTest.MakeClient();
var lockKey = c.Lock("test/lock");
var lockKey = c.CreateLock("test/lock");
try
{
lockKey.Acquire(CancellationToken.None);
Expand Down
2 changes: 1 addition & 1 deletion Consul.Test/SemaphoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public void Semaphore_Conflict()
{
var c = ClientTest.MakeClient();

var semaphoreLock = c.Lock("test/sema/.lock");
var semaphoreLock = c.CreateLock("test/sema/.lock");

semaphoreLock.Acquire(CancellationToken.None);

Expand Down
Loading

0 comments on commit 4c969ae

Please sign in to comment.