Search in sources :

Example 1 with BoltResult

use of org.neo4j.bolt.v1.runtime.spi.BoltResult in project neo4j by neo4j.

the class BoltStateMachineTest method testPublishingError.

@Test
public void testPublishingError() throws Throwable {
    // Given a new ready machine...
    BoltStateMachine machine = newMachine(READY);
    // ...and a result ready to be retrieved...
    machine.run("RETURN 1", null, nullResponseHandler());
    // ...and a handler guaranteed to break
    BoltResponseRecorder recorder = new BoltResponseRecorder() {

        @Override
        public void onRecords(BoltResult result, boolean pull) throws Exception {
            throw new RuntimeException("I've been expecting you, Mr Bond.");
        }
    };
    // When we pull using that handler
    machine.pullAll(recorder);
    // Then the breakage should surface as a FAILURE
    assertThat(recorder.nextResponse(), failedWithStatus(Status.General.UnknownError));
    // ...and the machine should have entered a FAILED state
    assertThat(machine, inState(FAILED));
}
Also used : BoltResponseRecorder(org.neo4j.bolt.testing.BoltResponseRecorder) BoltResult(org.neo4j.bolt.v1.runtime.spi.BoltResult) Test(org.junit.Test)

Example 2 with BoltResult

use of org.neo4j.bolt.v1.runtime.spi.BoltResult in project neo4j by neo4j.

the class BoltConnectionIT method shouldHandleFailureDuringResultPublishing.

@Test
public void shouldHandleFailureDuringResultPublishing() throws Throwable {
    // Given
    BoltStateMachine machine = env.newMachine(CONNECTION_DESCRIPTOR);
    machine.init(USER_AGENT, emptyMap(), null);
    final CountDownLatch pullAllCallbackCalled = new CountDownLatch(1);
    final AtomicReference<Neo4jError> error = new AtomicReference<>();
    // When something fails while publishing the result stream
    machine.run("RETURN 1", EMPTY_PARAMS, nullResponseHandler());
    machine.pullAll(new BoltResponseHandler() {

        @Override
        public void onStart() {
        }

        @Override
        public void onRecords(BoltResult result, boolean pull) throws Exception {
            throw new RuntimeException("Ooopsies!");
        }

        @Override
        public void onMetadata(String key, Object value) {
        }

        @Override
        public void markFailed(Neo4jError err) {
            error.set(err);
            pullAllCallbackCalled.countDown();
        }

        @Override
        public void markIgnored() {
        }

        @Override
        public void onFinish() {
        }
    });
    // Then
    assertTrue(pullAllCallbackCalled.await(30, TimeUnit.SECONDS));
    final Neo4jError err = error.get();
    assertThat(err.status(), equalTo((Status) Status.General.UnknownError));
    assertThat(err.message(), CoreMatchers.containsString("Ooopsies!"));
}
Also used : Status(org.neo4j.kernel.api.exceptions.Status) BoltMatchers.failedWithStatus(org.neo4j.bolt.testing.BoltMatchers.failedWithStatus) BoltResponseHandler(org.neo4j.bolt.v1.runtime.BoltResponseHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) BoltResult(org.neo4j.bolt.v1.runtime.spi.BoltResult) Neo4jError(org.neo4j.bolt.v1.runtime.Neo4jError) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) Test(org.junit.Test)

Example 3 with BoltResult

use of org.neo4j.bolt.v1.runtime.spi.BoltResult in project neo4j by neo4j.

the class BoltInteraction method collectResults.

