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));
}
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);
}
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);
}
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);
}
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()));
}
Aggregations