use of org.neo4j.bolt.v1.runtime.BoltConnectionFatality in project neo4j by neo4j.
the class TransactionIT method shouldReadYourOwnWrites.
@Test
public void shouldReadYourOwnWrites() throws Exception {
try (Transaction tx = env.graph().beginTx()) {
Node node = env.graph().createNode(Label.label("A"));
node.setProperty("prop", "one");
tx.success();
}
BinaryLatch latch = new BinaryLatch();
long dbVersion = env.lastClosedTxId();
Thread thread = new Thread() {
@Override
public void run() {
try (BoltStateMachine machine = env.newMachine(new BoltConnectionDescriptor(new InetSocketAddress("<testClient>", 56789), new InetSocketAddress("<writeServer>", 7468)))) {
machine.init(USER_AGENT, emptyMap(), null);
latch.await();
machine.run("MATCH (n:A) SET n.prop = 'two'", emptyMap(), nullResponseHandler());
machine.pullAll(nullResponseHandler());
} catch (BoltConnectionFatality connectionFatality) {
throw new RuntimeException(connectionFatality);
}
}
};
thread.start();
long dbVersionAfterWrite = dbVersion + 1;
try (BoltStateMachine machine = env.newMachine(new BoltConnectionDescriptor(new InetSocketAddress("<testClient>", 56789), new InetSocketAddress("<readServer>", 7468)))) {
BoltResponseRecorder recorder = new BoltResponseRecorder();
machine.init(USER_AGENT, emptyMap(), null);
latch.release();
final String bookmark = "neo4j:bookmark:v1:tx" + Long.toString(dbVersionAfterWrite);
machine.run("BEGIN", singletonMap("bookmark", bookmark), nullResponseHandler());
machine.pullAll(recorder);
machine.run("MATCH (n:A) RETURN n.prop", emptyMap(), nullResponseHandler());
machine.pullAll(recorder);
machine.run("COMMIT", emptyMap(), nullResponseHandler());
machine.pullAll(recorder);
assertThat(recorder.nextResponse(), succeededWithMetadata("bookmark", BOOKMARK_PATTERN));
assertThat(recorder.nextResponse(), succeededWithRecord("two"));
assertThat(recorder.nextResponse(), succeededWithMetadata("bookmark", BOOKMARK_PATTERN));
}
thread.join();
}
use of org.neo4j.bolt.v1.runtime.BoltConnectionFatality in project neo4j by neo4j.
the class TransactionIT method shouldTerminateQueriesEvenIfUsingPeriodicCommit.
@Test
public void shouldTerminateQueriesEvenIfUsingPeriodicCommit() throws Exception {
// Spawns a throttled HTTP server, runs a PERIODIC COMMIT that fetches data from this server,
// and checks that the query able to be terminated
// We start with 3, because that is how many actors we have -
// 1. the http server
// 2. the running query
// 3. the one terminating 2
final DoubleLatch latch = new DoubleLatch(3, true);
// This is used to block the http server between the first and second batch
final Barrier.Control barrier = new Barrier.Control();
// Serve CSV via local web server, let Jetty find a random port for us
Server server = createHttpServer(latch, barrier, 20, 30);
server.start();
int localPort = getLocalPort(server);
final BoltStateMachine[] machine = { null };
Thread thread = new Thread() {
@Override
public void run() {
try (BoltStateMachine stateMachine = env.newMachine(new BoltConnectionDescriptor(new InetSocketAddress("<testClient>", 56789), new InetSocketAddress("<writeServer>", 7468)))) {
machine[0] = stateMachine;
stateMachine.init(USER_AGENT, emptyMap(), null);
String query = format("USING PERIODIC COMMIT 10 LOAD CSV FROM 'http://localhost:%d' AS line " + "CREATE (n:A {id: line[0], square: line[1]}) " + "WITH count(*) as number " + "CREATE (n:ShouldNotExist)", localPort);
try {
latch.start();
stateMachine.run(query, emptyMap(), nullResponseHandler());
stateMachine.pullAll(nullResponseHandler());
} finally {
latch.finish();
}
} catch (BoltConnectionFatality connectionFatality) {
throw new RuntimeException(connectionFatality);
}
}
};
thread.setName("query runner");
thread.start();
// We block this thread here, waiting for the http server to spin up and the running query to get started
latch.startAndWaitForAllToStart();
Thread.sleep(1000);
// This is the call that RESETs the Bolt connection and will terminate the running query
machine[0].reset(nullResponseHandler());
barrier.release();
// We block again here, waiting for the running query to have been terminated, and for the server to have
// wrapped up and finished streaming http results
latch.finishAndWaitForAllToFinish();
// And now we check that the last node did not get created
try (Transaction ignored = env.graph().beginTx()) {
assertFalse("Query was not terminated in time - nodes were created!", env.graph().findNodes(Label.label("ShouldNotExist")).hasNext());
}
}
use of org.neo4j.bolt.v1.runtime.BoltConnectionFatality in project neo4j by neo4j.
the class BoltMatchers method verifyOneResponse.
public static void verifyOneResponse(BoltStateMachine.State initialState, ThrowingBiConsumer<BoltStateMachine, BoltResponseRecorder, BoltConnectionFatality> transition) throws AuthenticationException, BoltConnectionFatality {
BoltStateMachine machine = newMachine(initialState);
BoltResponseRecorder recorder = new BoltResponseRecorder();
try {
transition.accept(machine, recorder);
} catch (BoltConnectionFatality connectionFatality) {
// acceptable for invalid transitions
}
assertEquals(1, recorder.responseCount());
}
use of org.neo4j.bolt.v1.runtime.BoltConnectionFatality in project neo4j by neo4j.
the class BoltMatchers method canReset.
public static Matcher<BoltStateMachine> canReset() {
return new BaseMatcher<BoltStateMachine>() {
@Override
public boolean matches(final Object item) {
final BoltStateMachine machine = (BoltStateMachine) item;
final BoltResponseRecorder recorder = new BoltResponseRecorder();
try {
machine.reset(recorder);
return recorder.responseCount() == 1 && machine.state() == READY;
} catch (BoltConnectionFatality boltConnectionFatality) {
return false;
}
}
@Override
public void describeTo(Description description) {
description.appendText("can reset");
}
};
}
use of org.neo4j.bolt.v1.runtime.BoltConnectionFatality in project neo4j by neo4j.
the class MonitoredBoltWorkerFactoryTest method shouldSignalReceivedStartAndComplete.
@Test
public void shouldSignalReceivedStartAndComplete() throws Throwable {
// given
FakeClock clock = Clocks.fakeClock();
WorkerFactory delegate = mock(WorkerFactory.class);
BoltStateMachine machine = mock(BoltStateMachine.class);
when(delegate.newWorker(anyObject(), anyObject())).thenReturn(new BoltWorker() {
@Override
public void enqueue(Job job) {
clock.forward(1337, TimeUnit.MILLISECONDS);
try {
job.perform(machine);
} catch (BoltConnectionFatality connectionFatality) {
throw new RuntimeException(connectionFatality);
}
}
@Override
public void interrupt() {
throw new RuntimeException();
}
@Override
public void halt() {
throw new RuntimeException();
}
});
Monitors monitors = new Monitors();
CountingSessionMonitor monitor = new CountingSessionMonitor();
monitors.addMonitorListener(monitor);
MonitoredWorkerFactory workerFactory = new MonitoredWorkerFactory(monitors, delegate, clock);
BoltWorker worker = workerFactory.newWorker(CONNECTION_DESCRIPTOR);
// when
worker.enqueue((stateMachine) -> {
stateMachine.run("hello", null, nullResponseHandler());
clock.forward(1338, TimeUnit.MILLISECONDS);
});
// then
assertEquals(1, monitor.messagesReceived);
assertEquals(1337, monitor.queueTime);
assertEquals(1338, monitor.processingTime);
}
Aggregations