private static BoltResult collectResults(TransportConnection client) throws Exception {
    ResponseMessage message = TransportTestUtil.receiveOneResponseMessage(client);
    List<String> fieldNames = new ArrayList<>();
    List<Map<String, Object>> result = new ArrayList<>();
    if (message instanceof SuccessMessage) {
        Map<String, Object> metadata = ((SuccessMessage) message).meta();
        fieldNames = (List<String>) metadata.get("fields");
    } else if (message instanceof FailureMessage) {
        FailureMessage failMessage = (FailureMessage) message;
        // drain ignoredMessage, ack failure, get successMessage
        TransportTestUtil.receiveOneResponseMessage(client);
        client.send(TransportTestUtil.chunk(reset()));
        TransportTestUtil.receiveOneResponseMessage(client);
        throw new AuthenticationException(failMessage.status(), failMessage.message());
    }
    do {
        message = TransportTestUtil.receiveOneResponseMessage(client);
        if (message instanceof RecordMessage) {
            Object[] row = ((RecordMessage) message).record().fields();
            Map<String, Object> rowMap = new HashMap<>();
            for (int i = 0; i < row.length; i++) {
                rowMap.put(fieldNames.get(i), row[i]);
            }
            result.add(rowMap);
        }
    } while (!(message instanceof SuccessMessage) && !(message instanceof FailureMessage));
    if (message instanceof FailureMessage) {
        FailureMessage failMessage = (FailureMessage) message;
        // ack failure, get successMessage
        client.send(TransportTestUtil.chunk(reset()));
        TransportTestUtil.receiveOneResponseMessage(client);
        throw new AuthenticationException(failMessage.status(), failMessage.message());
    }
    return new BoltResult(result);
}
Also used : AuthenticationException(org.neo4j.bolt.security.auth.AuthenticationException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ResponseMessage(org.neo4j.bolt.v1.messaging.message.ResponseMessage) SuccessMessage(org.neo4j.bolt.v1.messaging.message.SuccessMessage) FailureMessage(org.neo4j.bolt.v1.messaging.message.FailureMessage) RecordMessage(org.neo4j.bolt.v1.messaging.message.RecordMessage) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with BoltResult

use of org.neo4j.bolt.v1.runtime.spi.BoltResult in project neo4j by neo4j.

the class TransactionStateMachineSPI method executeQuery.

@Override
public BoltResultHandle executeQuery(BoltQuerySource querySource, SecurityContext securityContext, String statement, Map<String, Object> params, ThrowingAction<KernelException> onFail) throws QueryExecutionKernelException {
    InternalTransaction internalTransaction = queryService.beginTransaction(implicit, securityContext);
    ClientConnectionInfo sourceDetails = new BoltConnectionInfo(querySource.principalName, querySource.clientName, querySource.connectionDescriptor.clientAddress, querySource.connectionDescriptor.serverAddress);
    TransactionalContext transactionalContext = contextFactory.newContext(sourceDetails, internalTransaction, statement, params);
    return new BoltResultHandle() {

        @Override
        public BoltResult start() throws KernelException {
            try {
                Result run = queryExecutionEngine.executeQuery(statement, params, transactionalContext);
                return new CypherAdapterStream(run, clock);
            } catch (KernelException e) {
                onFail.apply();
                throw new QueryExecutionKernelException(e);
            } catch (Throwable e) {
                onFail.apply();
                throw e;
            }
        }

        @Override
        public void terminate() {
            transactionalContext.terminate();
        }
    };
}
Also used : ClientConnectionInfo(org.neo4j.kernel.impl.query.clientconnection.ClientConnectionInfo) BoltConnectionInfo(org.neo4j.kernel.impl.query.clientconnection.BoltConnectionInfo) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) BoltResultHandle(org.neo4j.bolt.v1.runtime.TransactionStateMachine.BoltResultHandle) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) KernelException(org.neo4j.kernel.api.exceptions.KernelException) BoltResult(org.neo4j.bolt.v1.runtime.spi.BoltResult) Result(org.neo4j.graphdb.Result)

Aggregations

BoltResult (org.neo4j.bolt.v1.runtime.spi.BoltResult)3 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 AuthenticationException (org.neo4j.bolt.security.auth.AuthenticationException)1 BoltMatchers.failedWithStatus (org.neo4j.bolt.testing.BoltMatchers.failedWithStatus)1 BoltResponseRecorder (org.neo4j.bolt.testing.BoltResponseRecorder)1 FailureMessage (org.neo4j.bolt.v1.messaging.message.FailureMessage)1 RecordMessage (org.neo4j.bolt.v1.messaging.message.RecordMessage)1 ResponseMessage (org.neo4j.bolt.v1.messaging.message.ResponseMessage)1 SuccessMessage (org.neo4j.bolt.v1.messaging.message.SuccessMessage)1 BoltResponseHandler (org.neo4j.bolt.v1.runtime.BoltResponseHandler)1 BoltStateMachine (org.neo4j.bolt.v1.runtime.BoltStateMachine)1 Neo4jError (org.neo4j.bolt.v1.runtime.Neo4jError)1 BoltResultHandle (org.neo4j.bolt.v1.runtime.TransactionStateMachine.BoltResultHandle)1 Result (org.neo4j.graphdb.Result)1 KernelException (org.neo4j.kernel.api.exceptions.KernelException)1