Skip to content

Commit

Permalink
Issue #154 initial set of new TCK test scenarios
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Rauh <nathan.rauh@us.ibm.com>
  • Loading branch information
njr-11 committed Dec 7, 2021
1 parent 9bd22ed commit eb83143
Show file tree
Hide file tree
Showing 14 changed files with 2,497 additions and 0 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package jakarta.enterprise.concurrent.spec.ManagedExecutorService.asynchronous;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Exchanger;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import jakarta.enterprise.concurrent.Asynchronous;
import jakarta.enterprise.concurrent.spec.context.IntContext;
import jakarta.enterprise.concurrent.spec.context.StringContext;
import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class AppBean {
private static final long MAX_WAIT_SECONDS = TimeUnit.MINUTES.toSeconds(2);

@Asynchronous(executor = "java:module/concurrent/ExecutorB")
public CompletionStage<String> addStringContextAndWait(BlockingQueue<String> queue, CountDownLatch blocker) {
String s = StringContext.get();
try {
queue.add(s);
blocker.await(MAX_WAIT_SECONDS, TimeUnit.SECONDS);
return Asynchronous.Result.complete(s);
} catch (Exception x) {
throw new CompletionException(x);
}
}

@Asynchronous
public void exchange(Exchanger<String> exchanger, String value) {
try {
exchanger.exchange(value, MAX_WAIT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException | TimeoutException x) {
throw new CompletionException(x);
}
}

@Asynchronous(executor = "java:app/concurrent/ExecutorA")
public CompletableFuture<Integer> waitAndGetIntContext(Semaphore started, CountDownLatch blocker) {
started.release(1);
CompletableFuture<Integer> future = Asynchronous.Result.getFuture();
try {
while (!future.isDone() && !blocker.await(300, TimeUnit.MILLISECONDS))
System.out.println(Thread.currentThread().getName() +
": waitAndGetIntContext awaiting signal from caller");
future.complete(IntContext.get());
} catch (Exception x) {
future.completeExceptionally(x);
}
return future;
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package jakarta.enterprise.concurrent.spec.ManagedScheduledExecutorService.asynchronous;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

import jakarta.enterprise.concurrent.Asynchronous;
import jakarta.enterprise.concurrent.ContextService;
import jakarta.enterprise.concurrent.spec.context.IntContext;
import jakarta.enterprise.concurrent.spec.context.StringContext;
import jakarta.enterprise.context.RequestScoped;

import javax.naming.InitialContext;
import javax.naming.NamingException;

@RequestScoped
public class ReqBean {
private static final long MAX_WAIT_SECONDS = TimeUnit.MINUTES.toSeconds(2);

@Asynchronous(executor = "java:app/concurrent/ScheduledExecutorA")
public CompletableFuture<String> awaitAndGetThirdPartyContext(Semaphore invocationsStarted,
CountDownLatch blocker) {
invocationsStarted.release(1);
CompletableFuture<String> future = Asynchronous.Result.getFuture();
try {
blocker.await(MAX_WAIT_SECONDS, TimeUnit.SECONDS);
future.complete(IntContext.get() + StringContext.get());
} catch (Exception x) {
future.completeExceptionally(x);
}
return future;
}

@Asynchronous(executor = "java:comp/concurrent/ScheduledExecutorC")
public CompletionStage<ContextService> lookUpAContextService() {
try {
return CompletableFuture.completedFuture(InitialContext.doLookup("java:comp/concurrent/ContextC"));
} catch (NamingException x) {
throw new CompletionException(x);
}
}

public CompletableFuture<String> notAsynchronous() {
return CompletableFuture.completedFuture(Thread.currentThread().getName());
}
}

0 comments on commit eb83143

Please sign in to comment.