Skip to content

Commit

Permalink
Better PageCache unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacchella committed Nov 2, 2011
1 parent 26d5c55 commit 8ae8fcb
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions junit/jrds/caching/TestPageCache.java
Expand Up @@ -8,7 +8,6 @@
import javax.xml.parsers.ParserConfigurationException;

import jrds.Tools;
import jrds.Util;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
Expand All @@ -18,52 +17,65 @@

public class TestPageCache {
static final private Logger logger = Logger.getLogger(TestRrdCachedFileBackend.class);
static final private int numpages = 4;
static final private File testFile = new File("tmp/testpagecache");

@BeforeClass
static public void configure() throws IOException, ParserConfigurationException {
Tools.configure();
Tools.setLevel(logger, Level.TRACE, "jrds.caching.RrdCachedFileBackend", "jrds.caching.FilePage", "jrds.caching.PageCache");
}

private void checkcontent() throws IOException {
Assert.assertEquals("File size invalid", PageCache.PAGESIZE * numpages, testFile.length());
FileInputStream in = new FileInputStream(testFile);
byte[] bufferin = new byte[PageCache.PAGESIZE * numpages ];
in.read(bufferin);
for(int i=0 ; i < PageCache.PAGESIZE * numpages; i++ ) {
Assert.assertEquals(String.format("Invalid content at offset %d", i), (byte)Math.floor(i / PageCache.PAGESIZE), bufferin[i]);
}
}

@Test
public void test1() throws IOException {
String outString = getClass().getName();
PageCache pc = new PageCache(4,3600);
File testFile = new File("tmp/testpagecache");
public void fillSequential() throws IOException {
PageCache pc = new PageCache(numpages,3600);
if(testFile.exists())
testFile.delete();
byte[] buffer = outString.getBytes();
pc.write(testFile, 0, buffer);
byte[] buffer = new byte[PageCache.PAGESIZE];
for(byte i= 0; i < numpages; i++) {
Arrays.fill(buffer, i);
pc.write(testFile, i * PageCache.PAGESIZE, buffer);
}
pc.sync();

FileInputStream in = new FileInputStream(testFile);
byte[] b = new byte[(int) testFile.length()];
in.read(b);
Assert.assertEquals("read does not match write", outString.trim(), new String(b).trim());
testFile.delete();
checkcontent();
}

@Test
public void test2() throws IOException {
int numpages = 3;
public void fillReverse() throws IOException {
PageCache pc = new PageCache(numpages,3600);
File testFile = new File("tmp/testpagecache");
if(testFile.exists())
testFile.delete();
byte[] buffer = new byte[PageCache.PAGESIZE];
for(byte i= 0; i < numpages; i++) {
for(byte i = (byte) (numpages - 1) ; i > 0; i--) {
Arrays.fill(buffer, i);
pc.write(testFile, i * PageCache.PAGESIZE, buffer);
}
pc.sync();
Assert.assertEquals("File size invalid", PageCache.PAGESIZE * numpages, testFile.length());
FileInputStream in = new FileInputStream(testFile);
byte[] bufferin = new byte[PageCache.PAGESIZE * numpages ];
in.read(bufferin);
for(int i=0 ; i < PageCache.PAGESIZE * numpages; i++ ) {
Assert.assertEquals(String.format("Invalid content at offset %d", i), (byte)Math.floor(i / PageCache.PAGESIZE), bufferin[i]);
}
checkcontent();
}

@Test
public void fillOnce() throws IOException {
PageCache pc = new PageCache(numpages,3600);
if(testFile.exists())
testFile.delete();
byte[] buffer = new byte[PageCache.PAGESIZE * numpages];
for(byte i= 0; i < numpages; i++) {
Arrays.fill(buffer, PageCache.PAGESIZE * i , PageCache.PAGESIZE * (i + 1), i);
}
pc.write(testFile, 0, buffer);
pc.sync();
checkcontent();
}

}

0 comments on commit 8ae8fcb

Please sign in to comment.