Skip to content
Closed
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
6 changes: 4 additions & 2 deletions src/main/java/rx/observers/TestObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public List<Object> getEvents() {
* @throws AssertionError
* if the sequence of items observed does not exactly match {@code items}
*/
public void assertReceivedOnNext(List<T> items) {
public TestObserver assertReceivedOnNext(List<T> items) {
if (onNextEvents.size() != items.size()) {
throw new AssertionError("Number of items does not match. Provided: " + items.size() + " Actual: " + onNextEvents.size());
}
Expand All @@ -132,6 +132,7 @@ public void assertReceivedOnNext(List<T> items) {
}
}

return this;
}

/**
Expand All @@ -140,7 +141,7 @@ public void assertReceivedOnNext(List<T> items) {
* @throws AssertionError
* if not exactly one terminal event notification was received
*/
public void assertTerminalEvent() {
public TestObserver assertTerminalEvent() {
if (onErrorEvents.size() > 1) {
throw new AssertionError("Too many onError events: " + onErrorEvents.size());
}
Expand All @@ -156,6 +157,7 @@ public void assertTerminalEvent() {
if (onCompletedEvents.size() == 0 && onErrorEvents.size() == 0) {
throw new AssertionError("No terminal events received.");
}
return this;
}

// do nothing ... including swallowing errors
Expand Down
48 changes: 32 additions & 16 deletions src/main/java/rx/observers/TestSubscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,9 @@ public List<T> getOnNextEvents() {
* @throws AssertionError
* if the sequence of items observed does not exactly match {@code items}
*/
public void assertReceivedOnNext(List<T> items) {
public TestSubscriber assertReceivedOnNext(List<T> items) {
testObserver.assertReceivedOnNext(items);
return this;
}

/**
Expand All @@ -235,8 +236,9 @@ public void assertReceivedOnNext(List<T> items) {
* @throws AssertionError
* if not exactly one terminal event notification was received
*/
public void assertTerminalEvent() {
public TestSubscriber assertTerminalEvent() {
testObserver.assertTerminalEvent();
return this;
}

/**
Expand All @@ -245,10 +247,11 @@ public void assertTerminalEvent() {
* @throws AssertionError
* if this {@code Subscriber} is not unsubscribed
*/
public void assertUnsubscribed() {
public TestSubscriber assertUnsubscribed() {
if (!isUnsubscribed()) {
throw new AssertionError("Not unsubscribed.");
}
return this;
}

/**
Expand All @@ -257,7 +260,7 @@ public void assertUnsubscribed() {
* @throws AssertionError
* if this {@code Subscriber} has received one or more {@code onError} notifications
*/
public void assertNoErrors() {
public TestSubscriber assertNoErrors() {
List<Throwable> onErrorEvents = getOnErrorEvents();
if (onErrorEvents.size() > 0) {
AssertionError ae = new AssertionError("Unexpected onError events: " + getOnErrorEvents().size());
Expand All @@ -268,6 +271,7 @@ public void assertNoErrors() {
}
throw ae;
}
return this;
}


Expand All @@ -278,12 +282,13 @@ public void assertNoErrors() {
* @throws RuntimeException
* if the Subscriber is interrupted before the Observable is able to complete
*/
public void awaitTerminalEvent() {
public TestSubscriber awaitTerminalEvent() {
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted", e);
}
return this;
}

/**
Expand All @@ -297,12 +302,13 @@ public void awaitTerminalEvent() {
* @throws RuntimeException
* if the Subscriber is interrupted before the Observable is able to complete
*/
public void awaitTerminalEvent(long timeout, TimeUnit unit) {
public TestSubscriber awaitTerminalEvent(long timeout, TimeUnit unit) {
try {
latch.await(timeout, unit);
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted", e);
}
return this;
}

/**
Expand All @@ -316,7 +322,7 @@ public void awaitTerminalEvent(long timeout, TimeUnit unit) {
* @param unit
* the units in which {@code timeout} is expressed
*/
public void awaitTerminalEventAndUnsubscribeOnTimeout(long timeout, TimeUnit unit) {
public TestSubscriber awaitTerminalEventAndUnsubscribeOnTimeout(long timeout, TimeUnit unit) {
try {
boolean result = latch.await(timeout, unit);
if (!result) {
Expand All @@ -326,6 +332,7 @@ public void awaitTerminalEventAndUnsubscribeOnTimeout(long timeout, TimeUnit uni
} catch (InterruptedException e) {
unsubscribe();
}
return this;
}

/**
Expand All @@ -346,14 +353,15 @@ public Thread getLastSeenThread() {
* @since (if this graduates from "Experimental" replace this parenthetical with the release number)
*/
@Experimental
public void assertCompleted() {
public TestSubscriber assertCompleted() {
int s = testObserver.getOnCompletedEvents().size();
if (s == 0) {
throw new AssertionError("Not completed!");
} else
if (s > 1) {
throw new AssertionError("Completed multiple times: " + s);
}
return this;
}

/**
Expand All @@ -363,14 +371,15 @@ public void assertCompleted() {
* @since (if this graduates from "Experimental" replace this parenthetical with the release number)
*/
@Experimental
public void assertNotCompleted() {
public TestSubscriber assertNotCompleted() {
int s = testObserver.getOnCompletedEvents().size();
if (s == 1) {
throw new AssertionError("Completed!");
} else
if (s > 1) {
throw new AssertionError("Completed multiple times: " + s);
}
return this;
}

/**
Expand All @@ -382,7 +391,7 @@ public void assertNotCompleted() {
* @since (if this graduates from "Experimental" replace this parenthetical with the release number)
*/
@Experimental
public void assertError(Class<? extends Throwable> clazz) {
public TestSubscriber assertError(Class<? extends Throwable> clazz) {
List<Throwable> err = testObserver.getOnErrorEvents();
if (err.size() == 0) {
throw new AssertionError("No errors");
Expand All @@ -397,6 +406,7 @@ public void assertError(Class<? extends Throwable> clazz) {
ae.initCause(err.get(0));
throw ae;
}
return this;
}

/**
Expand All @@ -408,7 +418,7 @@ public void assertError(Class<? extends Throwable> clazz) {
* @since (if this graduates from "Experimental" replace this parenthetical with the release number)
*/
@Experimental
public void assertError(Throwable throwable) {
public TestSubscriber assertError(Throwable throwable) {
List<Throwable> err = testObserver.getOnErrorEvents();
if (err.size() == 0) {
throw new AssertionError("No errors");
Expand All @@ -423,6 +433,7 @@ public void assertError(Throwable throwable) {
ae.initCause(err.get(0));
throw ae;
}
return this;
}

/**
Expand All @@ -432,7 +443,7 @@ public void assertError(Throwable throwable) {
* @since (if this graduates from "Experimental" replace this parenthetical with the release number)
*/
@Experimental
public void assertNoTerminalEvent() {
public TestSubscriber assertNoTerminalEvent() {
List<Throwable> err = testObserver.getOnErrorEvents();
int s = testObserver.getOnCompletedEvents().size();
if (err.size() > 0 || s > 0) {
Expand All @@ -449,6 +460,7 @@ public void assertNoTerminalEvent() {
throw ae;
}
}
return this;
}

/**
Expand All @@ -458,11 +470,12 @@ public void assertNoTerminalEvent() {
* @since (if this graduates from "Experimental" replace this parenthetical with the release number)
*/
@Experimental
public void assertNoValues() {
public TestSubscriber assertNoValues() {
int s = testObserver.getOnNextEvents().size();
if (s > 0) {
throw new AssertionError("No onNext events expected yet some received: " + s);
}
return this;
}

/**
Expand All @@ -473,11 +486,12 @@ public void assertNoValues() {
* @since (if this graduates from "Experimental" replace this parenthetical with the release number)
*/
@Experimental
public void assertValueCount(int count) {
public TestSubscriber assertValueCount(int count) {
int s = testObserver.getOnNextEvents().size();
if (s != count) {
throw new AssertionError("Number of onNext events differ; expected: " + count + ", actual: " + s);
}
return this;
}

/**
Expand All @@ -488,8 +502,9 @@ public void assertValueCount(int count) {
* @since (if this graduates from "Experimental" replace this parenthetical with the release number)
*/
@Experimental
public void assertValues(T... values) {
public TestSubscriber assertValues(T... values) {
assertReceivedOnNext(Arrays.asList(values));
return this;
}

/**
Expand All @@ -500,7 +515,8 @@ public void assertValues(T... values) {
* @since (if this graduates from "Experimental" replace this parenthetical with the release number)
*/
@Experimental
public void assertValue(T value) {
public TestSubscriber assertValue(T value) {
assertReceivedOnNext(Collections.singletonList(value));
return this;
}
}