Search in sources :

Example 1 with Submission

use of jdk.incubator.sql2.Submission in project oracle-db-examples by oracle.

the class SessionOperationGroupTest method testCollect.

/**
 * Verify basic functionality of
 * {@link Session#collect(java.util.stream.Collector)}
 * @throws java.lang.Exception
 */
@Test
public void testCollect() throws Exception {
    Submission<Object> seSubmission = null;
    try (DataSource ds = TestConfig.getDataSource();
        Session se = ds.builder().build()) {
        Collector<Object, ?, Object> co = new ObjectCollector();
        se.attachOperation().timeout(getTimeout()).submit();
        se.collect(co).localOperation().onExecution(() -> 99).submit();
        se.rowOperation("SELECT COUNT(*) FROM " + TABLE).collect(Collectors.summingInt(row -> row.at(1).get(Integer.class))).timeout(getTimeout()).submit();
        se.endTransactionOperation(se.transactionCompletion()).submit();
        seSubmission = se.submit();
    }
    // se.closeOperation().submit() // 5
    assertNotNull(seSubmission);
    @SuppressWarnings("unchecked") List<Integer> result = (List<Integer>) seSubmission.getCompletionStage().toCompletableFuture().get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
    assertNotNull(result);
    // Note the inline comments which count each submit (1, 2, 3...). This is
    // the expected count of accumulated results.
    assertEquals(5, result.size());
    // Attach operation
    assertNull(result.get(0));
    // Local operation
    assertEquals(99, result.get(1).intValue());
    // Row operation
    assertEquals(0, result.get(2).intValue());
    assertEquals(TransactionOutcome.COMMIT, // Transaction operation
    result.get(3));
    // Close operation
    assertNull(result.get(4));
}
Also used : BeforeClass(org.junit.BeforeClass) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) TestConfig(com.oracle.adbaoverjdbc.test.TestConfig) BiConsumer(java.util.function.BiConsumer) Collector(java.util.stream.Collector) Session(jdk.incubator.sql2.Session) SqlSkippedException(jdk.incubator.sql2.SqlSkippedException) AfterClass(org.junit.AfterClass) Assert.assertNotNull(org.junit.Assert.assertNotNull) AdbaType(jdk.incubator.sql2.AdbaType) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) TransactionOutcome(jdk.incubator.sql2.TransactionOutcome) BinaryOperator(java.util.function.BinaryOperator) ArrayRowCountOperation(jdk.incubator.sql2.ArrayRowCountOperation) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) List(java.util.List) Result(jdk.incubator.sql2.Result) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Collections(java.util.Collections) Submission(jdk.incubator.sql2.Submission) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) List(java.util.List) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 2 with Submission

use of jdk.incubator.sql2.Submission in project oracle-db-examples by oracle.

the class SessionOperationGroupTest method testTrueConditonal.

@Test
public void testTrueConditonal() throws Exception {
    Submission<Object> seSubmission;
    AtomicBoolean opExecuted = new AtomicBoolean();
    try (DataSource ds = getDataSource();
        Session se = ds.builder().build()) {
        se.conditional(CompletableFuture.completedFuture(true));
        se.attachOperation().submit();
        se.localOperation().onExecution(() -> {
            opExecuted.set(true);
            return null;
        }).submit();
        seSubmission = se.collect(new ObjectCollector()).submit();
    }
    Object result = seSubmission.getCompletionStage().toCompletableFuture().get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
    assertTrue(opExecuted.get());
    assertNotNull(result);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 3 with Submission

use of jdk.incubator.sql2.Submission 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);
}
Also used : DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) BeforeClass(org.junit.BeforeClass) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) TestConfig(com.oracle.adbaoverjdbc.test.TestConfig) OperationGroup(jdk.incubator.sql2.OperationGroup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AdbaSessionProperty(jdk.incubator.sql2.AdbaSessionProperty) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) TransactionIsolation(jdk.incubator.sql2.AdbaSessionProperty.TransactionIsolation) Assert.fail(org.junit.Assert.fail) Session(jdk.incubator.sql2.Session) AfterClass(org.junit.AfterClass) Validation(jdk.incubator.sql2.Session.Validation) Assert.assertNotNull(org.junit.Assert.assertNotNull) Lifecycle(jdk.incubator.sql2.Session.Lifecycle) Assert.assertTrue(org.junit.Assert.assertTrue) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) LifecycleEvent(com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) TransactionOutcome(jdk.incubator.sql2.TransactionOutcome) Operation(jdk.incubator.sql2.Operation) SessionLifecycleListener(jdk.incubator.sql2.Session.SessionLifecycleListener) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Builder(jdk.incubator.sql2.Session.Builder) Assert.assertFalse(org.junit.Assert.assertFalse) Submission(jdk.incubator.sql2.Submission) Assert.assertEquals(org.junit.Assert.assertEquals) LifecycleEvent(com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent) List(java.util.List) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 4 with Submission

use of jdk.incubator.sql2.Submission in project oracle-db-examples by oracle-samples.

the class SessionOperationGroupTest method testTrueConditonal.

@Test
public void testTrueConditonal() throws Exception {
    Submission<Object> seSubmission;
    AtomicBoolean opExecuted = new AtomicBoolean();
    try (DataSource ds = getDataSource();
        Session se = ds.builder().build()) {
        se.conditional(CompletableFuture.completedFuture(true));
        se.attachOperation().submit();
        se.localOperation().onExecution(() -> {
            opExecuted.set(true);
            return null;
        }).submit();
        seSubmission = se.collect(new ObjectCollector()).submit();
    }
    Object result = seSubmission.getCompletionStage().toCompletableFuture().get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
    assertTrue(opExecuted.get());
    assertNotNull(result);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 5 with Submission

use of jdk.incubator.sql2.Submission in project java-webrisk by googleapis.

the class WebRiskServiceClientTest method createSubmissionTest.

@Test
public void createSubmissionTest() throws Exception {
    Submission expectedResponse = Submission.newBuilder().setUri("uri116076").build();
    mockWebRiskService.addResponse(expectedResponse);
    ProjectName parent = ProjectName.of("[PROJECT]");
    Submission submission = Submission.newBuilder().build();
    Submission actualResponse = client.createSubmission(parent, submission);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockWebRiskService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    CreateSubmissionRequest actualRequest = ((CreateSubmissionRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertEquals(submission, actualRequest.getSubmission());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) Submission(com.google.webrisk.v1.Submission) ProjectName(com.google.webrisk.v1.ProjectName) CreateSubmissionRequest(com.google.webrisk.v1.CreateSubmissionRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 DataSource (jdk.incubator.sql2.DataSource)8 Session (jdk.incubator.sql2.Session)8 Submission (com.google.webrisk.v1.Submission)7 TestConfig (com.oracle.adbaoverjdbc.test.TestConfig)6 List (java.util.List)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 TimeUnit (java.util.concurrent.TimeUnit)6 Function (java.util.function.Function)6 Collectors (java.util.stream.Collectors)6 Submission (jdk.incubator.sql2.Submission)6 TransactionOutcome (jdk.incubator.sql2.TransactionOutcome)6 AfterClass (org.junit.AfterClass)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 Assert.assertFalse (org.junit.Assert.assertFalse)6 Assert.assertNotNull (org.junit.Assert.assertNotNull)6 Assert.assertNull (org.junit.Assert.assertNull)6 Assert.assertTrue (org.junit.Assert.assertTrue)6 BeforeClass (org.junit.BeforeClass)6