-
Notifications
You must be signed in to change notification settings - Fork 36
TCK tests of Asynchronous repository method #1456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
njr-11
wants to merge
3
commits into
jakartaee:main
Choose a base branch
from
njr-11:tck-tests-for-asynchronous-repository-methods
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+401
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
tck/src/main/java/ee/jakarta/tck/data/web/async/Account.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * Copyright (c) 2026 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 ee.jakarta.tck.data.web.async; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @jakarta.nosql.Entity | ||
| @jakarta.persistence.Entity | ||
| public class Account { | ||
|
|
||
| @jakarta.nosql.Id | ||
| @jakarta.persistence.Id | ||
| int accountId; | ||
|
|
||
| @jakarta.nosql.Column | ||
| @jakarta.persistence.Column(nullable = false) | ||
| boolean active; | ||
|
|
||
| @jakarta.nosql.Column | ||
| @jakarta.persistence.Column(nullable = false) | ||
| float balance; | ||
|
|
||
| @jakarta.nosql.Column | ||
| @jakarta.persistence.Column(nullable = false) | ||
| LocalDateTime created; | ||
|
|
||
| @jakarta.nosql.Column | ||
| String email; | ||
|
|
||
| public static Account of(int accountId, | ||
| boolean active, | ||
| float balance, | ||
| LocalDateTime created, | ||
| String email) { | ||
| Account account = new Account(); | ||
| account.setAccountId(accountId); | ||
| account.setActive(active); | ||
| account.setBalance(balance); | ||
| account.setCreatedOn(created); | ||
| account.setEmail(email); | ||
| return account; | ||
| } | ||
|
|
||
| public int getAccountId() { | ||
| return accountId; | ||
| } | ||
|
|
||
| public float getBalance() { | ||
| return balance; | ||
| } | ||
|
|
||
| public LocalDateTime getCreatedOn() { | ||
| return created; | ||
| } | ||
|
|
||
| public String getEmail() { | ||
| return email; | ||
| } | ||
|
|
||
| public boolean isActive() { | ||
| return active; | ||
| } | ||
|
|
||
| public void setAccountId(int value) { | ||
| accountId = value; | ||
| } | ||
|
|
||
| public void setActive(boolean value) { | ||
| active = value; | ||
| } | ||
|
|
||
| public void setBalance(float value) { | ||
| balance = value; | ||
| } | ||
|
|
||
| public void setCreatedOn(LocalDateTime value) { | ||
| created = value; | ||
| } | ||
|
|
||
| public void setEmail(String value) { | ||
| email = value; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Account#" + accountId + | ||
| (active ? " active " : "") + | ||
| " for " + email + | ||
| " owes $" + balance + | ||
| " created " + created; | ||
| } | ||
|
|
||
| } | ||
47 changes: 47 additions & 0 deletions
47
tck/src/main/java/ee/jakarta/tck/data/web/async/Accounts.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Copyright (c) 2026 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 ee.jakarta.tck.data.web.async; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.concurrent.CompletionStage; | ||
|
|
||
| import jakarta.data.repository.BasicRepository; | ||
| import jakarta.data.repository.Insert; | ||
| import jakarta.data.repository.Find; | ||
| import jakarta.data.repository.Repository; | ||
| import jakarta.data.repository.Select; | ||
| import jakarta.enterprise.concurrent.Asynchronous; | ||
|
|
||
| /** | ||
| * A repository with some methods marked Asynchronous. | ||
| * Although this compiles against Jakarta Concurrency, the Asynchronous | ||
| * annotation need not be available at run time, because the TCK will | ||
| * detect the presence or absence of the Jakarta Concurrency API and | ||
| * skip the tests when not present. | ||
| */ | ||
| @Repository | ||
| public interface Accounts extends BasicRepository<Account, Integer> { | ||
|
|
||
| @Asynchronous | ||
| @Insert | ||
| void add(Account... accounts); | ||
|
|
||
| @Asynchronous | ||
| @Find | ||
| @Select(_Account.BALANCE) | ||
| CompletionStage<Optional<Float>> balance(int accountId); | ||
|
|
||
| } |
192 changes: 192 additions & 0 deletions
192
tck/src/main/java/ee/jakarta/tck/data/web/async/AsyncTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| /* | ||
| * Copyright (c) 2026 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 ee.jakarta.tck.data.web.async; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.TreeSet; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.jboss.arquillian.container.test.api.Deployment; | ||
| import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
| import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
|
|
||
| import ee.jakarta.tck.data.framework.junit.anno.AnyEntity; | ||
| import ee.jakarta.tck.data.framework.junit.anno.Assertion; | ||
| import ee.jakarta.tck.data.framework.junit.anno.Web; | ||
| import ee.jakarta.tck.data.framework.utilities.TestPropertyUtility; | ||
| import jakarta.inject.Inject; | ||
|
|
||
| @Web | ||
| @AnyEntity | ||
| public class AsyncTests { | ||
| /** | ||
| * Maximum amount of time to wait for asynchronous operations to complete. | ||
| */ | ||
| static final long TIMEOUT_SECONDS = TimeUnit.MINUTES.toSeconds(2); | ||
|
|
||
| @Deployment | ||
| public static WebArchive createDeployment() { | ||
| return ShrinkWrap.create(WebArchive.class) | ||
| .addClasses(_Account.class, Account.class, Accounts.class); | ||
| } | ||
|
|
||
| @Inject | ||
| Accounts accounts; | ||
|
|
||
| @Assertion(id = "19", strategy = """ | ||
| Tests an asynchronous repository method that performs | ||
| a Find operation. | ||
| """) | ||
| public void testAsynchronousFind() throws Exception { | ||
| try { | ||
| Class.forName("jakarta.enterprise.concurrent.Asynchronous"); | ||
| } catch (ClassNotFoundException x) { | ||
| return; // Jakarta Concurrency API is not present | ||
| } | ||
|
|
||
| List<Account> testData = List.of( | ||
| Account.of(104, false, 4.99f, | ||
| LocalDateTime.of(2026, 5, 19, 16, 25, 40), | ||
| "asyncUser101@eclipse.org"), | ||
| Account.of(105, true, 55.99f, | ||
| LocalDateTime.of(2026, 5, 20, 10, 24, 50), | ||
| "asyncUser105@eclipse.org"), | ||
| Account.of(106, true, 26.99f, | ||
| LocalDateTime.of(2026, 5, 20, 10, 23, 00), | ||
| "asyncUser106@eclipse.org"), | ||
| Account.of(107, true, 77.99f, | ||
| LocalDateTime.of(2026, 7, 27, 17, 27, 10), | ||
| "asyncUser107@eclipse.org")); | ||
| accounts.saveAll(testData); | ||
|
|
||
| TestPropertyUtility.waitForEventualConsistency(); | ||
|
|
||
| CompletableFuture<Float> futureSum = | ||
| accounts.balance(105) | ||
| .thenApply(b -> b.orElse(0.0f)) | ||
| .thenCombine(accounts.balance(107) | ||
| .thenApply(b -> b.orElse(0.0f)), | ||
| Float::sum) | ||
| .toCompletableFuture(); | ||
|
|
||
| assertEquals(133.98f, // 55.99 + 77.99 | ||
| futureSum.get(TIMEOUT_SECONDS, TimeUnit.SECONDS), | ||
| 0.01f); | ||
|
|
||
| accounts.deleteAll(testData); | ||
|
|
||
| TestPropertyUtility.waitForEventualConsistency(); | ||
| } | ||
|
|
||
| @Assertion(id = "19", strategy = """ | ||
| Tests an asynchronous repository method that performs | ||
| an Insert operation. | ||
| """) | ||
| public void testAsynchronousInsert() throws InterruptedException { | ||
| try { | ||
| Class.forName("jakarta.enterprise.concurrent.Asynchronous"); | ||
| } catch (ClassNotFoundException x) { | ||
| return; // Jakarta Concurrency API is not present | ||
| } | ||
|
|
||
| assertEquals(false, accounts.findById(100).isPresent()); | ||
| assertEquals(false, accounts.findById(101).isPresent()); | ||
| assertEquals(false, accounts.findById(102).isPresent()); | ||
|
|
||
| accounts.add( | ||
| Account.of(101, true, 10.99f, | ||
| LocalDateTime.of(2026, 5, 19, 16, 25, 10), | ||
| "asyncUser101@eclipse.org"), | ||
| Account.of(102, true, 20.99f, | ||
| LocalDateTime.of(2026, 5, 19, 16, 22, 20), | ||
| "asyncUser102@eclipse.org"), | ||
| Account.of(103, true, 13.99f, | ||
| LocalDateTime.of(2026, 5, 19, 16, 23, 30), | ||
| "asyncUser103@eclipse.org")); | ||
|
|
||
| TestPropertyUtility.waitForEventualConsistency(); | ||
|
|
||
| Set<Integer> idsFound = new TreeSet<>(); | ||
| for (long startNS = System.nanoTime(); | ||
| idsFound.size() < 3 && TIMEOUT_SECONDS > | ||
| TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startNS); | ||
| TimeUnit.SECONDS.sleep(1)) { | ||
| if (!idsFound.contains(101)) { | ||
| accounts.findById(101).ifPresent(account -> { | ||
| assertEquals(101, | ||
| account.accountId); | ||
| assertEquals(true, | ||
| account.active); | ||
| assertEquals(10.99f, | ||
| account.balance, | ||
| 0.001f); | ||
| assertEquals(LocalDateTime.of(2026, 5, 19, 16, 25, 10), | ||
| account.created); | ||
| assertEquals("asyncUser101@eclipse.org", | ||
| account.email); | ||
| idsFound.add(101); | ||
| }); | ||
| } | ||
| if (!idsFound.contains(102)) { | ||
| accounts.findById(102).ifPresent(account -> { | ||
| assertEquals(102, | ||
| account.accountId); | ||
| assertEquals(true, | ||
| account.active); | ||
| assertEquals(20.99f, | ||
| account.balance, | ||
| 0.001f); | ||
| assertEquals(LocalDateTime.of(2026, 5, 19, 16, 22, 20), | ||
| account.created); | ||
| assertEquals("asyncUser102@eclipse.org", | ||
| account.email); | ||
| idsFound.add(102); | ||
| }); | ||
| } | ||
| if (!idsFound.contains(103)) { | ||
| accounts.findById(103).ifPresent(account -> { | ||
| assertEquals(103, | ||
| account.accountId); | ||
| assertEquals(true, | ||
| account.active); | ||
| assertEquals(13.99f, | ||
| account.balance, | ||
| 0.001f); | ||
| assertEquals(LocalDateTime.of(2026, 5, 19, 16, 23, 30), | ||
| account.created); | ||
| assertEquals("asyncUser103@eclipse.org", | ||
| account.email); | ||
| idsFound.add(103); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| assertEquals(Set.of(101, 102, 103), | ||
| idsFound); | ||
|
|
||
| accounts.deleteById(101); | ||
| accounts.deleteById(102); | ||
| accounts.deleteById(103); | ||
|
|
||
| TestPropertyUtility.waitForEventualConsistency(); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mind putting a line here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand your question. My best guess is that you want a new line prior to the
Idannotation. I added a commit that does that:d882dfc