Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2002-2014 "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 recovery;
 
 
 import org.junit.Rule;
 import org.junit.Test;
 
 
 import static java.util.concurrent.TimeUnit.SECONDS;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
 import static org.neo4j.graphdb.DynamicLabel.label;
 import static org.neo4j.helpers.collection.Iterables.single;
 import static org.neo4j.test.EphemeralFileSystemRule.shutdownDb;

Arbitrary recovery scenarios boiled down to as small tests as possible
 
 public class TestRecoveryScenarios
 {
     @Test
     {
         // GIVEN
         Node node = createNodeWithProperty"key""value" );
         rotateLog();
         setPropertynode"other-key", 1 );
         deleteNodenode );
         flushAll();
 
         // WHEN
         crashAndRestart );
 
         // THEN
         // -- really the problem was that recovery threw exception, so mostly assert that.
         try ( Transaction tx = .beginTx() )
         {
             node = .getNodeByIdnode.getId() );
             tx.success();
             fail"Should not exist" );
         }
         catch ( NotFoundException e )
         {
             assertEquals"Node " + node.getId() + " not found"e.getMessage() );
         }
     }
 
     @Test
     {
         // GIVEN
         createIndex"key" );
         Node node = createNodeWithProperty"key""value" );
         rotateLog();
         addLabelnode );
         InMemoryIndexProvider outdatedIndexProvider = .snapshot();
         removePropertynode"key" );
         flushAll();
 
         // WHEN
         crashAndRestartoutdatedIndexProvider );
 
         // THEN
        // -- really the problem was that recovery threw exception, so mostly assert that.
        try ( Transaction tx = .beginTx() )
        {
            assertEquals"Updates not propagated correctly during recovery", Collections.<Node>emptyList(),
                          IteratorUtil.asList.findNodesByLabelAndProperty"key""value" ) ) );
            tx.success();
        }
    }
    @Test
    {
        // GIVEN
        createIndex"key" );
        Label[] labels = new Label[16];
        for (int i = 0; i < labels.lengthi++ )
        {
            labels[i] = DynamicLabel.label"Label" + Integer.toHexStringi ) );
        }
        Node node;
        try ( Transaction tx = .beginTx() )
        {
            node = .createNodelabels );
            node.addLabel );
            tx.success();
        }
        rotateLog();
        InMemoryIndexProvider outdatedIndexProvider = .snapshot();
        setPropertynode"key""value" );
        removeLabelsnodelabels );
        flushAll();
        // WHEN
        crashAndRestartoutdatedIndexProvider );
        // THEN
        // -- really the problem was that recovery threw exception, so mostly assert that.
        try ( Transaction tx = .beginTx() )
        {
            assertEqualsnodesingle.findNodesByLabelAndProperty"key""value" ) ) );
            tx.success();
        }
    }
    private void removeLabelsNode nodeLabel... labels )
    {
        try ( Transaction tx = .beginTx() )
        {
            for ( Label label : labels )
            {
                node.removeLabellabel );
            }
            tx.success();
        }
    }
    private void removePropertyNode nodeString key )
    {
        try ( Transaction tx = .beginTx() )
        {
            node.removePropertykey );
            tx.success();
        }
    }
    private void addLabelNode nodeLabel label )
    {
        try ( Transaction tx = .beginTx() )
        {
            node.addLabellabel );
            tx.success();
        }
    }
    private Node createNodeWithPropertyString keyString valueLabel... labels )
    {
        try ( Transaction tx = .beginTx() )
        {
            Node node = .createNodelabels );
            node.setPropertykeyvalue );
            tx.success();
            return node;
        }
    }
    private void createIndexLabel labelString key )
    {
        try ( Transaction tx = .beginTx() )
        {
            .schema().indexForlabel ).onkey ).create();
            tx.success();
        }
        try ( Transaction tx = .beginTx() )
        {
            .schema().awaitIndexesOnline( 10,  );
            tx.success();
        }
    }
    public final @Rule EphemeralFileSystemRule fsRule = new EphemeralFileSystemRule();
    private final Label label = label"label" );
    @SuppressWarnings("deprecation"private GraphDatabaseAPI db;
    private final InMemoryIndexProvider indexProvider = new InMemoryIndexProvider( 100 );
    @SuppressWarnings("deprecation")
    @Before
    public void before()
    {
    }
    {
        return new TestGraphDatabaseFactory()
            .setFileSystemfs ).addKernelExtensionnew InMemoryIndexProviderFactoryindexProvider ) );
    }
    @After
    public void after()
    {
        .shutdown();
    }
    private void rotateLog()
    {
    }
    @SuppressWarnings("deprecation")
    private void flushAll()
    {
                XaDataSourceManager.class ).getNeoStoreDataSource().getNeoStore().flushAll();
    }
    private void deleteNodeNode node )
    {
        try ( Transaction tx = .beginTx() )
        {
            node.delete();
            tx.success();
        }
    }
    private void setPropertyNode nodeString keyObject value )
    {
        try ( Transaction tx = .beginTx() )
        {
            node.setPropertykeyvalue );
            tx.success();
        }
    }
    @SuppressWarnings("deprecation")
    private void crashAndRestartInMemoryIndexProvider indexProvider )
    {
        FileSystemAbstraction uncleanFs = .snapshotshutdownDb ) );
         = (GraphDatabaseAPIdatabaseFactoryuncleanFsindexProvider ).newImpermanentDatabase();
    }
New to GrepCode? Check out our FAQ X