Search in sources :

Example 6 with LifecycleEvent

use of com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent in project oracle-db-examples by oracle.

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);
    }
}
Also used : Builder(jdk.incubator.sql2.Session.Builder) LifecycleEvent(com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Aggregations

LifecycleEvent (com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent)6 Session (jdk.incubator.sql2.Session)6 Test (org.junit.Test)6 Builder (jdk.incubator.sql2.Session.Builder)4 TestConfig (com.oracle.adbaoverjdbc.test.TestConfig)2 List (java.util.List)2 BlockingQueue (java.util.concurrent.BlockingQueue)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 TimeUnit (java.util.concurrent.TimeUnit)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 AdbaSessionProperty (jdk.incubator.sql2.AdbaSessionProperty)2 TransactionIsolation (jdk.incubator.sql2.AdbaSessionProperty.TransactionIsolation)2 DataSource (jdk.incubator.sql2.DataSource)2