Search in sources :

Example 1 with SessionExpiredException

use of org.neo4j.driver.exceptions.SessionExpiredException in project neo4j by neo4j.

the class BoltStateHandlerTest method triesAgainOnSessionExpired.

@Test
public void triesAgainOnSessionExpired() throws Exception {
    Session sessionMock = mock(Session.class);
    Result resultMock = mock(Result.class);
    Record recordMock = mock(Record.class);
    Value valueMock = mock(Value.class);
    Driver driverMock = stubResultSummaryInAnOpenSession(resultMock, sessionMock, "neo4j-version");
    when(resultMock.list()).thenReturn(singletonList(recordMock));
    when(valueMock.toString()).thenReturn("999");
    when(recordMock.get(0)).thenReturn(valueMock);
    when(sessionMock.run(any(Query.class))).thenThrow(new SessionExpiredException("leaderswitch")).thenReturn(resultMock);
    OfflineBoltStateHandler boltStateHandler = new OfflineBoltStateHandler(driverMock);
    boltStateHandler.connect();
    BoltResult boltResult = boltStateHandler.runCypher("RETURN 999", new HashMap<>()).get();
    verify(driverMock, times(2)).session(any());
    verify(sessionMock, times(2)).run(any(Query.class));
    assertEquals("999", boltResult.getRecords().get(0).get(0).toString());
}
Also used : Query(org.neo4j.driver.Query) HashMap(java.util.HashMap) Value(org.neo4j.driver.Value) Driver(org.neo4j.driver.Driver) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) Record(org.neo4j.driver.Record) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) Session(org.neo4j.driver.Session) FakeSession(org.neo4j.shell.test.bolt.FakeSession) Result(org.neo4j.driver.Result) Test(org.junit.Test)

Example 2 with SessionExpiredException

use of org.neo4j.driver.exceptions.SessionExpiredException in project spring-boot by spring-projects.

the class Neo4jHealthIndicatorTests method neo4jIsUpWithOneSessionExpiredException.

@Test
void neo4jIsUpWithOneSessionExpiredException() {
    ResultSummary resultSummary = ResultSummaryMock.createResultSummary("My Home", "");
    Session session = mock(Session.class);
    Result statementResult = mockStatementResult(resultSummary, "4711", "some edition");
    AtomicInteger count = new AtomicInteger();
    given(session.run(anyString())).will((invocation) -> {
        if (count.compareAndSet(0, 1)) {
            throw new SessionExpiredException("Session expired");
        }
        return statementResult;
    });
    Driver driver = mock(Driver.class);
    given(driver.session(any(SessionConfig.class))).willReturn(session);
    Neo4jHealthIndicator healthIndicator = new Neo4jHealthIndicator(driver);
    Health health = healthIndicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.UP);
    assertThat(health.getDetails()).containsEntry("server", "4711@My Home");
    then(session).should(times(2)).close();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Health(org.springframework.boot.actuate.health.Health) ResultSummary(org.neo4j.driver.summary.ResultSummary) Driver(org.neo4j.driver.Driver) SessionConfig(org.neo4j.driver.SessionConfig) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) Session(org.neo4j.driver.Session) Result(org.neo4j.driver.Result) Test(org.junit.jupiter.api.Test)

Example 3 with SessionExpiredException

use of org.neo4j.driver.exceptions.SessionExpiredException in project spring-boot by spring-projects.

the class Neo4jReactiveHealthIndicatorTests method neo4jIsUpWithOneSessionExpiredException.

@Test
void neo4jIsUpWithOneSessionExpiredException() {
    ResultSummary resultSummary = ResultSummaryMock.createResultSummary("My Home", "");
    RxSession session = mock(RxSession.class);
    RxResult statementResult = mockStatementResult(resultSummary, "4711", "some edition");
    AtomicInteger count = new AtomicInteger();
    given(session.run(anyString())).will((invocation) -> {
        if (count.compareAndSet(0, 1)) {
            throw new SessionExpiredException("Session expired");
        }
        return statementResult;
    });
    Driver driver = mock(Driver.class);
    given(driver.rxSession(any(SessionConfig.class))).willReturn(session);
    Neo4jReactiveHealthIndicator healthIndicator = new Neo4jReactiveHealthIndicator(driver);
    healthIndicator.health().as(StepVerifier::create).consumeNextWith((health) -> {
        assertThat(health.getStatus()).isEqualTo(Status.UP);
        assertThat(health.getDetails()).containsEntry("server", "4711@My Home");
        assertThat(health.getDetails()).containsEntry("edition", "some edition");
    }).verifyComplete();
    then(session).should(times(2)).close();
}
Also used : Status(org.springframework.boot.actuate.health.Status) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Driver(org.neo4j.driver.Driver) StepVerifier(reactor.test.StepVerifier) RxSession(org.neo4j.driver.reactive.RxSession) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) BDDMockito.then(org.mockito.BDDMockito.then) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) RxResult(org.neo4j.driver.reactive.RxResult) Test(org.junit.jupiter.api.Test) Values(org.neo4j.driver.Values) SessionConfig(org.neo4j.driver.SessionConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BDDMockito.given(org.mockito.BDDMockito.given) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ResultSummary(org.neo4j.driver.summary.ResultSummary) Record(org.neo4j.driver.Record) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) RxSession(org.neo4j.driver.reactive.RxSession) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResultSummary(org.neo4j.driver.summary.ResultSummary) Driver(org.neo4j.driver.Driver) SessionConfig(org.neo4j.driver.SessionConfig) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) StepVerifier(reactor.test.StepVerifier) RxResult(org.neo4j.driver.reactive.RxResult) Test(org.junit.jupiter.api.Test)

