Skip to content

Fix TCK §2.3 verification for when a legit call is made with onError/onComplete method name in the stacktrace #483

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

Merged
merged 2 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,12 @@ public void required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublishe
blackboxSubscriberWithoutSetupTest(new BlackboxTestStageTestRun() {
@Override
public void run(BlackboxTestStage stage) throws Throwable {
final String onCompleteMethod = "required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublisherInOnComplete_call";

final Subscription subs = new Subscription() {
@Override
public void request(long n) {
final Optional<StackTraceElement> onCompleteStackTraceElement = env.findCallerMethodInStackTrace("onComplete");
final Optional<StackTraceElement> onCompleteStackTraceElement = env.findCallerMethodInStackTrace(onCompleteMethod);
if (onCompleteStackTraceElement.isDefined()) {
final StackTraceElement stackElem = onCompleteStackTraceElement.get();
env.flop(String.format("Subscription::request MUST NOT be called from Subscriber::onComplete (Rule 2.3)! (Caller: %s::%s line %d)",
Expand All @@ -141,7 +143,7 @@ public void request(long n) {

@Override
public void cancel() {
final Optional<StackTraceElement> onCompleteStackElement = env.findCallerMethodInStackTrace("onComplete");
final Optional<StackTraceElement> onCompleteStackElement = env.findCallerMethodInStackTrace(onCompleteMethod);
if (onCompleteStackElement.isDefined()) {
final StackTraceElement stackElem = onCompleteStackElement.get();
env.flop(String.format("Subscription::cancel MUST NOT be called from Subscriber::onComplete (Rule 2.3)! (Caller: %s::%s line %d)",
Expand All @@ -152,10 +154,15 @@ public void cancel() {

final Subscriber<T> sub = createSubscriber();
sub.onSubscribe(subs);
sub.onComplete();
required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublisherInOnComplete_call(sub);

env.verifyNoAsyncErrorsNoDelay();
}

/** Makes sure the onComplete is initiated with a recognizable stacktrace element on the current thread. */
void required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublisherInOnComplete_call(Subscriber<?> sub) {
sub.onComplete();
}
});
}

Expand All @@ -164,36 +171,41 @@ public void required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublishe
blackboxSubscriberWithoutSetupTest(new BlackboxTestStageTestRun() {
@Override
public void run(BlackboxTestStage stage) throws Throwable {
final String onErrorMethod = "required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublisherInOnError_call";

final Subscription subs = new Subscription() {
@Override
public void request(long n) {
Throwable thr = new Throwable();
for (StackTraceElement stackElem : thr.getStackTrace()) {
if (stackElem.getMethodName().equals("onError")) {
env.flop(String.format("Subscription::request MUST NOT be called from Subscriber::onError (Rule 2.3)! (Caller: %s::%s line %d)",
stackElem.getClassName(), stackElem.getMethodName(), stackElem.getLineNumber()));
}
final Optional<StackTraceElement> onCompleteStackElement = env.findCallerMethodInStackTrace(onErrorMethod);
if (onCompleteStackElement.isDefined()) {
final StackTraceElement stackElem = onCompleteStackElement.get();
env.flop(String.format("Subscription::request MUST NOT be called from Subscriber::onError (Rule 2.3)! (Caller: %s::%s line %d)",
stackElem.getClassName(), stackElem.getMethodName(), stackElem.getLineNumber()));
}
}

@Override
public void cancel() {
Throwable thr = new Throwable();
for (StackTraceElement stackElem : thr.getStackTrace()) {
if (stackElem.getMethodName().equals("onError")) {
env.flop(String.format("Subscription::cancel MUST NOT be called from Subscriber::onError (Rule 2.3)! (Caller: %s::%s line %d)",
stackElem.getClassName(), stackElem.getMethodName(), stackElem.getLineNumber()));
}
final Optional<StackTraceElement> onCompleteStackElement = env.findCallerMethodInStackTrace(onErrorMethod);
if (onCompleteStackElement.isDefined()) {
final StackTraceElement stackElem = onCompleteStackElement.get();
env.flop(String.format("Subscription::cancel MUST NOT be called from Subscriber::onError (Rule 2.3)! (Caller: %s::%s line %d)",
stackElem.getClassName(), stackElem.getMethodName(), stackElem.getLineNumber()));
}
}
};

final Subscriber<T> sub = createSubscriber();
sub.onSubscribe(subs);
sub.onError(new TestException());
required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublisherInOnError_call(sub);

env.verifyNoAsyncErrorsNoDelay();
}

/** Makes sure the onError is initiated with a recognizable stacktrace element on the current thread. */
void required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublisherInOnError_call(Subscriber<?> sub) {
sub.onError(new TestException());
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ public void required_spec203_mustNotCallMethodsOnSubscriptionOrPublisherInOnComp
subscriberTestWithoutSetup(new TestStageTestRun() {
@Override
public void run(WhiteboxTestStage stage) throws Throwable {
final String onCompleteMethod = "required_spec203_mustNotCallMethodsOnSubscriptionOrPublisherInOnComplete_call";

final Subscription subs = new Subscription() {
@Override
public void request(long n) {
final Optional<StackTraceElement> onCompleteStackTraceElement = env.findCallerMethodInStackTrace("onComplete");
final Optional<StackTraceElement> onCompleteStackTraceElement = env.findCallerMethodInStackTrace(onCompleteMethod);
if (onCompleteStackTraceElement.isDefined()) {
final StackTraceElement stackElem = onCompleteStackTraceElement.get();
env.flop(String.format("Subscription::request MUST NOT be called from Subscriber::onComplete (Rule 2.3)! (Caller: %s::%s line %d)",
Expand All @@ -146,7 +148,7 @@ public void request(long n) {

@Override
public void cancel() {
final Optional<StackTraceElement> onCompleteStackElement = env.findCallerMethodInStackTrace("onComplete");
final Optional<StackTraceElement> onCompleteStackElement = env.findCallerMethodInStackTrace(onCompleteMethod);
if (onCompleteStackElement.isDefined()) {
final StackTraceElement stackElem = onCompleteStackElement.get();
env.flop(String.format("Subscription::cancel MUST NOT be called from Subscriber::onComplete (Rule 2.3)! (Caller: %s::%s line %d)",
Expand All @@ -159,10 +161,15 @@ public void cancel() {
final Subscriber<T> sub = createSubscriber(stage.probe);

sub.onSubscribe(subs);
sub.onComplete();
required_spec203_mustNotCallMethodsOnSubscriptionOrPublisherInOnComplete_call(sub);

env.verifyNoAsyncErrorsNoDelay();
}

/** Makes sure the onComplete is initiated with a recognizable stacktrace element on the current thread. */
void required_spec203_mustNotCallMethodsOnSubscriptionOrPublisherInOnComplete_call(Subscriber<?> sub) {
sub.onComplete();
}
});
}

Expand All @@ -172,26 +179,26 @@ public void required_spec203_mustNotCallMethodsOnSubscriptionOrPublisherInOnErro
subscriberTestWithoutSetup(new TestStageTestRun() {
@Override
public void run(WhiteboxTestStage stage) throws Throwable {
final String onErrorMethod = "required_spec203_mustNotCallMethodsOnSubscriptionOrPublisherInOnError_call";

final Subscription subs = new Subscription() {
@Override
public void request(long n) {
Throwable thr = new Throwable();
for (StackTraceElement stackElem : thr.getStackTrace()) {
if (stackElem.getMethodName().equals("onError")) {
env.flop(String.format("Subscription::request MUST NOT be called from Subscriber::onError (Rule 2.3)! (Caller: %s::%s line %d)",
final Optional<StackTraceElement> onCompleteStackElement = env.findCallerMethodInStackTrace(onErrorMethod);
if (onCompleteStackElement.isDefined()) {
final StackTraceElement stackElem = onCompleteStackElement.get();
env.flop(String.format("Subscription::request MUST NOT be called from Subscriber::onError (Rule 2.3)! (Caller: %s::%s line %d)",
stackElem.getClassName(), stackElem.getMethodName(), stackElem.getLineNumber()));
}
}
}

@Override
public void cancel() {
Throwable thr = new Throwable();
for (StackTraceElement stackElem : thr.getStackTrace()) {
if (stackElem.getMethodName().equals("onError")) {
env.flop(String.format("Subscription::cancel MUST NOT be called from Subscriber::onError (Rule 2.3)! (Caller: %s::%s line %d)",
stackElem.getClassName(), stackElem.getMethodName(), stackElem.getLineNumber()));
}
final Optional<StackTraceElement> onCompleteStackElement = env.findCallerMethodInStackTrace(onErrorMethod);
if (onCompleteStackElement.isDefined()) {
final StackTraceElement stackElem = onCompleteStackElement.get();
env.flop(String.format("Subscription::cancel MUST NOT be called from Subscriber::onError (Rule 2.3)! (Caller: %s::%s line %d)",
stackElem.getClassName(), stackElem.getMethodName(), stackElem.getLineNumber()));
}
}
};
Expand All @@ -200,10 +207,15 @@ public void cancel() {
final Subscriber<T> sub = createSubscriber(stage.probe);

sub.onSubscribe(subs);
sub.onError(new TestException());
required_spec203_mustNotCallMethodsOnSubscriptionOrPublisherInOnError_call(sub);

env.verifyNoAsyncErrorsNoDelay();
}

/** Makes sure the onError is initiated with a recognizable stacktrace element on the current thread. */
void required_spec203_mustNotCallMethodsOnSubscriptionOrPublisherInOnError_call(Subscriber<?> sub) {
sub.onError(new TestException());
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@

package org.reactivestreams.tck;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import org.reactivestreams.tck.flow.support.TCKVerificationSupport;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;

import org.reactivestreams.*;
import org.reactivestreams.tck.flow.support.*;
import org.testng.annotations.*;

/**
* Validates that the TCK's {@link org.reactivestreams.tck.SubscriberBlackboxVerification} fails with nice human readable errors.
Expand Down Expand Up @@ -98,6 +92,166 @@ public void required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublishe
}, "Subscription::cancel MUST NOT be called from Subscriber::onError (Rule 2.3)!");
}

@Test
public void required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublisherInOnComplete_shouldPass_unrelatedCancelFromOnComplete() throws Throwable {
customSubscriberVerification(new Subscriber<Integer>() {
@Override
public void onSubscribe(final Subscription s) {
// emulate unrelated calls by issuing them from a method named `onComplete`
new Subscriber<Object>() {
@Override
public void onSubscribe(Subscription s) {
}

@Override
public void onNext(Object t) {
}

@Override
public void onError(Throwable t) {
}

@Override
public void onComplete() {
s.cancel();
}
}.onComplete();
}

@Override
public void onNext(Integer t) {
}

@Override
public void onError(Throwable t) {
}

@Override
public void onComplete() {
}
}).required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublisherInOnComplete();
}

@Test
public void required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublisherInOnComplete_shouldPass_unrelatedRequestFromOnComplete() throws Throwable {
customSubscriberVerification(new Subscriber<Integer>() {
@Override
public void onSubscribe(final Subscription s) {
// emulate unrelated calls by issuing them from a method named `onComplete`
new Subscriber<Object>() {
@Override
public void onSubscribe(Subscription s) {
}

@Override
public void onNext(Object t) {
}

@Override
public void onError(Throwable t) {
}

@Override
public void onComplete() {
s.request(1);
}
}.onComplete();
}

@Override
public void onNext(Integer t) {
}

@Override
public void onError(Throwable t) {
}

@Override
public void onComplete() {
}
}).required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublisherInOnComplete();
}

@Test
public void required_spec203_mustNotCallMethodsOnSubscriptionOrPublisherInOnComplete_shouldPass_unrelatedCancelFromOnError() throws Throwable {
customSubscriberVerification(new Subscriber<Integer>() {
@Override
public void onSubscribe(final Subscription s) {
// emulate unrelated calls by issuing them from a method named `onComplete`
new Subscriber<Object>() {
@Override
public void onSubscribe(Subscription s) {
}

@Override
public void onNext(Object t) {
}

@Override
public void onError(Throwable t) {
s.cancel();
}

@Override
public void onComplete() {
}
}.onError(null);
}

@Override
public void onNext(Integer t) {
}

@Override
public void onError(Throwable t) {
}

@Override
public void onComplete() {
}
}).required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublisherInOnError();
}

@Test
public void required_spec203_mustNotCallMethodsOnSubscriptionOrPublisherInOnComplete_shouldPass_unrelatedRequestFromOnError() throws Throwable {
customSubscriberVerification(new Subscriber<Integer>() {
@Override
public void onSubscribe(final Subscription s) {
// emulate unrelated calls by issuing them from a method named `onComplete`
new Subscriber<Object>() {
@Override
public void onSubscribe(Subscription s) {
}

@Override
public void onNext(Object t) {
}

@Override
public void onError(Throwable t) {
s.request(1);
}

@Override
public void onComplete() {
}
}.onError(null);
}

@Override
public void onNext(Integer t) {
}

@Override
public void onError(Throwable t) {
}

@Override
public void onComplete() {
}
}).required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublisherInOnError();
}

@Test
public void required_spec205_blackbox_mustCallSubscriptionCancelIfItAlreadyHasAnSubscriptionAndReceivesAnotherOnSubscribeSignal_shouldFail() throws Throwable {
requireTestFailure(new ThrowingRunnable() {
Expand Down
Loading