Search in sources :

Example 1 with ConnectionException

use of org.neo4j.server.http.cypher.format.api.ConnectionException in project neo4j by neo4j.

the class InvocationTest method shouldKeepTransactionOpenIfConnectionErrorWhenReadingStatementsInExplicitTransaction.

@Test
void shouldKeepTransactionOpenIfConnectionErrorWhenReadingStatementsInExplicitTransaction() {
    // given
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    TransactionHandle handle = getTransactionHandle(executionEngine, registry, false);
    InputEventStream inputEventStream = mock(InputEventStream.class);
    when(inputEventStream.read()).thenThrow(new ConnectionException("Connection error", new IOException("Broken pipe")));
    Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, true);
    // when
    var e = assertThrows(ConnectionException.class, () -> invocation.execute(outputEventStream));
    assertEquals("Connection error", e.getMessage());
    // then
    verify(transactionContext, never()).rollback();
    verify(transactionContext, never()).commit();
    verify(registry, never()).forget(1337L);
    verifyNoInteractions(outputEventStream);
}
Also used : InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) IOException(java.io.IOException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException) MemoryPool(org.neo4j.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Example 2 with ConnectionException

use of org.neo4j.server.http.cypher.format.api.ConnectionException in project neo4j by neo4j.

the class InvocationTest method shouldHandleConnectionErrorWhenWritingOutputInImplicitTransaction.

@Test
void shouldHandleConnectionErrorWhenWritingOutputInImplicitTransaction() {
    // given
    when(internalTransaction.execute("query", emptyMap())).thenReturn(executionResult);
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    TransactionHandle handle = getTransactionHandle(executionEngine, registry);
    InputEventStream inputEventStream = mock(InputEventStream.class);
    Statement statement = new Statement("query", map());
    when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
    mockDefaultResult();
    Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, true);
    doThrow(new ConnectionException("Connection error", new IOException("Broken pipe"))).when(outputEventStream).writeStatementEnd(any(), any(), any(), any());
    // when
    var e = assertThrows(ConnectionException.class, () -> invocation.execute(outputEventStream));
    assertEquals("Connection error", e.getMessage());
    // then
    verify(internalTransaction).rollback();
    verify(registry).forget(1337L);
    InOrder outputOrder = inOrder(outputEventStream);
    outputOrder.verify(outputEventStream).writeStatementStart(statement, List.of("c1", "c2", "c3"));
    verifyDefaultResultRows(outputOrder);
    outputOrder.verify(outputEventStream).writeStatementEnd(queryExecutionType, queryStatistics, executionPlanDescription, notifications);
    verifyNoMoreInteractions(outputEventStream);
}
Also used : InOrder(org.mockito.InOrder) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) Statement(org.neo4j.server.http.cypher.format.api.Statement) IOException(java.io.IOException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException) MemoryPool(org.neo4j.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Example 3 with ConnectionException

use of org.neo4j.server.http.cypher.format.api.ConnectionException in project neo4j by neo4j.

the class LineDelimitedEventSourceJoltSerializer method writeStatementEnd.

protected void writeStatementEnd(StatementEndEvent statementEndEvent) {
    try {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeFieldName("summary");
        jsonGenerator.writeStartObject();
        if (inputStatement.includeStats()) {
            writeStats(statementEndEvent.getQueryStatistics());
        }
        if (statementEndEvent.getQueryExecutionType().requestedExecutionPlanDescription()) {
            writeRootPlanDescription(statementEndEvent.getExecutionPlanDescription());
        }
        jsonGenerator.writeEndObject();
        jsonGenerator.writeEndObject();
        statementEndEvent.getNotifications().forEach(notifications::add);
        flush();
    } catch (JsonGenerationException e) {
        throw new IllegalStateException(e);
    } catch (IOException e) {
        throw new ConnectionException("Failed to write to the connection", e);
    }
}
Also used : IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException)

Example 4 with ConnectionException

use of org.neo4j.server.http.cypher.format.api.ConnectionException in project neo4j by neo4j.

the class LineDelimitedEventSourceJoltSerializer method writeNotifications.

private void writeNotifications() {
    if (notifications.isEmpty()) {
        return;
    }
    try {
        jsonGenerator.writeArrayFieldStart("notifications");
        try {
            for (Notification notification : notifications) {
                jsonGenerator.writeStartObject();
                try {
                    jsonGenerator.writeStringField("code", notification.getCode());
                    jsonGenerator.writeStringField("severity", notification.getSeverity().toString());
                    jsonGenerator.writeStringField("title", notification.getTitle());
                    jsonGenerator.writeStringField("description", notification.getDescription());
                    writePosition(notification.getPosition());
                } finally {
                    jsonGenerator.writeEndObject();
                }
            }
        } finally {
            jsonGenerator.writeEndArray();
        }
    } catch (IOException e) {
        throw new ConnectionException("Failed to write to the response stream", e);
    }
}
Also used : IOException(java.io.IOException) Notification(org.neo4j.graphdb.Notification) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException)

Example 5 with ConnectionException

use of org.neo4j.server.http.cypher.format.api.ConnectionException in project neo4j by neo4j.

the class LineDelimitedEventSourceJoltSerializer method writeStatementStart.

protected void writeStatementStart(StatementStartEvent statementStartEvent, InputStatement inputStatement) {
    this.inputStatement = inputStatement;
    try {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeFieldName("header");
        jsonGenerator.writeStartObject();
        Iterable<String> columns = statementStartEvent.getColumns();
        writeColumns(columns);
        jsonGenerator.writeEndObject();
        jsonGenerator.writeEndObject();
        flush();
    } catch (JsonGenerationException e) {
        throw new IllegalStateException(e);
    } catch (IOException e) {
        throw new ConnectionException("Failed to write to the connection", e);
    }
}
Also used : IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException)

Aggregations

IOException (java.io.IOException)21 ConnectionException (org.neo4j.server.http.cypher.format.api.ConnectionException)21 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)10 Test (org.junit.jupiter.api.Test)5 MemoryPool (org.neo4j.memory.MemoryPool)4 InputEventStream (org.neo4j.server.http.cypher.format.api.InputEventStream)4 Statement (org.neo4j.server.http.cypher.format.api.Statement)3 JsonParseException (com.fasterxml.jackson.core.JsonParseException)2 JsonToken (com.fasterxml.jackson.core.JsonToken)2 InOrder (org.mockito.InOrder)2 Notification (org.neo4j.graphdb.Notification)2 TransactionStateChecker (org.neo4j.server.http.cypher.TransactionStateChecker)2 FailureEvent (org.neo4j.server.http.cypher.format.api.FailureEvent)2 InputFormatException (org.neo4j.server.http.cypher.format.api.InputFormatException)2 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ArrayList (java.util.ArrayList)1 Log (org.neo4j.logging.Log)1