-
Notifications
You must be signed in to change notification settings - Fork 16
Implement non-blocking trySend and tryReceive methods #262
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
endrju19
wants to merge
9
commits into
main
Choose a base branch
from
jox-try-send
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
974ad48
Add Channel.estimateSize() for monitoring and observability (#189)
0e75dd9
Implement non-blocking trySend and tryReceive methods (#187)
endrju19 f2d2685
Add RESUMING state comments in tryUpdateCellReceive and clean up chan…
endrju19 c4ec274
Improve Javadoc wording for contention caveat and add error-with-buff…
endrju19 fbd3b08
Strip redundant comments from non-blocking send/receive methods
endrju19 e3b89dd
fix: Simplify non-blocking send/receive test logic and remove redunda…
endrju19 00509e2
fix: Update non-blocking send/receive test to fallback to blocking re…
endrju19 2ecd720
Merge branch 'main' into jox-try-send
endrju19 0f040cc
Remove estimateSize() from Channel
endrju19 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
236 changes: 236 additions & 0 deletions
236
channels-fray-tests/src/test/java/com/softwaremill/jox/fray/FrayTrySendReceiveTest.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,236 @@ | ||
| package com.softwaremill.jox.fray; | ||
|
|
||
| import static com.softwaremill.jox.fray.Config.CHANNEL_SIZE; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.pastalab.fray.junit.junit5.FrayTestExtension; | ||
| import org.pastalab.fray.junit.junit5.annotations.ConcurrencyTest; | ||
|
|
||
| import com.softwaremill.jox.Channel; | ||
| import com.softwaremill.jox.ChannelDone; | ||
|
|
||
| @ExtendWith(FrayTestExtension.class) | ||
| public class FrayTrySendReceiveTest { | ||
|
|
||
| // trySend | receive | ||
|
|
||
| @ConcurrencyTest | ||
| public void trySendReceiveTest() throws InterruptedException { | ||
| Channel<Integer> ch = Channel.newBufferedChannel(CHANNEL_SIZE); | ||
|
|
||
| Fork<Void> f1 = | ||
| Fork.newNoResult( | ||
| () -> { | ||
| if (!ch.trySend(10)) { | ||
| ch.send(10); | ||
| } | ||
| }); | ||
| Fork<Integer> f2 = Fork.newWithResult(ch::receive); | ||
|
|
||
| Fork.startAll(f1, f2); | ||
| f1.join(); | ||
|
|
||
| assert (f2.join() == 10); | ||
| } | ||
|
|
||
| // send | tryReceive | ||
|
|
||
| @ConcurrencyTest | ||
| public void sendTryReceiveTest() throws InterruptedException { | ||
| Channel<Integer> ch = Channel.newBufferedChannel(CHANNEL_SIZE); | ||
|
|
||
| Fork<Void> f1 = Fork.newNoResult(() -> ch.send(10)); | ||
| Fork<Integer> f2 = | ||
| Fork.newWithResult( | ||
| () -> { | ||
| Integer result = ch.tryReceive(); | ||
| if (result != null) return result; | ||
| return ch.receive(); | ||
| }); | ||
|
|
||
| Fork.startAll(f1, f2); | ||
| f1.join(); | ||
|
|
||
| assert (f2.join() == 10); | ||
| } | ||
|
|
||
| // trySend | tryReceive | ||
|
|
||
| @ConcurrencyTest | ||
| public void trySendTryReceiveTest() throws InterruptedException { | ||
| Channel<Integer> ch = Channel.newBufferedChannel(CHANNEL_SIZE); | ||
|
|
||
| Fork<Boolean> f1 = Fork.newWithResult(() -> ch.trySend(10)); | ||
| Fork<Integer> f2 = Fork.newWithResult(ch::tryReceive); | ||
|
|
||
| Fork.startAll(f1, f2); | ||
| boolean sent = f1.join(); | ||
| Integer received = f2.join(); | ||
|
|
||
| if (received != null) { | ||
| assert sent; | ||
| assert (received == 10); | ||
| } | ||
| } | ||
|
|
||
| // multiple trySend | multiple tryReceive | ||
|
|
||
| @ConcurrencyTest | ||
| public void multiTrySendMultiTryReceiveTest() throws InterruptedException { | ||
| Channel<Integer> ch = Channel.newBufferedChannel(CHANNEL_SIZE); | ||
|
|
||
| int concurrency = 4; | ||
|
|
||
| var sendForks = new ArrayList<Fork<Void>>(); | ||
| var receiveForks = new ArrayList<Fork<Integer>>(); | ||
|
|
||
| for (int i = 0; i < concurrency; i++) { | ||
| final var finalI = i; | ||
| sendForks.add( | ||
| Fork.newNoResult( | ||
| () -> { | ||
| if (!ch.trySend(finalI)) { | ||
| ch.send(finalI); | ||
| } | ||
| })); | ||
| receiveForks.add( | ||
| Fork.newWithResult( | ||
| () -> { | ||
| Integer result = ch.tryReceive(); | ||
| if (result != null) return result; | ||
| return ch.receive(); | ||
| })); | ||
| } | ||
|
|
||
| Fork.startAll(sendForks.toArray(new Fork<?>[0])); | ||
| Fork.startAll(receiveForks.toArray(new Fork<?>[0])); | ||
|
|
||
| for (Fork<Void> sendFork : sendForks) { | ||
| sendFork.join(); | ||
| } | ||
|
|
||
| var result = 0; | ||
| for (Fork<Integer> receiveFork : receiveForks) { | ||
| result += receiveFork.join(); | ||
| } | ||
|
|
||
| assert (result == concurrency * (concurrency - 1) / 2); | ||
| } | ||
|
|
||
| // trySend | tryReceive (rendezvous) | ||
|
|
||
| @ConcurrencyTest | ||
| public void trySendTryReceive_rendezvousTest() throws InterruptedException { | ||
| Channel<Integer> ch = Channel.newRendezvousChannel(); | ||
|
|
||
| Fork<Void> f1 = | ||
| Fork.newNoResult( | ||
| () -> { | ||
| if (!ch.trySend(10)) { | ||
| ch.send(10); | ||
| } | ||
| }); | ||
| Fork<Integer> f2 = | ||
| Fork.newWithResult( | ||
| () -> { | ||
| Integer result = ch.tryReceive(); | ||
| if (result != null) return result; | ||
| return ch.receive(); | ||
| }); | ||
|
|
||
| Fork.startAll(f1, f2); | ||
| f1.join(); | ||
|
|
||
| assert (f2.join() == 10); | ||
| } | ||
|
|
||
| // trySend | receive (rendezvous) | ||
|
|
||
| @ConcurrencyTest | ||
| public void trySendReceive_rendezvousTest() throws InterruptedException { | ||
| Channel<Integer> ch = Channel.newRendezvousChannel(); | ||
|
|
||
| Fork<Void> f1 = | ||
| Fork.newNoResult( | ||
| () -> { | ||
| if (!ch.trySend(10)) { | ||
| ch.send(10); | ||
| } | ||
| }); | ||
| Fork<Integer> f2 = Fork.newWithResult(ch::receive); | ||
|
|
||
| Fork.startAll(f1, f2); | ||
| f1.join(); | ||
|
|
||
| assert (f2.join() == 10); | ||
| } | ||
|
|
||
| // send | tryReceive (rendezvous) | ||
|
|
||
| @ConcurrencyTest | ||
| public void sendTryReceive_rendezvousTest() throws InterruptedException { | ||
| Channel<Integer> ch = Channel.newRendezvousChannel(); | ||
|
|
||
| Fork<Void> f1 = Fork.newNoResult(() -> ch.send(10)); | ||
| Fork<Integer> f2 = | ||
| Fork.newWithResult( | ||
| () -> { | ||
| Integer result = ch.tryReceive(); | ||
| if (result != null) return result; | ||
| return ch.receive(); | ||
| }); | ||
|
|
||
| Fork.startAll(f1, f2); | ||
| f1.join(); | ||
|
|
||
| assert (f2.join() == 10); | ||
| } | ||
|
|
||
| // trySend | tryReceive (unlimited) | ||
|
|
||
| @ConcurrencyTest | ||
| public void trySendTryReceive_unlimitedTest() throws InterruptedException { | ||
| Channel<Integer> ch = Channel.newUnlimitedChannel(); | ||
|
|
||
| Fork<Boolean> f1 = Fork.newWithResult(() -> ch.trySend(10)); | ||
| Fork<Integer> f2 = Fork.newWithResult(ch::tryReceive); | ||
|
|
||
| Fork.startAll(f1, f2); | ||
| boolean sent = f1.join(); | ||
| Integer received = f2.join(); | ||
|
|
||
| assert sent; | ||
| assert received == null || (received == 10); | ||
| } | ||
|
|
||
| // trySend | close | tryReceive | ||
|
|
||
| @ConcurrencyTest | ||
| public void trySendCloseTryReceiveTest() throws InterruptedException { | ||
| Channel<Integer> ch = Channel.newBufferedChannel(CHANNEL_SIZE); | ||
|
|
||
| Fork<Boolean> f1 = | ||
| Fork.newWithResult( | ||
| () -> { | ||
| Object r = ch.trySendOrClosed(10); | ||
| return r == null; | ||
| }); | ||
|
|
||
| Fork<Void> f2 = Fork.newNoResult(ch::done); | ||
|
|
||
| Fork<Object> f3 = Fork.newWithResult(ch::tryReceiveOrClosed); | ||
|
|
||
| Fork.startAll(f1, f2, f3); | ||
|
|
||
| boolean sent = f1.join(); | ||
| f2.join(); | ||
| Object received = f3.join(); | ||
|
|
||
| if (received != null && !(received instanceof ChannelDone)) { | ||
| assert (received.equals(10)); | ||
| assert sent; | ||
| } | ||
| } | ||
| } | ||
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.
the channel size for fray tests is controlled differently- see CHANNEL_SIZE. All of these tests should be removed