Search in sources :

Example 1 with LifecycleListener

use of org.neo4j.kernel.lifecycle.LifecycleListener in project neo4j by neo4j.

the class ClusterTransactionIT method givenClusterWhenShutdownMasterThenCannotStartTransactionOnSlave.

@Test
public void givenClusterWhenShutdownMasterThenCannotStartTransactionOnSlave() throws Throwable {
    final HighlyAvailableGraphDatabase master = cluster.getMaster();
    final HighlyAvailableGraphDatabase slave = cluster.getAnySlave();
    final long nodeId;
    try (Transaction tx = master.beginTx()) {
        nodeId = master.createNode().getId();
        tx.success();
    }
    cluster.sync();
    // When
    final FutureTask<Boolean> result = new FutureTask<>(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            try (Transaction tx = slave.beginTx()) {
                tx.acquireWriteLock(slave.getNodeById(nodeId));
            } catch (Exception e) {
                return contains(e, TransactionFailureException.class);
            }
            // Fail otherwise
            return false;
        }
    });
    master.getDependencyResolver().resolveDependency(LifeSupport.class).addLifecycleListener(new LifecycleListener() {

        @Override
        public void notifyStatusChanged(Object instance, LifecycleStatus from, LifecycleStatus to) {
            if (instance.getClass().getName().contains("DatabaseAvailability") && to == LifecycleStatus.STOPPED) {
                result.run();
            }
        }
    });
    master.shutdown();
    // Then
    assertThat(result.get(), equalTo(true));
}
Also used : LifecycleListener(org.neo4j.kernel.lifecycle.LifecycleListener) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) FutureTask(java.util.concurrent.FutureTask) HighlyAvailableGraphDatabase(org.neo4j.kernel.ha.HighlyAvailableGraphDatabase) LifecycleStatus(org.neo4j.kernel.lifecycle.LifecycleStatus) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) Test(org.junit.Test)

Aggregations

FutureTask (java.util.concurrent.FutureTask)1 Test (org.junit.Test)1 Transaction (org.neo4j.graphdb.Transaction)1 TransactionFailureException (org.neo4j.kernel.api.exceptions.TransactionFailureException)1 HighlyAvailableGraphDatabase (org.neo4j.kernel.ha.HighlyAvailableGraphDatabase)1 LifeSupport (org.neo4j.kernel.lifecycle.LifeSupport)1 LifecycleListener (org.neo4j.kernel.lifecycle.LifecycleListener)1 LifecycleStatus (org.neo4j.kernel.lifecycle.LifecycleStatus)1