diff --git a/core/src/test/java/org/dcache/nfs/v4/nlm/NlmLockTest.java b/core/src/test/java/org/dcache/nfs/v4/nlm/NlmLockTest.java index 46492865c..eb26fc4c4 100644 --- a/core/src/test/java/org/dcache/nfs/v4/nlm/NlmLockTest.java +++ b/core/src/test/java/org/dcache/nfs/v4/nlm/NlmLockTest.java @@ -106,4 +106,49 @@ public void shouldAllowEndingLockRange() { assertFalse("false conflicting lock range", lock.isOverlappingRange(lock1)); assertFalse("false conflicting lock range", lock.isOverlappingRange(lock2)); } + + @Test + public void shouldEqual() { + NlmLock lock1 = new NlmLock(lo1, nfs_lock_type4.WRITE_LT, 0, 1); + NlmLock lock2 = new NlmLock(lo1, nfs_lock_type4.WRITE_LT, 0, 1); + + assertTrue("the same lock should be equal", lock1.equals(lock2)); + assertTrue("equal objects must have the same hashcode", lock1.hashCode() == lock2.hashCode()); + } + + @Test + public void shouldNotBeEqualByOwner() { + + NlmLock lock1 = new NlmLock(lo1, nfs_lock_type4.WRITE_LT, 0, nfs4_prot.NFS4_UINT64_MAX); + NlmLock lock2 = new NlmLock(lo2, nfs_lock_type4.WRITE_LT, 0, nfs4_prot.NFS4_UINT64_MAX); + + assertFalse("lock with different owner can't be equal", lock1.equals(lock2)); + } + + @Test + public void shouldNotBeEqualByOffset() { + + NlmLock lock1 = new NlmLock(lo1, nfs_lock_type4.WRITE_LT, 0, nfs4_prot.NFS4_UINT64_MAX); + NlmLock lock2 = new NlmLock(lo1, nfs_lock_type4.WRITE_LT, 1, nfs4_prot.NFS4_UINT64_MAX); + + assertFalse("lock with different offsets can't be equal", lock1.equals(lock2)); + } + + @Test + public void shouldNotBeEqualByType() { + + NlmLock lock1 = new NlmLock(lo1, nfs_lock_type4.WRITE_LT, 0, nfs4_prot.NFS4_UINT64_MAX); + NlmLock lock2 = new NlmLock(lo1, nfs_lock_type4.READ_LT, 0, nfs4_prot.NFS4_UINT64_MAX); + + assertFalse("lock with different types can't be equal", lock1.equals(lock2)); + } + + @Test + public void shouldNotBeEqualByLegth() { + + NlmLock lock1 = new NlmLock(lo1, nfs_lock_type4.WRITE_LT, 0, 1); + NlmLock lock2 = new NlmLock(lo1, nfs_lock_type4.WRITE_LT, 0, 2); + + assertFalse("lock with different length can't be equal", lock1.equals(lock2)); + } } diff --git a/core/src/test/java/org/dcache/nfs/v4/nlm/SimpleLmTest.java b/core/src/test/java/org/dcache/nfs/v4/nlm/SimpleLmTest.java index 1832745cb..9f4c9ef0d 100644 --- a/core/src/test/java/org/dcache/nfs/v4/nlm/SimpleLmTest.java +++ b/core/src/test/java/org/dcache/nfs/v4/nlm/SimpleLmTest.java @@ -292,6 +292,33 @@ public void testSplitLock() throws LockException { nlm.test(file1, lock3); } + @Test + public void testSplitWholeFileLock() throws LockException { + NlmLock lock1 = new LockBuilder() + .withOwner("owner1") + .from(0) + .length(nfs4_prot.NFS4_UINT64_MAX) + .forWrite() + .build(); + nlm.lock(file1, lock1); + + NlmLock lock2 = new LockBuilder() + .withOwner("owner1") + .from(1) + .length(nfs4_prot.NFS4_UINT64_MAX) + .forRead() + .build(); + nlm.unlock(file1, lock2); + + NlmLock lock3 = new LockBuilder() + .withOwner("owner2") + .from(1) + .length(1) + .forWrite() + .build(); + nlm.test(file1, lock3); + } + @Test(expected = LockDeniedException.class) public void testMergeOfOverlapingLocks() throws LockException { NlmLock lock1 = new LockBuilder() @@ -319,6 +346,93 @@ public void testMergeOfOverlapingLocks() throws LockException { nlm.test(file1, lock3); } + @Test(expected = LockDeniedException.class) + public void testMergeOfContinuesLocks() throws LockException { + NlmLock lock1 = new LockBuilder() + .withOwner("owner1") + .from(74) + .length(nfs4_prot.NFS4_UINT64_MAX) + .forWrite() + .build(); + nlm.lock(file1, lock1); + + NlmLock lock2 = new LockBuilder() + .withOwner("owner1") + .from(0) + .length(75) + .forWrite() + .build(); + nlm.lock(file1, lock2); + + NlmLock lock3 = new LockBuilder() + .withOwner("owner2") + .from(1) + .length(100) + .forWrite() + .build(); + nlm.test(file1, lock3); + } + + @Test(expected = LockDeniedException.class) + public void testMergeOfWholeFileLocks() throws LockException { + NlmLock lock1 = new LockBuilder() + .withOwner("owner1") + .from(0) + .length(nfs4_prot.NFS4_UINT64_MAX) + .forWrite() + .build(); + nlm.lock(file1, lock1); + + NlmLock lock2 = new LockBuilder() + .withOwner("owner1") + .from(0) + .length(nfs4_prot.NFS4_UINT64_MAX) + .forWrite() + .build(); + nlm.lock(file1, lock2); + + NlmLock lock3 = new LockBuilder() + .withOwner("owner2") + .from(1) + .length(100) + .forWrite() + .build(); + nlm.test(file1, lock3); + } + + @Test + public void testLocAfterUnlockIfExist() throws LockException { + NlmLock lock1 = new LockBuilder() + .withOwner("owner1") + .from(0) + .length(1) + .forRead() + .build(); + nlm.lock(file1, lock1); + + nlm.unlockIfExists(file1, lock1); + + NlmLock lock2 = new LockBuilder() + .withOwner("owner2") + .from(0) + .length(1) + .forWrite() + .build(); + nlm.test(file1, lock2); + } + + @Test + public void testUnlockIfExistNonExisting() throws LockException { + NlmLock lock1 = new LockBuilder() + .withOwner("owner1") + .from(0) + .length(1) + .forRead() + .build(); + + nlm.unlockIfExists(file1, lock1); + } + public static class LockBuilder { private long offset;