Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.

Commit 9c436fd

Browse files
GH-32 - Use writeTransaction functions for health checks.
1 parent e46e1d1 commit 9c436fd

File tree

4 files changed

+80
-30
lines changed

4 files changed

+80
-30
lines changed

neo4j-java-driver-spring-boot-autoconfigure/src/main/java/org/neo4j/driver/springframework/boot/actuate/Neo4jHealthIndicator.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import org.neo4j.driver.Driver;
2525
import org.neo4j.driver.Result;
2626
import org.neo4j.driver.Session;
27-
import org.neo4j.driver.exceptions.SessionExpiredException;
2827
import org.neo4j.driver.SessionConfig;
28+
import org.neo4j.driver.exceptions.SessionExpiredException;
2929
import org.neo4j.driver.summary.DatabaseInfo;
3030
import org.neo4j.driver.summary.ResultSummary;
3131
import org.neo4j.driver.summary.ServerInfo;
@@ -94,7 +94,7 @@ protected void doHealthCheck(Health.Builder builder) {
9494
* Applies the given {@link ResultSummary} to the {@link Health.Builder builder} without actually calling {@code build}.
9595
*
9696
* @param resultSummaryWithEdition the result summary returned by the server
97-
* @param builder the health builder to be modified
97+
* @param builder the health builder to be modified
9898
* @return the modified health builder
9999
*/
100100
static Health.Builder buildStatusUp(ResultSummaryWithEdition resultSummaryWithEdition, Health.Builder builder) {
@@ -113,13 +113,17 @@ static Health.Builder buildStatusUp(ResultSummaryWithEdition resultSummaryWithEd
113113
}
114114

115115
ResultSummaryWithEdition runHealthCheckQuery() {
116-
// We use WRITE here to make sure UP is returned for a server that supports
117-
// all possible workloads
116+
118117
try (Session session = this.driver.session(DEFAULT_SESSION_CONFIG)) {
119-
Result result = session.run(CYPHER);
120-
String edition = result.single().get("edition").asString();
121-
ResultSummary resultSummary = result.consume();
122-
return new ResultSummaryWithEdition(resultSummary, edition);
118+
119+
// We use WRITE here to make sure UP is returned for a server that supports
120+
// all possible workloads
121+
return session.writeTransaction(tx -> {
122+
Result result = tx.run(CYPHER);
123+
String edition = result.single().get("edition").asString();
124+
ResultSummary resultSummary = result.consume();
125+
return new ResultSummaryWithEdition(resultSummary, edition);
126+
});
123127
}
124128
}
125129
}

neo4j-java-driver-spring-boot-autoconfigure/src/main/java/org/neo4j/driver/springframework/boot/actuate/Neo4jReactiveHealthIndicator.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import static org.neo4j.driver.springframework.boot.actuate.Neo4jHealthIndicator.*;
2222

23+
import reactor.core.publisher.Flux;
2324
import reactor.core.publisher.Mono;
2425

2526
import org.apache.commons.logging.Log;
@@ -28,6 +29,8 @@
2829
import org.neo4j.driver.exceptions.SessionExpiredException;
2930
import org.neo4j.driver.reactive.RxResult;
3031
import org.neo4j.driver.reactive.RxSession;
32+
import org.neo4j.driver.reactive.RxTransactionWork;
33+
import org.reactivestreams.Publisher;
3134
import org.springframework.boot.actuate.health.AbstractReactiveHealthIndicator;
3235
import org.springframework.boot.actuate.health.Health;
3336
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
@@ -61,16 +64,20 @@ protected Mono<Health> doHealthCheck(Health.Builder builder) {
6164
}
6265

6366
Mono<ResultSummaryWithEdition> runHealthCheckQuery() {
64-
// We use WRITE here to make sure UP is returned for a server that supports
65-
// all possible workloads
66-
return Mono.using(
67-
() -> this.driver.rxSession(DEFAULT_SESSION_CONFIG),
68-
session -> {
69-
RxResult result = session.run(Neo4jHealthIndicator.CYPHER);
67+
68+
RxTransactionWork<Publisher<ResultSummaryWithEdition>> txFunction =
69+
tx -> {
70+
RxResult result = tx.run(Neo4jHealthIndicator.CYPHER);
7071
return Mono.from(result.records()).map(record -> record.get("edition").asString())
7172
.zipWhen(edition -> Mono.from(result.consume()), (e, r) -> new ResultSummaryWithEdition(r, e));
72-
},
73+
};
74+
75+
return Flux.usingWhen(
76+
Mono.fromSupplier(() -> this.driver.rxSession(DEFAULT_SESSION_CONFIG)),
77+
// We use WRITE here to make sure UP is returned for a server that supports
78+
// all possible workloads
79+
s -> s.writeTransaction(txFunction),
7380
RxSession::close
74-
);
81+
).single();
7582
}
7683
}

