Search in sources :

Example 26 with DoubleLatch

use of org.neo4j.test.DoubleLatch in project neo4j by neo4j.

the class AuthProceduresInteractionTestBase method shouldAllowProcedureStartingTransactionInNewThread.

@Test
public void shouldAllowProcedureStartingTransactionInNewThread() throws Throwable {
    exceptionsInProcedure.clear();
    DoubleLatch latch = new DoubleLatch(2);
    ClassWithProcedures.doubleLatch = latch;
    latch.start();
    assertEmpty(writeSubject, "CALL test.threadTransaction");
    latch.finishAndWaitForAllToFinish();
    assertThat(exceptionsInProcedure.size(), equalTo(0));
    assertSuccess(adminSubject, "MATCH (:VeryUniqueLabel) RETURN toString(count(*)) as n", r -> assertKeyIs(r, "n", "1"));
}
Also used : DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Example 27 with DoubleLatch

use of org.neo4j.test.DoubleLatch in project neo4j by neo4j.

the class FileUserRepositoryTest method shouldProvideUserByUsernameEvenIfMidSetUsers.

@Test
public void shouldProvideUserByUsernameEvenIfMidSetUsers() throws Throwable {
    // Given
    FileUserRepository users = new FileUserRepository(fs, authFile, logProvider);
    users.create(new User.Builder("oskar", Credential.forPassword("hidden")).build());
    DoubleLatch latch = new DoubleLatch(2);
    // When
    Future<Object> setUsers = threading.execute(o -> {
        users.setUsers(new HangingListSnapshot(latch, 10L, Collections.emptyList()));
        return null;
    }, null);
    latch.startAndWaitForAllToStart();
    // Then
    assertNotNull(users.getUserByName("oskar"));
    latch.finish();
    setUsers.get();
}
Also used : DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Example 28 with DoubleLatch

use of org.neo4j.test.DoubleLatch in project neo4j by neo4j.

the class TransactionIT method shouldTerminateQueriesEvenIfUsingPeriodicCommit.

@Test
public void shouldTerminateQueriesEvenIfUsingPeriodicCommit() throws Exception {
    // Spawns a throttled HTTP server, runs a PERIODIC COMMIT that fetches data from this server,
    // and checks that the query able to be terminated
    // We start with 3, because that is how many actors we have -
    // 1. the http server
    // 2. the running query
    // 3. the one terminating 2
    final DoubleLatch latch = new DoubleLatch(3, true);
    // This is used to block the http server between the first and second batch
    final Barrier.Control barrier = new Barrier.Control();
    // Serve CSV via local web server, let Jetty find a random port for us
    Server server = createHttpServer(latch, barrier, 20, 30);
    server.start();
    int localPort = getLocalPort(server);
    final BoltStateMachine[] machine = { null };
    Thread thread = new Thread() {

        @Override
        public void run() {
            try (BoltStateMachine stateMachine = env.newMachine(new BoltConnectionDescriptor(new InetSocketAddress("<testClient>", 56789), new InetSocketAddress("<writeServer>", 7468)))) {
                machine[0] = stateMachine;
                stateMachine.init(USER_AGENT, emptyMap(), null);
                String query = format("USING PERIODIC COMMIT 10 LOAD CSV FROM 'http://localhost:%d' AS line " + "CREATE (n:A {id: line[0], square: line[1]}) " + "WITH count(*) as number " + "CREATE (n:ShouldNotExist)", localPort);
                try {
                    latch.start();
                    stateMachine.run(query, emptyMap(), nullResponseHandler());
                    stateMachine.pullAll(nullResponseHandler());
                } finally {
                    latch.finish();
                }
            } catch (BoltConnectionFatality connectionFatality) {
                throw new RuntimeException(connectionFatality);
            }
        }
    };
    thread.setName("query runner");
    thread.start();
    // We block this thread here, waiting for the http server to spin up and the running query to get started
    latch.startAndWaitForAllToStart();
    Thread.sleep(1000);
    // This is the call that RESETs the Bolt connection and will terminate the running query
    machine[0].reset(nullResponseHandler());
    barrier.release();
    // We block again here, waiting for the running query to have been terminated, and for the server to have
    // wrapped up and finished streaming http results
    latch.finishAndWaitForAllToFinish();
    // And now we check that the last node did not get created
    try (Transaction ignored = env.graph().beginTx()) {
        assertFalse("Query was not terminated in time - nodes were created!", env.graph().findNodes(Label.label("ShouldNotExist")).hasNext());
    }
}
Also used : Server(org.eclipse.jetty.server.Server) InetSocketAddress(java.net.InetSocketAddress) BoltConnectionFatality(org.neo4j.bolt.v1.runtime.BoltConnectionFatality) Barrier(org.neo4j.test.Barrier) BoltConnectionDescriptor(org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor) Transaction(org.neo4j.graphdb.Transaction) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Example 29 with DoubleLatch