Example 4 with SessionExpiredException

use of org.neo4j.driver.exceptions.SessionExpiredException in project neo4j by neo4j.

the class BoltStateHandler method connect.

@Override
public ConnectionConfig connect(@Nonnull ConnectionConfig connectionConfig, ThrowingAction<CommandException> command) throws CommandException {
    if (isConnected()) {
        throw new CommandException("Already connected");
    }
    final AuthToken authToken = AuthTokens.basic(connectionConfig.username(), connectionConfig.password());
    try {
        String previousDatabaseName = activeDatabaseNameAsSetByUser;
        try {
            activeDatabaseNameAsSetByUser = connectionConfig.database();
            driver = getDriver(connectionConfig, authToken);
            reconnect(activeDatabaseNameAsSetByUser, previousDatabaseName, command);
        } catch (ServiceUnavailableException | SessionExpiredException e) {
            String scheme = connectionConfig.scheme();
            String fallbackScheme;
            switch(scheme) {
                case Scheme.NEO4J_URI_SCHEME:
                    fallbackScheme = Scheme.BOLT_URI_SCHEME;
                    break;
                case Scheme.NEO4J_LOW_TRUST_URI_SCHEME:
                    fallbackScheme = Scheme.BOLT_LOW_TRUST_URI_SCHEME;
                    break;
                case Scheme.NEO4J_HIGH_TRUST_URI_SCHEME:
                    fallbackScheme = Scheme.BOLT_HIGH_TRUST_URI_SCHEME;
                    break;
                default:
                    throw e;
            }
            connectionConfig = new ConnectionConfig(fallbackScheme, connectionConfig.host(), connectionConfig.port(), connectionConfig.username(), connectionConfig.password(), connectionConfig.encryption(), connectionConfig.database());
            try {
                driver = getDriver(connectionConfig, authToken);
                reconnect(activeDatabaseNameAsSetByUser, previousDatabaseName, command);
            } catch (Throwable fallbackThrowable) {
                // Throw the original exception to not cause confusion.
                throw e;
            }
        }
    } catch (Throwable t) {
        try {
            silentDisconnect();
        } catch (Exception e) {
            t.addSuppressed(e);
        }
        throw t;
    }
    return connectionConfig;
}
Also used : AuthToken(org.neo4j.driver.AuthToken) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) CommandException(org.neo4j.shell.exception.CommandException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ConnectionConfig(org.neo4j.shell.ConnectionConfig) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) Versions.isPasswordChangeRequiredException(org.neo4j.shell.util.Versions.isPasswordChangeRequiredException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ClientException(org.neo4j.driver.exceptions.ClientException) Neo4jException(org.neo4j.driver.exceptions.Neo4jException) CommandException(org.neo4j.shell.exception.CommandException)

Aggregations

SessionExpiredException (org.neo4j.driver.exceptions.SessionExpiredException)4 Driver (org.neo4j.driver.Driver)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.jupiter.api.Test)2 Record (org.neo4j.driver.Record)2 Result (org.neo4j.driver.Result)2 Session (org.neo4j.driver.Session)2 SessionConfig (org.neo4j.driver.SessionConfig)2 ServiceUnavailableException (org.neo4j.driver.exceptions.ServiceUnavailableException)2 ResultSummary (org.neo4j.driver.summary.ResultSummary)2 HashMap (java.util.HashMap)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.Test)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 BDDMockito.given (org.mockito.BDDMockito.given)1 BDDMockito.then (org.mockito.BDDMockito.then)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.times (org.mockito.Mockito.times)1 AuthToken (org.neo4j.driver.AuthToken)1