Skip to content

Commit

Permalink
migrates to junit 5
Browse files Browse the repository at this point in the history
Signed-off-by: Oleh Dokuka <odokuka@vmware.com>
Signed-off-by: Oleh Dokuka <oleh.dokuka@icloud.com>
  • Loading branch information
OlegDokuka committed Jun 2, 2021
1 parent 4484bc1 commit 93248e9
Show file tree
Hide file tree
Showing 31 changed files with 475 additions and 578 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ subprojects {
entry 'mockito-junit-jupiter'
entry 'mockito-core'
}
// TODO: Remove after JUnit5 migration
dependency 'junit:junit:4.12'
dependency "org.hamcrest:hamcrest-library:${ext['hamcrest.version']}"
dependencySet(group: 'org.openjdk.jmh', version: ext['jmh.version']) {
entry 'jmh-core'
Expand Down
3 changes: 0 additions & 3 deletions rsocket-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ dependencies {
testRuntimeOnly 'ch.qos.logback:logback-classic'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'

// TODO: Remove after JUnit5 migration
testCompileOnly 'junit:junit'
testImplementation 'org.hamcrest:hamcrest-library'
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine'

jcstressImplementation "ch.qos.logback:logback-classic"
}
Expand Down
30 changes: 10 additions & 20 deletions rsocket-core/src/test/java/io/rsocket/core/AbstractSocketRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@
import io.rsocket.test.util.TestDuplexConnection;
import io.rsocket.test.util.TestSubscriber;
import java.time.Duration;
import org.junit.rules.ExternalResource;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.reactivestreams.Subscriber;

public abstract class AbstractSocketRule<T extends RSocket> extends ExternalResource {
public abstract class AbstractSocketRule<T extends RSocket> {

protected TestDuplexConnection connection;
protected Subscriber<Void> connectSub;
Expand All @@ -38,22 +35,15 @@ public abstract class AbstractSocketRule<T extends RSocket> extends ExternalReso
protected int maxFrameLength = FRAME_LENGTH_MASK;
protected int maxInboundPayloadSize = Integer.MAX_VALUE;

@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
allocator =
LeaksTrackingByteBufAllocator.instrument(
ByteBufAllocator.DEFAULT, Duration.ofSeconds(5), "");
connectSub = TestSubscriber.create();
init();
base.evaluate();
}
};
public void init() {
allocator =
LeaksTrackingByteBufAllocator.instrument(
ByteBufAllocator.DEFAULT, Duration.ofSeconds(5), "");
connectSub = TestSubscriber.create();
doInit();
}

protected void init() {
protected void doInit() {
if (socket != null) {
socket.dispose();
}
Expand All @@ -66,12 +56,12 @@ protected void init() {

public void setMaxInboundPayloadSize(int maxInboundPayloadSize) {
this.maxInboundPayloadSize = maxInboundPayloadSize;
init();
doInit();
}

public void setMaxFrameLength(int maxFrameLength) {
this.maxFrameLength = maxFrameLength;
init();
doInit();
}

protected abstract T newRSocket();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package io.rsocket.core;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
Expand Down Expand Up @@ -65,32 +65,32 @@ public void clientSplits() {
.subscribe();

source.addToReceivedBuffer(errorFrame(1).retain());
assertEquals(1, clientFrames.get());
assertEquals(0, serverFrames.get());
assertThat(clientFrames.get()).isOne();
assertThat(serverFrames.get()).isZero();

source.addToReceivedBuffer(errorFrame(1).retain());
assertEquals(2, clientFrames.get());
assertEquals(0, serverFrames.get());
assertThat(clientFrames.get()).isEqualTo(2);
assertThat(serverFrames.get()).isZero();

source.addToReceivedBuffer(leaseFrame().retain());
assertEquals(3, clientFrames.get());
assertEquals(0, serverFrames.get());
assertThat(clientFrames.get()).isEqualTo(3);
assertThat(serverFrames.get()).isZero();

source.addToReceivedBuffer(keepAliveFrame().retain());
assertEquals(4, clientFrames.get());
assertEquals(0, serverFrames.get());
assertThat(clientFrames.get()).isEqualTo(4);
assertThat(serverFrames.get()).isZero();

source.addToReceivedBuffer(errorFrame(2).retain());
assertEquals(4, clientFrames.get());
assertEquals(1, serverFrames.get());
assertThat(clientFrames.get()).isEqualTo(4);
assertThat(serverFrames.get()).isOne();

source.addToReceivedBuffer(errorFrame(0).retain());
assertEquals(5, clientFrames.get());
assertEquals(1, serverFrames.get());
assertThat(clientFrames.get()).isEqualTo(5);
assertThat(serverFrames.get()).isOne();

source.addToReceivedBuffer(metadataPushFrame().retain());
assertEquals(5, clientFrames.get());
assertEquals(2, serverFrames.get());
assertThat(clientFrames.get()).isEqualTo(5);
assertThat(serverFrames.get()).isEqualTo(2);
}

@Test
Expand All @@ -110,32 +110,32 @@ public void serverSplits() {
.subscribe();

source.addToReceivedBuffer(errorFrame(1).retain());
assertEquals(1, clientFrames.get());
assertEquals(0, serverFrames.get());
assertThat(clientFrames.get()).isEqualTo(1);
assertThat(serverFrames.get()).isZero();

source.addToReceivedBuffer(errorFrame(1).retain());
assertEquals(2, clientFrames.get());
assertEquals(0, serverFrames.get());
assertThat(clientFrames.get()).isEqualTo(2);
assertThat(serverFrames.get()).isZero();

source.addToReceivedBuffer(leaseFrame().retain());
assertEquals(2, clientFrames.get());
assertEquals(1, serverFrames.get());
assertThat(clientFrames.get()).isEqualTo(2);
assertThat(serverFrames.get()).isOne();

source.addToReceivedBuffer(keepAliveFrame().retain());
assertEquals(2, clientFrames.get());
assertEquals(2, serverFrames.get());
assertThat(clientFrames.get()).isEqualTo(2);
assertThat(serverFrames.get()).isEqualTo(2);

source.addToReceivedBuffer(errorFrame(2).retain());
assertEquals(2, clientFrames.get());
assertEquals(3, serverFrames.get());
assertThat(clientFrames.get()).isEqualTo(2);
assertThat(serverFrames.get()).isEqualTo(3);

source.addToReceivedBuffer(errorFrame(0).retain());
assertEquals(2, clientFrames.get());
assertEquals(4, serverFrames.get());
assertThat(clientFrames.get()).isEqualTo(2);
assertThat(serverFrames.get()).isEqualTo(4);

source.addToReceivedBuffer(metadataPushFrame().retain());
assertEquals(3, clientFrames.get());
assertEquals(4, serverFrames.get());
assertThat(clientFrames.get()).isEqualTo(3);
assertThat(serverFrames.get()).isEqualTo(4);
}

private ByteBuf leaseFrame() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
* limitations under the License.
*/

import static io.rsocket.frame.FrameHeaderCodec.frameType;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasSize;

import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
Expand All @@ -37,7 +32,6 @@
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -52,7 +46,6 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.runners.model.Statement;
import org.reactivestreams.Publisher;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
Expand All @@ -64,6 +57,7 @@
import reactor.test.publisher.TestPublisher;
import reactor.test.util.RaceTestUtils;
import reactor.util.context.Context;
import reactor.util.context.ContextView;
import reactor.util.retry.Retry;

public class DefaultRSocketClientTests {
Expand All @@ -75,13 +69,7 @@ public void setUp() throws Throwable {
Hooks.onNextDropped(ReferenceCountUtil::safeRelease);
Hooks.onErrorDropped((t) -> {});
rule = new ClientSocketRule();
rule.apply(
new Statement() {
@Override
public void evaluate() {}
},
null)
.evaluate();
rule.init();
}

@AfterEach
Expand Down Expand Up @@ -179,19 +167,12 @@ public void shouldSentFrameOnResolution(
@MethodSource("interactions")
@SuppressWarnings({"unchecked", "rawtypes"})
public void shouldHaveNoLeaksOnPayloadInCaseOfRacingOfOnNextAndCancel(
BiFunction<RSocketClient, Publisher<Payload>, Publisher<?>> request, FrameType requestType)
throws Throwable {
BiFunction<RSocketClient, Publisher<Payload>, Publisher<?>> request, FrameType requestType) {
Assumptions.assumeThat(requestType).isNotEqualTo(FrameType.REQUEST_CHANNEL);

for (int i = 0; i < RaceTestConstants.REPEATS; i++) {
ClientSocketRule rule = new ClientSocketRule();
rule.apply(
new Statement() {
@Override
public void evaluate() {}
},
null)
.evaluate();
rule.init();
Payload payload = ByteBufPayload.create("test", "testMetadata");
TestPublisher<Payload> testPublisher =
TestPublisher.createNoncompliant(TestPublisher.Violation.DEFER_CANCELLATION);
Expand Down Expand Up @@ -241,19 +222,12 @@ public void evaluate() {}
@MethodSource("interactions")
@SuppressWarnings({"unchecked", "rawtypes"})
public void shouldHaveNoLeaksOnPayloadInCaseOfRacingOfRequestAndCancel(
BiFunction<RSocketClient, Publisher<Payload>, Publisher<?>> request, FrameType requestType)
throws Throwable {
BiFunction<RSocketClient, Publisher<Payload>, Publisher<?>> request, FrameType requestType) {
Assumptions.assumeThat(requestType).isNotEqualTo(FrameType.REQUEST_CHANNEL);

for (int i = 0; i < RaceTestConstants.REPEATS; i++) {
ClientSocketRule rule = new ClientSocketRule();
rule.apply(
new Statement() {
@Override
public void evaluate() {}
},
null)
.evaluate();
rule.init();
ByteBuf dataBuffer = rule.allocator.buffer();
dataBuffer.writeCharSequence("test", CharsetUtil.UTF_8);

Expand Down Expand Up @@ -311,14 +285,17 @@ public void shouldPropagateDownstreamContext(
Payload payload = ByteBufPayload.create(dataBuffer, metadataBuffer);
AssertSubscriber assertSubscriber = new AssertSubscriber(Context.of("test", "test"));

Context[] receivedContext = new Context[1];
ContextView[] receivedContext = new Context[1];
Publisher<?> publisher =
request.apply(
rule.client,
Mono.just(payload)
.mergeWith(
Mono.subscriberContext()
.doOnNext(c -> receivedContext[0] = c)
Mono.deferContextual(
c -> {
receivedContext[0] = c;
return Mono.empty();
})
.then(Mono.empty())));
publisher.subscribe(assertSubscriber);

Expand Down Expand Up @@ -481,16 +458,11 @@ public void shouldDisposeOriginalSource() {
}

@Test
public void shouldDisposeOriginalSourceIfRacing() throws Throwable {
public void shouldDisposeOriginalSourceIfRacing() {
for (int i = 0; i < RaceTestConstants.REPEATS; i++) {
ClientSocketRule rule = new ClientSocketRule();
rule.apply(
new Statement() {
@Override
public void evaluate() {}
},
null)
.evaluate();

rule.init();

AssertSubscriber<RSocket> assertSubscriber = AssertSubscriber.create();
rule.client.source().subscribe(assertSubscriber);
Expand Down Expand Up @@ -520,8 +492,8 @@ public static class ClientSocketRule extends AbstractSocketRule<RSocketRequester
protected Sinks.One<RSocket> producer;

@Override
protected void init() {
super.init();
protected void doInit() {
super.doInit();
delayer = () -> producer.tryEmitValue(socket);
producer = Sinks.one();
client =
Expand All @@ -547,22 +519,5 @@ protected RSocketRequester newRSocket() {
__ -> null,
null);
}

public int getStreamIdForRequestType(FrameType expectedFrameType) {
assertThat("Unexpected frames sent.", connection.getSent(), hasSize(greaterThanOrEqualTo(1)));
List<FrameType> framesFound = new ArrayList<>();
for (ByteBuf frame : connection.getSent()) {
FrameType frameType = frameType(frame);
if (frameType == expectedFrameType) {
return FrameHeaderCodec.streamId(frame);
}
framesFound.add(frameType);
}
throw new AssertionError(
"No frames sent with frame type: "
+ expectedFrameType
+ ", frames found: "
+ framesFound);
}
}
}
Loading

0 comments on commit 93248e9

Please sign in to comment.