public void TestReadWriteLock()
 {
     var testDir = new DirectoryInfo(Path.Combine(tempDir.FullName, Guid.NewGuid().ToString()));
     testDir.Create();
     var locker = new DirectoryLocker(testDir.FullName);
     locker.ReadWriteLock();
     Assert.AreEqual(1, testDir.GetFiles("readwrite.lock").Count());
 }
 public void TestStaticWriteUnlock()
 {
     var testDir = new DirectoryInfo(Path.Combine(tempDir.FullName, Guid.NewGuid().ToString()));
     testDir.Create();
     var locker = new DirectoryLocker(testDir.FullName);
     locker.WriteLock();
     Assert.AreEqual(1, testDir.GetFiles("write*.lock").Count());
     DirectoryLocker.WriteUnlock(locker.LockerId, testDir.FullName);
     Assert.AreEqual(0, testDir.GetFiles("write*.lock").Count());
 }
 public void TestIsReadWriteLocked()
 {
     var testDir = new DirectoryInfo(Path.Combine(tempDir.FullName, Guid.NewGuid().ToString()));
     testDir.Create();
     using (var lockFile = File.Create(Path.Combine(testDir.FullName, "readwrite.lock"))) { }
     var locker = new DirectoryLocker(testDir.FullName);
     Assert.IsTrue(locker.IsReadWriteLocked);
 }
 private void WriteUnLock(DirectoryLocker locker, int waitSeconds)
 {
     Thread.Sleep(waitSeconds * 1000);
     locker.WriteUnlock();
 }
 public void TestWaitWriteUnlock()
 {
     var testDir = new DirectoryInfo(Path.Combine(tempDir.FullName, Guid.NewGuid().ToString()));
     testDir.Create();
     var testLocker = new DirectoryLocker(testDir.FullName);
     testLocker.WriteLock();
     Task.Factory.StartNew(() =>
     {
         WriteUnLock(testLocker, 10);
     });
     var start = DateTime.Now;
     testLocker.WaitWriteUnlock(100, 1000);
     var finish = DateTime.Now;
     var delta = finish - start;
     Assert.IsTrue(delta.TotalSeconds > 7);
 }