|
| 1 | +/* |
| 2 | + * Copyright 2011-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.neo4j.integration.issues.gh2632; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.UUID; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.neo4j.driver.Driver; |
| 26 | +import org.neo4j.driver.GraphDatabase; |
| 27 | +import org.neo4j.driver.Query; |
| 28 | +import org.neo4j.driver.Session; |
| 29 | +import org.neo4j.driver.reactive.RxResult; |
| 30 | +import org.neo4j.driver.reactive.RxSession; |
| 31 | +import org.neo4j.driver.reactive.RxTransaction; |
| 32 | +import org.springframework.beans.factory.annotation.Autowired; |
| 33 | +import org.springframework.context.annotation.Bean; |
| 34 | +import org.springframework.context.annotation.Configuration; |
| 35 | +import org.springframework.data.neo4j.config.AbstractReactiveNeo4jConfig; |
| 36 | +import org.springframework.data.neo4j.repository.config.EnableReactiveNeo4jRepositories; |
| 37 | +import org.springframework.data.neo4j.test.Neo4jExtension; |
| 38 | +import org.springframework.data.neo4j.test.Neo4jIntegrationTest; |
| 39 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 40 | + |
| 41 | +import reactor.core.publisher.Flux; |
| 42 | +import reactor.core.publisher.Mono; |
| 43 | +import reactor.test.StepVerifier; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author Michael J. Simons |
| 47 | + */ |
| 48 | +@Neo4jIntegrationTest |
| 49 | +class ReactiveConnectionAcquisitionIT { |
| 50 | + |
| 51 | + protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport; |
| 52 | + |
| 53 | + @Test // GH-2632 |
| 54 | + void connectionAcquisitionAfterErrorViaSDNTxManagerShouldWork(@Autowired MovieRepository movieRepository, @Autowired Driver driver) { |
| 55 | + UUID id = UUID.randomUUID(); |
| 56 | + Flux |
| 57 | + .range(1, 5) |
| 58 | + .flatMap( |
| 59 | + i -> movieRepository |
| 60 | + .findById(id) |
| 61 | + .switchIfEmpty(Mono.error(new RuntimeException())) |
| 62 | + ) |
| 63 | + .then() |
| 64 | + .as(StepVerifier::create) |
| 65 | + .verifyError(); |
| 66 | + |
| 67 | + try (Session session = driver.session()) { |
| 68 | + long aNumber = session.run("RETURN 1").single().get(0).asLong(); |
| 69 | + assertThat(aNumber).isOne(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test // GH-2632 |
| 74 | + void connectionAcquisitionAfterErrorViaImplicitTXShouldWork(@Autowired Driver driver) { |
| 75 | + Flux |
| 76 | + .range(1, 5) |
| 77 | + .flatMap( |
| 78 | + i -> { |
| 79 | + Query query = new Query("MATCH (p:Product) WHERE p.id = $id RETURN p.title", Collections.singletonMap("id", 0)); |
| 80 | + return Flux.usingWhen( |
| 81 | + Mono.fromSupplier(driver::rxSession), |
| 82 | + session -> Mono.fromSupplier(() -> session.run(query)) |
| 83 | + .flatMapMany(result -> Flux.from(result.records())) |
| 84 | + .map(record -> record.get(0).asString()), |
| 85 | + session -> Mono.fromDirect(session.close())) |
| 86 | + .switchIfEmpty(Mono.error(new RuntimeException())); |
| 87 | + } |
| 88 | + ) |
| 89 | + .then() |
| 90 | + .as(StepVerifier::create) |
| 91 | + .verifyError(); |
| 92 | + |
| 93 | + try (Session session = driver.session()) { |
| 94 | + long aNumber = session.run("RETURN 1").single().get(0).asLong(); |
| 95 | + assertThat(aNumber).isOne(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static class SessionAndTx { |
| 100 | + RxSession session; |
| 101 | + RxTransaction tx; |
| 102 | + |
| 103 | + SessionAndTx(RxSession session, RxTransaction tx) { |
| 104 | + this.session = session; |
| 105 | + this.tx = tx; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Test // GH-2632 |
| 110 | + void connectionAcquisitionAfterErrorViaExplicitTXShouldWork(@Autowired Driver driver) { |
| 111 | + Flux |
| 112 | + .range(1, 5) |
| 113 | + .flatMap( |
| 114 | + i -> { |
| 115 | + Mono<SessionAndTx> f = Mono |
| 116 | + .just(driver.rxSession()) |
| 117 | + .flatMap(s -> Mono.fromDirect(s.beginTransaction()).map(tx -> new SessionAndTx(s, tx))); |
| 118 | + return Flux.usingWhen(f, |
| 119 | + h -> Mono.fromSupplier(() -> h.tx.run("MATCH (n) WHERE false = true RETURN n")).flatMapMany(RxResult::records), |
| 120 | + h -> Mono.from(h.tx.commit()).then(Mono.from(h.session.close())), |
| 121 | + (h, e) -> Mono.from(h.tx.rollback()).then(Mono.from(h.session.close())), |
| 122 | + h -> Mono.from(h.tx.rollback()).then(Mono.from(h.session.close())) |
| 123 | + ).switchIfEmpty(Mono.error(new RuntimeException())); |
| 124 | + } |
| 125 | + ) |
| 126 | + .then() |
| 127 | + .as(StepVerifier::create) |
| 128 | + .verifyError(); |
| 129 | + |
| 130 | + try (Session session = driver.session()) { |
| 131 | + long aNumber = session.run("RETURN 1").single().get(0).asLong(); |
| 132 | + assertThat(aNumber).isOne(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @Configuration |
| 137 | + @EnableTransactionManagement |
| 138 | + @EnableReactiveNeo4jRepositories(considerNestedRepositories = true) |
| 139 | + static class Config extends AbstractReactiveNeo4jConfig { |
| 140 | + |
| 141 | + @Bean |
| 142 | + public Driver driver() { |
| 143 | + org.neo4j.driver.Config config = org.neo4j.driver.Config.builder() |
| 144 | + .withMaxConnectionPoolSize(2) |
| 145 | + .withConnectionAcquisitionTimeout(20, TimeUnit.SECONDS) |
| 146 | + .withLeakedSessionsLogging() |
| 147 | + .build(); |
| 148 | + return |
| 149 | + GraphDatabase.driver(neo4jConnectionSupport.url, neo4jConnectionSupport.authToken, config); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments