use of com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent in project oracle-db-examples by oracle.
the class SessionTest method testAbort.
/**
* Verifies the following behavior:
* <br>
* (1) If lifecycle is Session.Lifecycle.NEW, Session.Lifecycle.OPEN,
* Session.Lifecycle.INACTIVE or Session.Lifecycle.CLOSING ->
* Session.Lifecycle.ABORTING.[Spec: {@link Session#abort()}]
* <br>
* (2) If lifecycle is Session.Lifecycle.ABORTING or Session.Lifecycle.
* CLOSED this is a no-op. [Spec: {@link Session#abort()}]
*
* @throws java.lang.Exception
*/
@Test
public void testAbort() throws Exception {
DataSource ds = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().url(getUrl()).username(getUser()).password(getPassword()).sessionProperty(AdbaSessionProperty.TRANSACTION_ISOLATION, TransactionIsolation.SERIALIZABLE).build();
Session se1 = ds.builder().build();
TestLifecycleListener se1LifecycleListener = new TestLifecycleListener();
se1.registerLifecycleListener(se1LifecycleListener);
Submission<?> se1Submission = se1.submit();
se1.attachOperation().submit();
// Create a record which two sessions will try to update.
se1.rowCountOperation("INSERT INTO " + ABORT_TABLE_1 + " VALUES ('a')").submit();
se1.commitMaybeRollback(se1.transactionCompletion());
// Session 1 acquires a lock on the record.
se1.rowCountOperation("UPDATE " + ABORT_TABLE_1 + " SET c='b' WHERE c='a'").submit();
se1.<List<String>>rowOperation("SELECT * FROM " + ABORT_TABLE_1).collect(Collectors.mapping(row -> row.at(1).get(String.class), Collectors.toList())).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
// Session 2 gets blocked acquiring a lock on the same record.
Session se2 = ds.builder().build();
Submission<?> se2Submission = se2.submit();
se2.attachOperation().submit();
Submission<Long> blockedSubmission = se2.<Long>rowCountOperation("UPDATE " + ABORT_TABLE_1 + " SET c='z' WHERE c='a'").apply(count -> count.getCount()).timeout(getTimeout()).submit();
// Session 1 is aborted so that session 2 can proceed.
se1.abort();
// (2)
se1.abort();
// (1)
LifecycleEvent[] expectedEvents = { new LifecycleEvent(se1, Lifecycle.NEW, Lifecycle.ATTACHED), new LifecycleEvent(se1, Lifecycle.ATTACHED, Lifecycle.ABORTING), new LifecycleEvent(se1, Lifecycle.ABORTING, Lifecycle.CLOSED) };
for (LifecycleEvent expected : expectedEvents) {
LifecycleEvent actual = se1LifecycleListener.record.poll(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
if (actual == null)
fail("Timeout waiting for lifecycle event: " + expected);
assertEquals(expected, actual);
}
long unblockedResult = blockedSubmission.getCompletionStage().toCompletableFuture().get();
assertEquals(1L, unblockedResult);
se2.rollback();
se2.close();
se1Submission.getCompletionStage().toCompletableFuture().handle((r, e) -> {
assertNotNull(e);
return null;
}).get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
se2Submission.getCompletionStage().toCompletableFuture().handle((r, e) -> {
assertNull(e);
return null;
}).get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
}
use of com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent in project oracle-db-examples by oracle.
the class SessionTest method testSessionBuilder.
/**
* Verifies the following behavior:
* <br>
* (1) Session.Builder.property(SessionProperty, Object) will specify a
* property and its value for the built Session.
* [Spec: {@link Session.Builder#property(SessionProperty, Object)}]
* Session.getProperties() will return the set of properties configured on
* this Session excepting any sensitive properties.
* [Spec: {@link Session#getProperties()}]
* <br>
* (2) A Session is initially in the Session.Lifecycle.NEW lifecycle state.
* [Spec: {@link Session.Builder}]
* <br>
* (3) If an attach operation completes successfully and the lifecycle is
* Session.Lifecycle.NEW -> Session.Lifecycle.OPEN.
* [Spec: {@link Session#attachOperation()}]
* <br>
* (4) Session.getSessionLifecycle() returns the current lifecycle of this
* Session. [Spec: {@link Session#getSessionLifecycle()}]
* <br>
* (5) Session.registerLifcycleListener(SessionLifecycleListener) registers
* a listener that will be called whenever there is a change in the
* lifecycle of this Session. If the listener is already registered this
* is a no-op. [Spec:
* {@link Session#registerLifecycleListener(SessionLifecycleListener)}]
* (6) Session.closeOperation() returns an operation which, upon execution,
* transitions the session's lifecyle from OPEN to CLOSING. When the
* operation completes, and no other operations are enqueued, the lifecycle
* transistions from CLOSING to CLOSED. If the operation is submitted when
* the lifecycle is closed, the execution of the operation is a no-op.
* [Spec: {@link Session#closeOperation()}]
*/
@Test
public void testSessionBuilder() throws Exception {
TestLifecycleListener testListener = new TestLifecycleListener();
final String url = getUrl();
final String user = getUser();
final String password = getPassword();
Session se = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().build().builder().property(AdbaSessionProperty.URL, url).property(AdbaSessionProperty.USER, user).property(AdbaSessionProperty.PASSWORD, password).build();
// (1)
assertEquals(url, se.getProperties().get(AdbaSessionProperty.URL));
assertEquals(user, se.getProperties().get(AdbaSessionProperty.USER));
assertNull(se.getProperties().get(AdbaSessionProperty.PASSWORD));
// (2, 4)
assertEquals(Lifecycle.NEW, se.getSessionLifecycle());
// (5)
// The duplicate call verifies the no-op behavior.
se.registerLifecycleListener(testListener);
se.registerLifecycleListener(testListener);
// (3, 4)
se.submit();
se.attachOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
assertEquals(Lifecycle.ATTACHED, se.getSessionLifecycle());
// (4, 6)
se.closeOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
assertEquals(Lifecycle.CLOSED, se.getSessionLifecycle());
// (6)
// The duplicate close operation verifies the no-op behavior.
se.closeOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
// (2, 3, 5, 6)
LifecycleEvent[] expected = { new LifecycleEvent(se, Lifecycle.NEW, Lifecycle.ATTACHED), new LifecycleEvent(se, Lifecycle.ATTACHED, Lifecycle.CLOSING), new LifecycleEvent(se, Lifecycle.CLOSING, Lifecycle.CLOSED) };
LifecycleEvent[] actual = testListener.record.toArray(new LifecycleEvent[0]);
assertArrayEquals(expected, actual);
}
use of com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent in project oracle-db-examples by oracle-samples.
the class SessionTest method testTryWithResources.
/**
* Verify that declaring a Session in try-with-resources closes the session
* when the try block exits.
* @throws Exception
*/
@Test
public void testTryWithResources() throws Exception {
Builder sessionBuilder = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().url(getUrl()).username(getUser()).password(getPassword()).build().builder();
TestLifecycleListener listener = new TestLifecycleListener();
LifecycleEvent[] expectedEvents;
try (Session se = sessionBuilder.build()) {
se.registerLifecycleListener(listener);
se.submit();
se.attachOperation().timeout(getTimeout()).submit();
expectedEvents = new LifecycleEvent[] { new LifecycleEvent(se, Lifecycle.NEW, Lifecycle.ATTACHED), new LifecycleEvent(se, Lifecycle.ATTACHED, Lifecycle.CLOSING), new LifecycleEvent(se, Lifecycle.CLOSING, Lifecycle.CLOSED) };
}
for (LifecycleEvent expected : expectedEvents) {
LifecycleEvent actual = listener.record.poll(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
if (actual == null)
fail("Timeout waiting for lifecycle event: " + expected);
assertEquals(expected, actual);
}
}
use of com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent in project oracle-db-examples by oracle-samples.
the class SessionTest method testSessionBuilder.
/**
* Verifies the following behavior:
* <br>
* (1) Session.Builder.property(SessionProperty, Object) will specify a
* property and its value for the built Session.
* [Spec: {@link Session.Builder#property(SessionProperty, Object)}]
* Session.getProperties() will return the set of properties configured on
* this Session excepting any sensitive properties.
* [Spec: {@link Session#getProperties()}]
* <br>
* (2) A Session is initially in the Session.Lifecycle.NEW lifecycle state.
* [Spec: {@link Session.Builder}]
* <br>
* (3) If an attach operation completes successfully and the lifecycle is
* Session.Lifecycle.NEW -> Session.Lifecycle.OPEN.
* [Spec: {@link Session#attachOperation()}]
* <br>
* (4) Session.getSessionLifecycle() returns the current lifecycle of this
* Session. [Spec: {@link Session#getSessionLifecycle()}]
* <br>
* (5) Session.registerLifcycleListener(SessionLifecycleListener) registers
* a listener that will be called whenever there is a change in the
* lifecycle of this Session. If the listener is already registered this
* is a no-op. [Spec:
* {@link Session#registerLifecycleListener(SessionLifecycleListener)}]
* (6) Session.closeOperation() returns an operation which, upon execution,
* transitions the session's lifecyle from OPEN to CLOSING. When the
* operation completes, and no other operations are enqueued, the lifecycle
* transistions from CLOSING to CLOSED. If the operation is submitted when
* the lifecycle is closed, the execution of the operation is a no-op.
* [Spec: {@link Session#closeOperation()}]
*/
@Test
public void testSessionBuilder() throws Exception {
TestLifecycleListener testListener = new TestLifecycleListener();
final String url = getUrl();
final String user = getUser();
final String password = getPassword();
Session se = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().build().builder().property(AdbaSessionProperty.URL, url).property(AdbaSessionProperty.USER, user).property(AdbaSessionProperty.PASSWORD, password).build();
// (1)
assertEquals(url, se.getProperties().get(AdbaSessionProperty.URL));
assertEquals(user, se.getProperties().get(AdbaSessionProperty.USER));
assertNull(se.getProperties().get(AdbaSessionProperty.PASSWORD));
// (2, 4)
assertEquals(Lifecycle.NEW, se.getSessionLifecycle());
// (5)
// The duplicate call verifies the no-op behavior.
se.registerLifecycleListener(testListener);
se.registerLifecycleListener(testListener);
// (3, 4)
se.submit();
se.attachOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
assertEquals(Lifecycle.ATTACHED, se.getSessionLifecycle());
// (4, 6)
se.closeOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
assertEquals(Lifecycle.CLOSED, se.getSessionLifecycle());
// (6)
// The duplicate close operation verifies the no-op behavior.
se.closeOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
// (2, 3, 5, 6)
LifecycleEvent[] expected = { new LifecycleEvent(se, Lifecycle.NEW, Lifecycle.ATTACHED), new LifecycleEvent(se, Lifecycle.ATTACHED, Lifecycle.CLOSING), new LifecycleEvent(se, Lifecycle.CLOSING, Lifecycle.CLOSED) };
LifecycleEvent[] actual = testListener.record.toArray(new LifecycleEvent[0]);
assertArrayEquals(expected, actual);
}
use of com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent in project oracle-db-examples by oracle-samples.
the class SessionTest method testAbort.
/**
* Verifies the following behavior:
* <br>
* (1) If lifecycle is Session.Lifecycle.NEW, Session.Lifecycle.OPEN,
* Session.Lifecycle.INACTIVE or Session.Lifecycle.CLOSING ->
* Session.Lifecycle.ABORTING.[Spec: {@link Session#abort()}]
* <br>
* (2) If lifecycle is Session.Lifecycle.ABORTING or Session.Lifecycle.
* CLOSED this is a no-op. [Spec: {@link Session#abort()}]
*
* @throws java.lang.Exception
*/
@Test
public void testAbort() throws Exception {
DataSource ds = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().url(getUrl()).username(getUser()).password(getPassword()).sessionProperty(AdbaSessionProperty.TRANSACTION_ISOLATION, TransactionIsolation.SERIALIZABLE).build();
Session se1 = ds.builder().build();
TestLifecycleListener se1LifecycleListener = new TestLifecycleListener();
se1.registerLifecycleListener(se1LifecycleListener);
Submission<?> se1Submission = se1.submit();
se1.attachOperation().submit();
// Create a record which two sessions will try to update.
se1.rowCountOperation("INSERT INTO " + ABORT_TABLE_1 + " VALUES ('a')").submit();
se1.commitMaybeRollback(se1.transactionCompletion());
// Session 1 acquires a lock on the record.
se1.rowCountOperation("UPDATE " + ABORT_TABLE_1 + " SET c='b' WHERE c='a'").submit();
se1.<List<String>>rowOperation("SELECT * FROM " + ABORT_TABLE_1).collect(Collectors.mapping(row -> row.at(1).get(String.class), Collectors.toList())).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
// Session 2 gets blocked acquiring a lock on the same record.
Session se2 = ds.builder().build();
Submission<?> se2Submission = se2.submit();
se2.attachOperation().submit();
Submission<Long> blockedSubmission = se2.<Long>rowCountOperation("UPDATE " + ABORT_TABLE_1 + " SET c='z' WHERE c='a'").apply(count -> count.getCount()).timeout(getTimeout()).submit();
// Session 1 is aborted so that session 2 can proceed.
se1.abort();
// (2)
se1.abort();
// (1)
LifecycleEvent[] expectedEvents = { new LifecycleEvent(se1, Lifecycle.NEW, Lifecycle.ATTACHED), new LifecycleEvent(se1, Lifecycle.ATTACHED, Lifecycle.ABORTING), new LifecycleEvent(se1, Lifecycle.ABORTING, Lifecycle.CLOSED) };
for (LifecycleEvent expected : expectedEvents) {
LifecycleEvent actual = se1LifecycleListener.record.poll(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
if (actual == null)
fail("Timeout waiting for lifecycle event: " + expected);
assertEquals(expected, actual);
}
long unblockedResult = blockedSubmission.getCompletionStage().toCompletableFuture().get();
assertEquals(1L, unblockedResult);
se2.rollback();
se2.close();
se1Submission.getCompletionStage().toCompletableFuture().handle((r, e) -> {
assertNotNull(e);
return null;
}).get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
se2Submission.getCompletionStage().toCompletableFuture().handle((r, e) -> {
assertNull(e);
return null;
}).get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
}
Aggregations