Skip to content

Commit

Permalink
Log tests should write logfiles to target/ dir
Browse files Browse the repository at this point in the history
  • Loading branch information
jhalterman committed Aug 23, 2015
1 parent a35588d commit 6eaf1d6
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 176 deletions.
@@ -0,0 +1,57 @@
package net.kuujo.copycat.io.storage;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.UUID;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

@Test
public abstract class AbstractLogTest {
protected Log log;
String logId;

protected abstract Log createLog();

@BeforeMethod
void setLog() {
logId = UUID.randomUUID().toString();
log = createLog();
}

@AfterMethod
protected void deleteLog() {
log.delete();
}

protected Storage.Builder tempStorageBuilder() {
return Storage.builder().withDirectory(new File(String.format("target/test-logs/%s", logId)));
}

@AfterTest
protected void cleanLogDir() throws IOException {
Path directory = Paths.get("target/test-logs/");
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
}
39 changes: 23 additions & 16 deletions storage/src/test/java/net/kuujo/copycat/io/storage/CleanerTest.java
Expand Up @@ -15,42 +15,49 @@
*/
package net.kuujo.copycat.io.storage;

import net.jodah.concurrentunit.ConcurrentTestCase;
import net.kuujo.copycat.io.serializer.Serializer;
import net.kuujo.copycat.io.serializer.ServiceLoaderTypeResolver;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

import java.util.concurrent.CountDownLatch;

import org.testng.annotations.Test;

import static org.testng.Assert.*;
import net.kuujo.copycat.io.serializer.Serializer;
import net.kuujo.copycat.io.serializer.ServiceLoaderTypeResolver;

/**
* Minor compaction test.
*
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
@Test
public class CleanerTest extends ConcurrentTestCase {
public class CleanerTest extends AbstractLogTest {

protected Log createLog() {
return tempStorageBuilder()
.withMaxEntriesPerSegment(10)
.withSerializer(new Serializer(new ServiceLoaderTypeResolver()))
.build()
.open();
}

/**
* Tests compacting the log.
*/
public void testCompact() throws Throwable {
Storage storage = Storage.builder()
.withMaxEntriesPerSegment(10)
.withSerializer(new Serializer(new ServiceLoaderTypeResolver()))
.build();

Log log = storage.open();

writeEntries(log, 30);
writeEntries(30);

assertEquals(log.length(), 30L);

for (long index = 21; index < 28; index++) {
log.clean(index);
}

log.cleaner().clean().thenRun(this::resume);
await();
CountDownLatch latch = new CountDownLatch(1);
log.cleaner().clean().thenRun(latch::countDown);
latch.await();

assertEquals(log.length(), 30L);

Expand All @@ -66,7 +73,7 @@ public void testCompact() throws Throwable {
/**
* Writes a set of session entries to the log.
*/
private void writeEntries(Log log, int entries) {
private void writeEntries(int entries) {
for (int i = 0; i < entries; i++) {
try (TestEntry entry = log.create(TestEntry.class)) {
entry.setTerm(1);
Expand Down

0 comments on commit 6eaf1d6

Please sign in to comment.