Skip to content

Commit

Permalink
chimera: fix unit tests
Browse files Browse the repository at this point in the history
Two unit tests are racy because they assume at least 1 ms has elapsed
between two calls to stat.  This patch has the testing thread sleep
for a millisecond to ensure this is true.

The patch also alters how the directory listing helper handles invalid
situations, with it throwing an exception rather than logging an error.
This allows the unit-test that checks this behaviour to do so without
logging the error.

Target: master
Request: 2.10
Request: 2.9
Request: 2.8
Request: 2.7
Request: 2.6

Conflicts:
	modules/chimera/src/main/java/org/dcache/chimera/DirectoryStreamHelper.java
	modules/chimera/src/test/java/org/dcache/chimera/DirectoryStreamHelperTest.java
  • Loading branch information
paulmillar committed Oct 20, 2014
1 parent eef8ed2 commit 91781bf
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ public class DirectoryStreamHelper {
*/
public static List<HimeraDirectoryEntry> listOf(FsInode inode) throws IOException, IOHimeraFsException {

List<HimeraDirectoryEntry> directoryList = new ArrayList<>(inode.statCache().getNlink());
List<HimeraDirectoryEntry> directoryList;

int estimatedListSize = inode.statCache().getNlink();
if (estimatedListSize < 0) {
throw new RuntimeException("Invalid nlink count for directory: " + inode);
} else {
directoryList = new ArrayList<>(estimatedListSize);
}

try (DirectoryStreamB<HimeraDirectoryEntry> dirStream =
inode.newDirectoryStream()) {
for (HimeraDirectoryEntry e : dirStream) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ public void testDeleteFile() throws Exception {
base.create("testCreateFile", 0, 0, 0644);
Stat stat = base.stat();

Thread.sleep(1); // ensure updated directory mtime is distinct from creation mtime

base.remove("testCreateFile");

assertEquals("remove have to decrease parents link count", base.stat().getNlink(), stat.getNlink() - 1);
Expand All @@ -186,6 +188,8 @@ public void testDeleteDir() throws Exception {
base.create("testCreateDir", 0, 0, 0644);
Stat stat = base.stat();

Thread.sleep(1); // ensure updated directory mtime is distinct from creation mtime

base.remove("testCreateDir");

assertEquals("remove have to decrease parents link count", base.stat().getNlink(), stat.getNlink() - 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program (see the file COPYING.LIB for more
* details); if not, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.dcache.chimera;

import org.junit.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;

import org.dcache.chimera.posix.Stat;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.*;

public class DirectoryStreamHelperTest {

@Test(expected=RuntimeException.class)
public void testNegativeNlink() throws ChimeraFsException, IOException {

FsInode inode = mock(FsInode.class);
Stat stat = mock(Stat.class);

given(stat.getNlink()).willReturn(-7);
given(inode.stat()).willReturn(stat);
given(inode.statCache()).willReturn(stat);
given(inode.newDirectoryStream()).willReturn( new DirectoryStreamB<HimeraDirectoryEntry>() {

@Override
public Iterator<HimeraDirectoryEntry> iterator() {
return Collections.emptyIterator();
}

@Override
public void close() throws IOException {}
});
DirectoryStreamHelper.listOf(inode);
}

}

0 comments on commit 91781bf

Please sign in to comment.