-
Notifications
You must be signed in to change notification settings - Fork 10
Replaced TestUntil by AccessSafely #11
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
eliezio
wants to merge
2
commits into
vlingo:master
Choose a base branch
from
eliezio:master
base: master
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
2 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
187 changes: 187 additions & 0 deletions
187
src/test/java/io/vlingo/wire/fdx/bidirectional/AbstractServerChannelActorTest.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,187 @@ | ||
| // Copyright © 2012-2018 Vaughn Vernon. All rights reserved. | ||
| // | ||
| // This Source Code Form is subject to the terms of the | ||
| // Mozilla Public License, v. 2.0. If a copy of the MPL | ||
| // was not distributed with this file, You can obtain | ||
| // one at https://mozilla.org/MPL/2.0/. | ||
| package io.vlingo.wire.fdx.bidirectional; | ||
|
|
||
| import io.vlingo.actors.World; | ||
| import io.vlingo.wire.channel.RequestChannelConsumerProvider; | ||
| import io.vlingo.wire.channel.ResponseChannelConsumer; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.List; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| public abstract class AbstractServerChannelActorTest { | ||
| private static AtomicInteger TEST_PORT = new AtomicInteger(49560); | ||
|
|
||
| protected static final int POOL_SIZE = 100; | ||
|
|
||
| private ClientRequestResponseChannel client; | ||
| private ServerRequestResponseChannel server; | ||
| private World world; | ||
|
|
||
| protected int testPort = TEST_PORT.incrementAndGet(); | ||
|
|
||
| protected abstract ClientRequestResponseChannel createClient(ResponseChannelConsumer consumer) | ||
| throws Exception; | ||
|
|
||
| protected abstract ServerRequestResponseChannel createServer(World world, | ||
| RequestChannelConsumerProvider consumerProvider); | ||
|
|
||
| protected abstract void request(ClientRequestResponseChannel client, final String request); | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| world = World.startWithDefaults("test-request-response-channel"); | ||
| // the client is created lazily | ||
| } | ||
|
|
||
| @Test | ||
| public void testBasicRequestResponse() | ||
| throws Exception { | ||
| final String request = "Hello, Request-Response"; | ||
|
|
||
| TestRequestChannelConsumer serverConsumer = new TestRequestChannelConsumer(1, request.length()); | ||
| TestResponseChannelConsumer clientConsumer = new TestResponseChannelConsumer(1, request.length()); | ||
| client = createClient(clientConsumer); | ||
| server = createServer(world, () -> serverConsumer); | ||
| request(client, request); | ||
|
|
||
| while (serverConsumer.remaining() > 0) { | ||
| ; | ||
| } | ||
|
|
||
| while (clientConsumer.remaining() > 0) { | ||
| client.probeChannel(); | ||
| } | ||
|
|
||
| assertEquals(1, serverConsumer.consumeCount()); | ||
| assertEquals(1, clientConsumer.consumeCount()); | ||
| assertEquals(clientConsumer.responses().get(0), serverConsumer.requests().get(0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGappyRequestResponse() throws Exception { | ||
| final String requestPart1 = "Request Part-1"; | ||
| final String requestPart2 = ":Request Part-2"; | ||
| final String requestPart3 = ":Request Part-3"; | ||
|
|
||
| int expectedRequestLength = requestPart1.length() + requestPart2.length() + requestPart3.length(); | ||
| TestRequestChannelConsumer serverConsumer = new TestRequestChannelConsumer(1, expectedRequestLength); | ||
| TestResponseChannelConsumer clientConsumer = new TestResponseChannelConsumer(1, expectedRequestLength); | ||
| client = createClient(clientConsumer); | ||
| server = createServer(world, () -> serverConsumer); | ||
|
|
||
| // simulate network latency for parts of single request | ||
|
|
||
| request(client, requestPart1); | ||
| Thread.sleep(100); | ||
| request(client, requestPart2); | ||
| Thread.sleep(200); | ||
| request(client, requestPart3); | ||
| while (serverConsumer.remaining() > 0) { | ||
| ; | ||
| } | ||
|
|
||
| while (clientConsumer.remaining() > 0) { | ||
| Thread.sleep(10); | ||
| client.probeChannel(); | ||
| } | ||
|
|
||
| assertEquals(1, serverConsumer.consumeCount()); | ||
| assertEquals(1, clientConsumer.consumeCount()); | ||
| assertEquals(clientConsumer.responses().get(0), serverConsumer.requests().get(0)); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void test10RequestResponse() | ||
| throws Exception { | ||
| final String request = "Hello, Request-Response"; | ||
|
|
||
| int numMessages = 10; | ||
| int expectedRequestLength = request.length() + 1; // digits 0 - 9 | ||
| TestRequestChannelConsumer serverConsumer = new TestRequestChannelConsumer(numMessages, expectedRequestLength); | ||
| TestResponseChannelConsumer clientConsumer = new TestResponseChannelConsumer(numMessages, expectedRequestLength); | ||
| client = createClient(clientConsumer); | ||
| server = createServer(world, () -> serverConsumer); | ||
|
|
||
| for (int idx = 0; idx < numMessages; ++idx) { | ||
| request(client, request + idx); | ||
| } | ||
|
|
||
| while (clientConsumer.remaining() > 0) { | ||
| client.probeChannel(); | ||
| } | ||
|
|
||
| assertEquals(numMessages, serverConsumer.consumeCount()); | ||
| assertEquals(numMessages, clientConsumer.consumeCount()); | ||
|
|
||
| List<String> requests = serverConsumer.requests(); | ||
| List<String> responses = clientConsumer.responses(); | ||
| for (int idx = 0; idx < numMessages; ++idx) { | ||
| assertEquals(responses.get(idx), requests.get(idx)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testThatRequestResponsePoolLimitsNotExceeded() | ||
| throws Exception { | ||
| final int TOTAL = POOL_SIZE * 2; | ||
|
|
||
| final String request = "Hello, Request-Response"; | ||
|
|
||
| int expectedRequestLength = request.length() + 3; // digits 000 - 999 | ||
| TestRequestChannelConsumer serverConsumer = new TestRequestChannelConsumer(TOTAL, expectedRequestLength); | ||
| TestResponseChannelConsumer clientConsumer = new TestResponseChannelConsumer(TOTAL, expectedRequestLength); | ||
| client = createClient(clientConsumer); | ||
| server = createServer(world, () -> serverConsumer); | ||
|
|
||
| for (int idx = 0; idx < TOTAL; ++idx) { | ||
| request(client, request + String.format("%03d", idx)); | ||
| } | ||
|
|
||
| while (clientConsumer.remaining() > 0) { | ||
| client.probeChannel(); | ||
| } | ||
|
|
||
| assertEquals(TOTAL, serverConsumer.consumeCount()); | ||
| assertEquals(TOTAL, clientConsumer.consumeCount()); | ||
|
|
||
| List<String> requests = serverConsumer.requests(); | ||
| List<String> responses = clientConsumer.responses(); | ||
| for (int idx = 0; idx < TOTAL; ++idx) { | ||
| assertEquals(responses.get(idx), requests.get(idx)); | ||
| } | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| try { | ||
| server.close(); | ||
| } catch (Exception e) { | ||
| // ignore | ||
| } | ||
| try { | ||
| client.close(); | ||
| } catch (Exception e) { | ||
| // ignore | ||
| } | ||
|
|
||
| try { Thread.sleep(1000); } catch (Exception e) { } | ||
|
|
||
| try { | ||
| world.terminate(); | ||
| } catch (Exception e) { | ||
| // ignore | ||
| } | ||
| } | ||
|
|
||
| } | ||
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.
Each test, extending from this class, should have their own port range to avoid collision when running parallel tests .
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.
Got it. Is it possible to use random ports on all tests, allowing a definitive solution for problems like this?
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.
Even a single static
AtomicIntegerand usingincrementAndGet()in the abstract base will work for all extenders. I don't know why random would be better. You would still have to set ranges to different tests don't collide. I don't think there could be a central static because it could get recreated if multiple JVMs are employed in a given test run.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.
When I say random I mean letting the OS choose a random local port and configuring the client accordingly. This is a common failsafe practice on testing server applications.
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 could not find any reliable way to do what you are suggesting with raw
java.nio.channels.SocketChannelandio.rsocket.transport.netty.server.TcpServerTransportat the time. I think most of the frameworks simply loop through sockets until find one that's open. We could replicate the approach but it's prone to race conditions when the loop identifies a free socket, closes it, and the consumer did not yet attached to it.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.
This automatic port allocation is provided by every ServerSocket implementation. See https://docs.oracle.com/javase/8/docs/api/java/net/ServerSocket.html#ServerSocket-int-
Moreover, the test at https://github.com/rsocket/rsocket-java/blob/88ee909b8bfb78b5c53fa25bc39e16f6d893f0df/rsocket-transport-netty/src/test/java/io/rsocket/transport/netty/server/TcpServerTransportTest.java#L85 suggests that this mechanism is also available on rsocket.
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.
Great find! Can you test this?
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.
@alexguzun Can this be merged?