use of org.neo4j.test.DoubleLatch in project neo4j by neo4j.

the class AuthScenariosInteractionTestBase method roleManagement5.

/*
    Admin creates user Henrik with password bar
    Admin adds user Henrik to role Publisher
    Henrik logs in with correct password
    Henrik starts transaction with long running writing query Q
    Admin removes user Henrik from role Publisher (while Q still running)
    Q finishes and transaction is committed → ok
    Henrik starts new transaction with write query → permission denied
     */
@Test
public void roleManagement5() throws Throwable {
    assertEmpty(adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)");
    assertEmpty(adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Henrik')");
    S henrik = neo.login("Henrik", "bar");
    neo.assertAuthenticated(henrik);
    DoubleLatch latch = new DoubleLatch(2);
    ThreadedTransaction<S> write = new ThreadedTransaction<>(neo, latch);
    write.executeCreateNode(threading, henrik);
    latch.startAndWaitForAllToStart();
    assertEmpty(adminSubject, "CALL dbms.security.removeRoleFromUser('" + PUBLISHER + "', 'Henrik')");
    latch.finishAndWaitForAllToFinish();
    write.closeAndAssertSuccess();
    testFailWrite(henrik);
}
Also used : DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Example 30 with DoubleLatch

use of org.neo4j.test.DoubleLatch in project neo4j by neo4j.

the class BuiltInProceduresInteractionTestBase method shouldTerminateAllTransactionsForGivenUser.

//@Test
public void shouldTerminateAllTransactionsForGivenUser() throws Throwable {
    DoubleLatch latch = new DoubleLatch(3);
    ThreadedTransaction<S> schema1 = new ThreadedTransaction<>(neo, latch);
    ThreadedTransaction<S> schema2 = new ThreadedTransaction<>(neo, latch);
    schema1.executeCreateNode(threading, schemaSubject);
    schema2.executeCreateNode(threading, schemaSubject);
    latch.startAndWaitForAllToStart();
    assertSuccess(adminSubject, "CALL dbms.terminateTransactionsForUser( 'schemaSubject' )", r -> assertKeyIsMap(r, "username", "transactionsTerminated", map("schemaSubject", "2")));
    assertSuccess(adminSubject, "CALL dbms.listTransactions()", r -> assertKeyIsMap(r, "username", "activeTransactions", map("adminSubject", "1")));
    latch.finishAndWaitForAllToFinish();
    schema1.closeAndAssertTransactionTermination();
    schema2.closeAndAssertTransactionTermination();
    assertEmpty(adminSubject, "MATCH (n:Test) RETURN n.name AS name");
}
Also used : DoubleLatch(org.neo4j.test.DoubleLatch)

Aggregations

DoubleLatch (org.neo4j.test.DoubleLatch)48 Test (org.junit.Test)37 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)15 Collections.emptyMap (java.util.Collections.emptyMap)9 Map (java.util.Map)9 MapUtil.stringMap (org.neo4j.helpers.collection.MapUtil.stringMap)9 OffsetDateTime (java.time.OffsetDateTime)5 JobScheduler (org.neo4j.kernel.impl.util.JobScheduler)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Server (org.eclipse.jetty.server.Server)3 SchemaIndexTestHelper.mockIndexProxy (org.neo4j.kernel.impl.api.index.SchemaIndexTestHelper.mockIndexProxy)3 Neo4jJobScheduler (org.neo4j.kernel.impl.util.Neo4jJobScheduler)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.never (org.mockito.Mockito.never)2 Mockito.times (org.mockito.Mockito.times)2 Mockito.verify (org.mockito.Mockito.verify)2