|
| 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.BeforeAll; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.neo4j.driver.Driver; |
| 27 | +import org.neo4j.driver.GraphDatabase; |
| 28 | +import org.neo4j.driver.Query; |
| 29 | +import org.neo4j.driver.Session; |
| 30 | +import org.neo4j.driver.Transaction; |
| 31 | +import org.neo4j.driver.reactivestreams.ReactiveResult; |
| 32 | +import org.neo4j.driver.reactivestreams.ReactiveSession; |
| 33 | +import org.neo4j.driver.reactivestreams.ReactiveTransaction; |
| 34 | +import org.springframework.beans.factory.annotation.Autowired; |
| 35 | +import org.springframework.context.annotation.Bean; |
| 36 | +import org.springframework.context.annotation.Configuration; |
| 37 | +import org.springframework.data.neo4j.config.AbstractReactiveNeo4jConfig; |
| 38 | +import org.springframework.data.neo4j.repository.config.EnableReactiveNeo4jRepositories; |
| 39 | +import org.springframework.data.neo4j.test.Neo4jExtension; |
| 40 | +import org.springframework.data.neo4j.test.Neo4jIntegrationTest; |
| 41 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 42 | + |
| 43 | +import reactor.core.publisher.Flux; |
| 44 | +import reactor.core.publisher.Mono; |
| 45 | +import reactor.test.StepVerifier; |
| 46 | + |
| 47 | +/** |
| 48 | + * @author Michael J. Simons |
| 49 | + */ |
| 50 | +@Neo4jIntegrationTest |
| 51 | +class ReactiveConnectionAcquisitionIT { |
| 52 | + |
| 53 | + protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport; |
| 54 | + |
| 55 | + @BeforeAll |
| 56 | + protected static void setupData(@Autowired Driver driver) { |
| 57 | + try (Session session = driver.session()) { |
| 58 | + try (Transaction transaction = session.beginTransaction()) { |
| 59 | + transaction.run("MATCH (n) detach delete n"); |
| 60 | + transaction.run(""" |
| 61 | + CREATE (m:Movie {title: "I don't want warnings", id: randomUUID()}) |
| 62 | + """ |
| 63 | + ).consume(); |
| 64 | + transaction.commit(); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + // GH-2632 |
| 71 | + void connectionAcquisitionAfterErrorViaSDNTxManagerShouldWork(@Autowired MovieRepository movieRepository, @Autowired Driver driver) { |
| 72 | + UUID id = UUID.randomUUID(); |
| 73 | + Flux |
| 74 | + .range(1, 5) |
| 75 | + .flatMap(i -> movieRepository.findById(id).switchIfEmpty(Mono.error(new RuntimeException()))) |
| 76 | + .then() |
| 77 | + .as(StepVerifier::create) |
| 78 | + .verifyError(); |
| 79 | + |
| 80 | + try (Session session = driver.session()) { |
| 81 | + long aNumber = session.run("RETURN 1").single().get(0).asLong(); |
| 82 | + assertThat(aNumber).isOne(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Test // GH-2632 |
| 87 | + void connectionAcquisitionAfterErrorViaImplicitTXShouldWork(@Autowired Driver driver) { |
| 88 | + Flux |
| 89 | + .range(1, 5) |
| 90 | + .flatMap( |
| 91 | + i -> { |
| 92 | + Query query = new Query("MATCH (p:Product) WHERE p.id = $id RETURN p.title", Collections.singletonMap("id", 0)); |
| 93 | + return Flux.usingWhen( |
| 94 | + Mono.fromSupplier(() -> driver.session(ReactiveSession.class)), |
| 95 | + session -> Flux.from(session.run(query)) |
| 96 | + .flatMap(result -> Flux.from(result.records())) |
| 97 | + .map(record -> record.get(0).asString()), |
| 98 | + session -> Mono.fromDirect(session.close()) |
| 99 | + ).switchIfEmpty(Mono.error(new RuntimeException())); |
| 100 | + } |
| 101 | + ) |
| 102 | + .then() |
| 103 | + .as(StepVerifier::create) |
| 104 | + .verifyError(); |
| 105 | + |
| 106 | + try (Session session = driver.session()) { |
| 107 | + long aNumber = session.run("RETURN 1").single().get(0).asLong(); |
| 108 | + assertThat(aNumber).isOne(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + record SessionAndTx(ReactiveSession session, ReactiveTransaction tx) { |
| 113 | + } |
| 114 | + |
| 115 | + @Test // GH-2632 |
| 116 | + void connectionAcquisitionAfterErrorViaExplicitTXShouldWork(@Autowired Driver driver) { |
| 117 | + Flux |
| 118 | + .range(1, 5) |
| 119 | + .flatMap( |
| 120 | + i -> { |
| 121 | + Mono<SessionAndTx> f = Mono |
| 122 | + .just(driver.session(ReactiveSession.class)) |
| 123 | + .flatMap(s -> Mono.fromDirect(s.beginTransaction()).map(tx -> new SessionAndTx(s, tx))); |
| 124 | + return Flux.usingWhen(f, |
| 125 | + h -> Flux.from(h.tx.run("MATCH (n) WHERE false = true RETURN n")).flatMap(ReactiveResult::records), |
| 126 | + h -> Mono.from(h.tx.commit()).then(Mono.from(h.session.close())), |
| 127 | + (h, e) -> Mono.from(h.tx.rollback()).then(Mono.from(h.session.close())), |
| 128 | + h -> Mono.from(h.tx.rollback()).then(Mono.from(h.session.close())) |
| 129 | + ).switchIfEmpty(Mono.error(new RuntimeException())); |
| 130 | + } |
| 131 | + ) |
| 132 | + .then() |
| 133 | + .as(StepVerifier::create) |
| 134 | + .verifyError(); |
| 135 | + |
| 136 | + try (Session session = driver.session()) { |
| 137 | + long aNumber = session.run("RETURN 1").single().get(0).asLong(); |
| 138 | + assertThat(aNumber).isOne(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Configuration |
| 143 | + @EnableTransactionManagement |
| 144 | + @EnableReactiveNeo4jRepositories |
| 145 | + static class Config extends AbstractReactiveNeo4jConfig { |
| 146 | + |
| 147 | + @Bean |
| 148 | + public Driver driver() { |
| 149 | + var config = org.neo4j.driver.Config.builder() |
| 150 | + .withMaxConnectionPoolSize(2) |
| 151 | + .withConnectionAcquisitionTimeout(2, TimeUnit.SECONDS) |
| 152 | + .withLeakedSessionsLogging() |
| 153 | + .build(); |
| 154 | + return GraphDatabase.driver(neo4jConnectionSupport.url, neo4jConnectionSupport.authToken, config); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments