Skip to content

Commit

Permalink
Fix flaky test on Windows
Browse files Browse the repository at this point in the history
Casting int to char was causing mismatches when writing and reading
them back on Windows
  • Loading branch information
davidegrohmann committed May 20, 2016
1 parent ecf47cb commit 70a5194
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 26 deletions.
Expand Up @@ -35,18 +35,19 @@
import org.neo4j.coreedge.raft.ReplicatedString;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.lifecycle.Lifecycle;
import org.neo4j.test.rule.TargetDirectory;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertEquals;

public abstract class ConcurrentStressIT
public abstract class ConcurrentStressIT<T extends RaftLog & Lifecycle>
{
private static final int MAX_CONTENT_SIZE = 2048;
@Rule
public final TargetDirectory.TestDirectory dir = TargetDirectory.testDirForTest( getClass() );

protected abstract RaftLog createRaftLog( FileSystemAbstraction fsa, File dir ) throws Throwable;
protected abstract T createRaftLog( FileSystemAbstraction fsa, File dir ) throws Throwable;

@Test
public void readAndWrite() throws Throwable
Expand All @@ -57,30 +58,36 @@ public void readAndWrite() throws Throwable
private void readAndWrite( int nReaders, int time, TimeUnit unit ) throws Throwable
{
DefaultFileSystemAbstraction fsa = new DefaultFileSystemAbstraction();
RaftLog raftLog = createRaftLog( fsa, dir.directory() );
T raftLog = createRaftLog( fsa, dir.directory() );

ExecutorService es = Executors.newCachedThreadPool();
try
{
ExecutorService es = Executors.newCachedThreadPool();

Collection<Future<Long>> futures = new ArrayList<>();
Collection<Future<Long>> futures = new ArrayList<>();

futures.add(
es.submit( new TimedTask( () -> {
write( raftLog );
}, time, unit ) )
);
futures.add( es.submit( new TimedTask( () -> {
write( raftLog );
}, time, unit ) ) );

for ( int i = 0; i < nReaders; i++ )
{
futures.add(
es.submit( new TimedTask( () -> {
read( raftLog );
}, time, unit ) )
);
}
for ( int i = 0; i < nReaders; i++ )
{
futures.add( es.submit( new TimedTask( () -> {
read( raftLog );
}, time, unit ) ) );
}

for ( Future<Long> f : futures )
for ( Future<Long> f : futures )
{
long iterations = f.get();
}

es.shutdown();
}
finally
{
long iterations = f.get();
//noinspection ThrowFromFinallyBlock
raftLog.shutdown();
}
}

Expand Down Expand Up @@ -141,14 +148,16 @@ private void write( RaftLog raftLog )
}
}

private static final CharSequence CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

private String stringForIndex( long index )
{
int len = ((int) index) % MAX_CONTENT_SIZE + 1;
StringBuilder str = new StringBuilder( len );

while ( len-- > 0 )
{
str.append( (char) len );
str.append( CHARS.charAt( len % CHARS.length() ) );
}

return str.toString();
Expand Down
Expand Up @@ -24,12 +24,41 @@
import org.neo4j.coreedge.raft.log.InMemoryRaftLog;
import org.neo4j.coreedge.raft.log.RaftLog;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.lifecycle.Lifecycle;

public class ConcurrentStressIT extends org.neo4j.coreedge.raft.log.ConcurrentStressIT
public class ConcurrentStressIT extends org.neo4j.coreedge.raft.log.ConcurrentStressIT<ConcurrentStressIT.LifecycledInMemoryRaftLog>
{
@Override
public RaftLog createRaftLog( FileSystemAbstraction fsa, File dir ) throws Throwable
public LifecycledInMemoryRaftLog createRaftLog( FileSystemAbstraction fsa, File dir ) throws Throwable
{
return new InMemoryRaftLog();
return new LifecycledInMemoryRaftLog();
}

public static class LifecycledInMemoryRaftLog extends InMemoryRaftLog implements Lifecycle
{

@Override
public void init() throws Throwable
{

}

@Override
public void start() throws Throwable
{

}

@Override
public void stop() throws Throwable
{

}

@Override
public void shutdown() throws Throwable
{

}
}
}
Expand Up @@ -28,10 +28,10 @@

import static org.neo4j.coreedge.server.CoreEdgeClusterSettings.raft_log_pruning;

public class ConcurrentStressIT extends org.neo4j.coreedge.raft.log.ConcurrentStressIT
public class ConcurrentStressIT extends org.neo4j.coreedge.raft.log.ConcurrentStressIT<SegmentedRaftLog>
{
@Override
public RaftLog createRaftLog( FileSystemAbstraction fsa, File dir ) throws Throwable
public SegmentedRaftLog createRaftLog( FileSystemAbstraction fsa, File dir ) throws Throwable
{
SegmentedRaftLog raftLog = new SegmentedRaftLog( fsa, dir, 8 * 1024 * 1024, new DummyRaftableContentSerializer(), NullLogProvider.getInstance(), 8,
raft_log_pruning.getDefaultValue());
Expand Down

0 comments on commit 70a5194

Please sign in to comment.