Skip to content

Commit

Permalink
Moved tests related to filesystem to IdFile instead of IdGeneratorImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
klaren authored and MishaDemianenko committed Jul 4, 2017
1 parent 70a2363 commit 28c9f38
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 95 deletions.
Expand Up @@ -34,7 +34,7 @@ public class IdFile
static final long NO_RESULT = -1; static final long NO_RESULT = -1;


// sticky(byte), nextFreeId(long) // sticky(byte), nextFreeId(long)
private static final int HEADER_SIZE = Byte.BYTES + Long.BYTES; static final int HEADER_SIZE = Byte.BYTES + Long.BYTES;


// if sticky the id generator wasn't closed properly so it has to be // if sticky the id generator wasn't closed properly so it has to be
// rebuilt (go through the node, relationship, property, rel type etc files) // rebuilt (go through the node, relationship, property, rel type etc files)
Expand Down
@@ -0,0 +1,147 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.store.id;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.File;

import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.impl.store.InvalidIdGeneratorException;
import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.DefaultFileSystemRule;
import org.neo4j.test.rule.fs.FileSystemRule;

import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;


public class IdFileTest
{
@Rule
public final TestDirectory testDirectory = TestDirectory.testDirectory();
@Rule
public final FileSystemRule fileSystemRule = new DefaultFileSystemRule();
private FileSystemAbstraction fs;
private File file;

@Before
public void setUp() throws Exception
{
fs = fileSystemRule.get();
file = testDirectory.file( "ids" );
}

@Test
public void shouldDeleteIfOpen() throws Exception
{
// GIVEN
createEmptyFile();
IdFile idFile = new IdFile( fs, file, 100, false );
idFile.init();

// WHEN
idFile.delete();

// THEN
assertFalse( fs.fileExists( file ) );

idFile.close( 0 );
}

@Test
public void shouldDeleteIfClosed() throws Exception
{
// GIVEN
createEmptyFile();
IdFile idFile = new IdFile( fs, file, 100, false );
idFile.init();
idFile.close( 0 );

// WHEN
idFile.delete();

// THEN
assertFalse( fs.fileExists( file ) );
}

@Test
public void shouldForceStickyMark() throws Exception
{
// GIVEN
createEmptyFile();

// WHEN opening the id generator, where the jvm crashes right after
IdFile idFile = new IdFile( fs, file, 100, false );
idFile.init();

// THEN
try
{
IdFile.readHighId( fs, file );
fail( "Should have thrown, saying something with sticky generator" );
}
catch ( InvalidIdGeneratorException e )
{
// THEN Good
}
finally
{
idFile.close( 0 );
}
}

@Test
public void shouldTruncateTheFileIfOverwriting() throws Exception
{
// GIVEN
IdFile.createEmptyIdFile( fs, file, 30, false );
IdFile idFile = new IdFile( fs, file, 5, false );
idFile.init();
for ( int i = 0; i < 17; i++ )
{
idFile.freeId( i );
}
idFile.close( 30 );
assertThat( (int) fs.getFileSize( file ), greaterThan( IdFile.HEADER_SIZE ) );

// WHEN
IdFile.createEmptyIdFile( fs, file, 30, false );

// THEN
assertEquals( IdFile.HEADER_SIZE, (int) fs.getFileSize( file ) );
assertEquals( 30, IdFile.readHighId( fs, file ) );
idFile = new IdFile( fs, file, 5, false );
idFile.init();
assertEquals( 30, idFile.getInitialHighId() );

idFile.close( 30 );
}

private void createEmptyFile()
{
IdFile.createEmptyIdFile( fs, file, 42, false );
}
}
Expand Up @@ -23,24 +23,17 @@
import org.junit.Test; import org.junit.Test;


import java.io.File; import java.io.File;
import java.io.IOException;


import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.impl.store.InvalidIdGeneratorException;
import org.neo4j.kernel.impl.store.id.validation.IdCapacityExceededException; import org.neo4j.kernel.impl.store.id.validation.IdCapacityExceededException;
import org.neo4j.kernel.impl.store.id.validation.NegativeIdException; import org.neo4j.kernel.impl.store.id.validation.NegativeIdException;
import org.neo4j.test.rule.TestDirectory; import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.EphemeralFileSystemRule; import org.neo4j.test.rule.fs.EphemeralFileSystemRule;


import static java.util.concurrent.TimeUnit.MINUTES;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.neo4j.test.ProcessTestUtil.executeSubProcess;


public class IdGeneratorImplTest public class IdGeneratorImplTest
{ {
Expand Down Expand Up @@ -169,91 +162,4 @@ public void shouldBeAbleToReadWrittenGenerator()
// Then // Then
assertThat( idGenerator.getHighId(), equalTo( 42L ) ); assertThat( idGenerator.getHighId(), equalTo( 42L ) );
} }

@Test
public void shouldForceStickyMark() throws Exception
{
// GIVEN
try ( FileSystemAbstraction fs = new DefaultFileSystemAbstraction() )
{
File file = testDirectory.file( "ids" );
IdGeneratorImpl.createGenerator( fs, file, 0, false );

// WHEN opening the id generator, where the jvm crashes right after
executeSubProcess( getClass(), 1, MINUTES, file.getAbsolutePath() );

// THEN
try
{
IdGeneratorImpl.readHighId( fs, file );
fail( "Should have thrown, saying something with sticky generator" );
}
catch ( InvalidIdGeneratorException e )
{
// THEN Good
}
}
}

@Test
public void shouldDeleteIfOpen() throws Exception
{
// GIVEN
IdGeneratorImpl.createGenerator( fsr.get(), file, 42, false );
IdGeneratorImpl idGenerator = new IdGeneratorImpl( fsr.get(), file, 100, 100, false, 42 );

// WHEN
idGenerator.delete();

// THEN
assertFalse( fsr.get().fileExists( file ) );
}

@Test
public void shouldDeleteIfClosed() throws Exception
{
// GIVEN
IdGeneratorImpl.createGenerator( fsr.get(), file, 42, false );
IdGeneratorImpl idGenerator = new IdGeneratorImpl( fsr.get(), file, 100, 100, false, 42 );
idGenerator.close();

// WHEN
idGenerator.delete();

// THEN
assertFalse( fsr.get().fileExists( file ) );
}

// @Test
// public void shouldTruncateTheFileIfOverwriting() throws Exception
// {
// // GIVEN
// IdGeneratorImpl.createGenerator( fsr.get(), file, 10, true );
// IdGeneratorImpl idGenerator = new IdGeneratorImpl( fsr.get(), file, 5, 100, false, 30 );
// for ( int i = 0; i < 17; i++ )
// {
// idGenerator.freeId( i );
// }
// idGenerator.close();
// assertThat( (int) fsr.get().getFileSize( file ), greaterThan( IdGeneratorImpl.HEADER_SIZE ) );
//
// // WHEN
// IdGeneratorImpl.createGenerator( fsr.get(), file, 30, false );
//
// // THEN
// assertEquals( IdGeneratorImpl.HEADER_SIZE, (int) fsr.get().getFileSize( file ) );
// assertEquals( 30, IdGeneratorImpl.readHighId( fsr.get(), file ) );
// idGenerator = new IdGeneratorImpl( fsr.get(), file, 5, 100, false, 30 );
// assertEquals( 30, idGenerator.nextId() );
// }

public static void main( String[] args ) throws IOException
{
// Leave it opened
try ( DefaultFileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction() )
{
new IdGeneratorImpl( fileSystem, new File( args[0] ), 100, 100, false, 42 );
}
System.exit( 0 );
}
} }

0 comments on commit 28c9f38

Please sign in to comment.