Skip to content

Commit

Permalink
test: improve test coverage of lock manager
Browse files Browse the repository at this point in the history
Acked-by: Paul Millar
Target: master
  • Loading branch information
kofemann committed Dec 17, 2018
1 parent 985b50e commit 92e12da
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
45 changes: 45 additions & 0 deletions core/src/test/java/org/dcache/nfs/v4/nlm/NlmLockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
114 changes: 114 additions & 0 deletions core/src/test/java/org/dcache/nfs/v4/nlm/SimpleLmTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 92e12da

Please sign in to comment.