neo4j-java-driver-spring-boot-autoconfigure/src/test/java/org/neo4j/driver/springframework/boot/actuate/Neo4jHealthIndicatorTest.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
import org.mockito.junit.jupiter.MockitoExtension;
3030
import org.neo4j.driver.Result;
3131
import org.neo4j.driver.Session;
32+
import org.neo4j.driver.SessionConfig;
33+
import org.neo4j.driver.Transaction;
34+
import org.neo4j.driver.TransactionWork;
3235
import org.neo4j.driver.Values;
3336
import org.neo4j.driver.exceptions.ServiceUnavailableException;
3437
import org.neo4j.driver.exceptions.SessionExpiredException;
35-
import org.neo4j.driver.SessionConfig;
3638
import org.springframework.boot.actuate.health.Health;
3739
import org.springframework.boot.actuate.health.Status;
3840

@@ -48,6 +50,9 @@ class Neo4jHealthIndicatorTest extends Neo4jHealthIndicatorTestBase {
4850
@Mock
4951
private Result statementResult;
5052

53+
@Mock
54+
private Transaction transaction;
55+
5156
@Test
5257
void shouldWorkWithoutDatabaseName() {
5358
when(this.serverInfo.version()).thenReturn("4711");
@@ -60,7 +65,11 @@ void shouldWorkWithoutDatabaseName() {
6065
when(record.get("edition")).thenReturn(Values.value("some edition"));
6166
when(this.statementResult.single()).thenReturn(this.record);
6267
when(this.statementResult.consume()).thenReturn(this.resultSummary);
63-
when(this.session.run(anyString())).thenReturn(this.statementResult);
68+
when(this.transaction.run(anyString())).thenReturn(this.statementResult);
69+
when(this.session.writeTransaction(any(TransactionWork.class))).then(invocationOnMock -> {
70+
TransactionWork<ResultSummaryWithEdition> tw = invocationOnMock.getArgument(0);
71+
return tw.execute(transaction);
72+
});
6473

6574
when(this.driver.session(any(SessionConfig.class))).thenReturn(this.session);
6675

@@ -84,7 +93,11 @@ void shouldWorkWithEmptyDatabaseName() {
8493
when(record.get("edition")).thenReturn(Values.value("some edition"));
8594
when(this.statementResult.single()).thenReturn(this.record);
8695
when(this.statementResult.consume()).thenReturn(this.resultSummary);
87-
when(this.session.run(anyString())).thenReturn(this.statementResult);
96+
when(this.transaction.run(anyString())).thenReturn(this.statementResult);
97+
when(this.session.writeTransaction(any(TransactionWork.class))).then(invocationOnMock -> {
98+
TransactionWork<ResultSummaryWithEdition> tw = invocationOnMock.getArgument(0);
99+
return tw.execute(transaction);
100+
});
88101

89102
when(driver.session(any(SessionConfig.class))).thenReturn(this.session);
90103

@@ -103,8 +116,11 @@ void neo4jIsUp() {
103116

104117
when(this.statementResult.single()).thenReturn(this.record);
105118
when(this.statementResult.consume()).thenReturn(this.resultSummary);
106-
when(this.session.run(anyString())).thenReturn(this.statementResult);
107-
119+
when(this.transaction.run(anyString())).thenReturn(this.statementResult);
120+
when(this.session.writeTransaction(any(TransactionWork.class))).then(invocationOnMock -> {
121+
TransactionWork<ResultSummaryWithEdition> tw = invocationOnMock.getArgument(0);
122+
return tw.execute(transaction);
123+
});
108124
when(this.driver.session(any(SessionConfig.class))).thenReturn(this.session);
109125

110126
Neo4jHealthIndicator healthIndicator = new Neo4jHealthIndicator(this.driver);
@@ -115,7 +131,8 @@ void neo4jIsUp() {
115131
assertThat(health.getDetails()).containsEntry("edition", "ultimate collectors edition");
116132

117133
verify(session).close();
118-
verifyNoMoreInteractions(this.driver, this.session, this.statementResult, this.resultSummary, this.serverInfo, this.databaseInfo);
134+
verifyNoMoreInteractions(this.driver, this.session, this.statementResult, this.resultSummary, this.serverInfo,
135+
this.databaseInfo, this.transaction);
119136
}
120137

121138
@Test
@@ -126,12 +143,16 @@ void neo4jSessionIsExpiredOnce() {
126143
prepareSharedMocks();
127144
when(this.statementResult.single()).thenReturn(this.record);
128145
when(this.statementResult.consume()).thenReturn(this.resultSummary);
129-
when(this.session.run(anyString())).thenAnswer(invocation -> {
146+
when(this.transaction.run(anyString())).thenAnswer(invocation -> {
130147
if (cnt.compareAndSet(0, 1)) {
131148
throw new SessionExpiredException("Session expired");
132149
}
133150
return Neo4jHealthIndicatorTest.this.statementResult;
134151
});
152+
when(this.session.writeTransaction(any(TransactionWork.class))).then(invocationOnMock -> {
153+
TransactionWork<ResultSummaryWithEdition> tw = invocationOnMock.getArgument(0);
154+
return tw.execute(transaction);
155+
});
135156
when(driver.session(any(SessionConfig.class))).thenReturn(this.session);
136157

137158
Neo4jHealthIndicator healthIndicator = new Neo4jHealthIndicator(this.driver);
@@ -141,7 +162,8 @@ void neo4jSessionIsExpiredOnce() {
141162
assertThat(health.getDetails()).containsEntry("server", "4711@Zu Hause");
142163

143164
verify(this.session, times(2)).close();
144-
verifyNoMoreInteractions(this.driver, this.session, this.statementResult, this.resultSummary, this.serverInfo, this.databaseInfo);
165+
verifyNoMoreInteractions(this.driver, this.session, this.statementResult, this.resultSummary, this.serverInfo,
166+
this.databaseInfo, this.transaction);
145167
}
146168

147169
@Test
@@ -155,6 +177,7 @@ void neo4jSessionIsDown() {
155177
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
156178
assertThat(health.getDetails()).containsKeys("error");
157179

158-
verifyNoMoreInteractions(this.driver, this.session, this.statementResult, this.resultSummary, this.serverInfo, this.databaseInfo);
180+
verifyNoMoreInteractions(this.driver, this.session, this.statementResult, this.resultSummary, this.serverInfo,
181+
this.databaseInfo, this.transaction);
159182
}
160183
}

neo4j-java-driver-spring-boot-autoconfigure/src/test/java/org/neo4j/driver/springframework/boot/actuate/Neo4jReactiveHealthIndicatorTest.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import org.neo4j.driver.SessionConfig;
3636
import org.neo4j.driver.reactive.RxResult;
3737
import org.neo4j.driver.reactive.RxSession;
38+
import org.neo4j.driver.reactive.RxTransaction;
39+
import org.neo4j.driver.reactive.RxTransactionWork;
3840
import org.springframework.boot.actuate.health.Status;
3941

4042
/**
@@ -49,13 +51,21 @@ class Neo4jReactiveHealthIndicatorTest extends Neo4jHealthIndicatorTestBase {
4951
@Mock
5052
private RxResult statementResult;
5153

54+
@Mock
55+
private RxTransaction transaction;
56+
5257
@Test
5358
void neo4jIsUp() {
5459

5560
prepareSharedMocks();
5661
when(statementResult.records()).thenReturn(Mono.just(record));
5762
when(statementResult.consume()).thenReturn(Mono.just(resultSummary));
58-
when(session.run(anyString())).thenReturn(statementResult);
63+
when(transaction.run(anyString())).thenReturn(this.statementResult);
64+
when(session.writeTransaction(any(RxTransactionWork.class))).then(invocationOnMock -> {
65+
RxTransactionWork<ResultSummaryWithEdition> tw = invocationOnMock.getArgument(0);
66+
return tw.execute(transaction);
67+
});
68+
when(session.close()).thenReturn(Mono.empty());
5969

6070
when(driver.rxSession(any(SessionConfig.class))).thenReturn(session);
6171

@@ -71,7 +81,7 @@ void neo4jIsUp() {
7181
.verifyComplete();
7282

7383
verify(session).close();
74-
verifyNoMoreInteractions(driver, session, statementResult, resultSummary, serverInfo, databaseInfo);
84+
verifyNoMoreInteractions(driver, session, statementResult, resultSummary, serverInfo, databaseInfo, transaction);
7585
}
7686

7787
@Test
@@ -82,12 +92,18 @@ void neo4jSessionIsExpiredOnce() {
8292
prepareSharedMocks();
8393
when(statementResult.records()).thenReturn(Mono.just(record));
8494
when(statementResult.consume()).thenReturn(Mono.just(resultSummary));
85-
when(session.run(anyString())).thenAnswer(invocation -> {
95+
when(transaction.run(anyString())).thenAnswer(invocation -> {
8696
if (cnt.compareAndSet(0, 1)) {
8797
throw new SessionExpiredException("Session expired");
8898
}
8999
return statementResult;
90100
});
101+
when(session.writeTransaction(any(RxTransactionWork.class))).then(invocationOnMock -> {
102+
RxTransactionWork<ResultSummaryWithEdition> tw = invocationOnMock.getArgument(0);
103+
return tw.execute(transaction);
104+
});
105+
when(session.close()).thenReturn(Mono.empty());
106+
91107
when(driver.rxSession(any(SessionConfig.class))).thenReturn(session);
92108

93109
Neo4jReactiveHealthIndicator healthIndicator = new Neo4jReactiveHealthIndicator(driver);
@@ -102,7 +118,7 @@ void neo4jSessionIsExpiredOnce() {
102118
.verifyComplete();
103119

104120
verify(session, times(2)).close();
105-
verifyNoMoreInteractions(driver, session, statementResult, resultSummary, serverInfo, databaseInfo);
121+
verifyNoMoreInteractions(driver, session, statementResult, resultSummary, serverInfo, databaseInfo, transaction);
106122
}
107123

108124
@Test
@@ -120,6 +136,6 @@ void neo4jSessionIsDown() {
120136
})
121137
.verifyComplete();
122138

123-
verifyNoMoreInteractions(driver, session, statementResult, resultSummary, serverInfo, databaseInfo);
139+
verifyNoMoreInteractions(driver, session, statementResult, resultSummary, serverInfo, databaseInfo, transaction);
124140
}
125141
}

0 commit comments

Comments
 (0)