Package rx.observers
Class TestSubscriber<T>
- java.lang.Object
-
- rx.Subscriber<T>
-
- rx.observers.TestSubscriber<T>
-
- Type Parameters:
T- the value type
- All Implemented Interfaces:
Observer<T>,Subscription
public class TestSubscriber<T> extends Subscriber<T>
ATestSubscriberis a variety ofSubscriberthat you can use for unit testing, to perform assertions, inspect received events, or wrap a mockedSubscriber.
-
-
Field Summary
Fields Modifier and Type Field Description private intcompletionsThe number of onCompleted() calls.private Observer<T>delegateprivate java.util.List<java.lang.Throwable>errorsprivate static Observer<java.lang.Object>INERTThe shared no-op observer.private java.lang.ThreadlastSeenThreadprivate java.util.concurrent.CountDownLatchlatchprivate intvalueCountWritten after an onNext value has been added to thevalueslist.private java.util.List<T>values
-
Constructor Summary
Constructors Constructor Description TestSubscriber()Constructs a TestSubscriber with an initial request of Long.MAX_VALUE and no delegation.TestSubscriber(long initialRequest)Constructs a TestSubscriber with the initial request to be requested from upstream.TestSubscriber(Observer<T> delegate)Constructs a TestSubscriber which requests Long.MAX_VALUE and delegates events to the given Observer.TestSubscriber(Observer<T> delegate, long initialRequest)Constructs a TestSubscriber with the initial request to be requested from upstream and a delegate Observer to wrap.TestSubscriber(Subscriber<T> delegate)Constructs a TestSubscriber which requests Long.MAX_VALUE and delegates events to the given Subscriber.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidassertCompleted()Asserts that there is exactly one completion event.voidassertError(java.lang.Class<? extends java.lang.Throwable> clazz)Asserts that there is exactly one error event which is a subclass of the given class.voidassertError(java.lang.Throwable throwable)Asserts that there is a single onError event with the exact exception.(package private) voidassertionError(java.lang.String message)Combines an assertion error message with the current completion and error state of this TestSubscriber, giving more information when some assertXXX check fails.voidassertNoErrors()Asserts that thisSubscriberhas received noonErrornotifications.voidassertNotCompleted()Asserts that there is no completion event.voidassertNoTerminalEvent()Asserts that there are no onError and onCompleted events.voidassertNoValues()Asserts that there are no onNext events received.voidassertReceivedOnNext(java.util.List<T> items)Asserts that a particular sequence of items was received by thisSubscriberin order.voidassertTerminalEvent()Asserts that a single terminal event occurred, eitheronCompleted()oronError(java.lang.Throwable).voidassertUnsubscribed()Asserts that thisSubscriberis unsubscribed.voidassertValue(T value)Asserts that there is only a single received onNext event and that it marks the emission of a specific item.voidassertValueCount(int count)Asserts that the given number of onNext events are received.voidassertValues(T... values)Asserts that the received onNext events, in order, are the specified items.voidawaitTerminalEvent()Blocks until thisSubscriberreceives a notification that theObservableis complete (either anonCompletedoronErrornotification).voidawaitTerminalEvent(long timeout, java.util.concurrent.TimeUnit unit)Blocks until thisSubscriberreceives a notification that theObservableis complete (either anonCompletedoronErrornotification), or until a timeout expires.voidawaitTerminalEventAndUnsubscribeOnTimeout(long timeout, java.util.concurrent.TimeUnit unit)Blocks until thisSubscriberreceives a notification that theObservableis complete (either anonCompletedoronErrornotification), or until a timeout expires; if the Subscriber is interrupted before either of these events take place, this method unsubscribes the Subscriber from the Observable).booleanawaitValueCount(int expected, long timeout, java.util.concurrent.TimeUnit unit)Wait until the current committed value count is less than the expected amount by sleeping 1 unit at most timeout times and return true if at least the required amount of onNext values have been received.static <T> TestSubscriber<T>create()Factory method to construct a TestSubscriber with an initial request of Long.MAX_VALUE and no delegation.static <T> TestSubscriber<T>create(long initialRequest)Factory method to construct a TestSubscriber with the given initial request amount and no delegation.static <T> TestSubscriber<T>create(Observer<T> delegate)Factory method to construct a TestSubscriber which delegates events to the given Observer and an issues an initial request of Long.MAX_VALUE.static <T> TestSubscriber<T>create(Observer<T> delegate, long initialRequest)Factory method to construct a TestSubscriber which delegates events to the given Observer and issues the given initial request amount.static <T> TestSubscriber<T>create(Subscriber<T> delegate)Factory method to construct a TestSubscriber which delegates events to the given Subscriber and an issues an initial request of Long.MAX_VALUE.intgetCompletions()Returns the number of times onCompleted was called on this TestSubscriber.java.lang.ThreadgetLastSeenThread()Returns the last thread that was in use when an item or notification was received by thisSubscriber.java.util.List<Notification<T>>getOnCompletedEvents()Deprecated.usegetCompletions()instead.java.util.List<java.lang.Throwable>getOnErrorEvents()java.util.List<T>getOnNextEvents()Returns the sequence of items observed by thisSubscriber, as an orderedList.intgetValueCount()Returns the committed number of onNext elements that are safe to be read fromgetOnNextEvents()other threads.voidonCompleted()Notifies the Subscriber that theObservablehas finished sending push-based notifications.voidonError(java.lang.Throwable e)Notifies the Subscriber that theObservablehas experienced an error condition.voidonNext(T t)Provides the Subscriber with a new item to observe.voidrequestMore(long n)Allows calling the protectedSubscriber.request(long)from unit tests.-
Methods inherited from class rx.Subscriber
add, isUnsubscribed, onStart, request, setProducer, unsubscribe
-
-
-
-
Field Detail
-
values
private final java.util.List<T> values
-
errors
private final java.util.List<java.lang.Throwable> errors
-
completions
private int completions
The number of onCompleted() calls.
-
latch
private final java.util.concurrent.CountDownLatch latch
-
valueCount
private volatile int valueCount
Written after an onNext value has been added to thevalueslist.
-
lastSeenThread
private volatile java.lang.Thread lastSeenThread
-
INERT
private static final Observer<java.lang.Object> INERT
The shared no-op observer.
-
-
Constructor Detail
-
TestSubscriber
public TestSubscriber(long initialRequest)
Constructs a TestSubscriber with the initial request to be requested from upstream.- Parameters:
initialRequest- the initial request value, negative value will revert to the default unbounded behavior- Since:
- 1.1.0
-
TestSubscriber
public TestSubscriber(Observer<T> delegate, long initialRequest)
Constructs a TestSubscriber with the initial request to be requested from upstream and a delegate Observer to wrap.- Parameters:
initialRequest- the initial request value, negative value will revert to the default unbounded behaviordelegate- the Observer instance to wrap- Throws:
java.lang.NullPointerException- if delegate is null- Since:
- 1.1.0
-
TestSubscriber
public TestSubscriber(Subscriber<T> delegate)
Constructs a TestSubscriber which requests Long.MAX_VALUE and delegates events to the given Subscriber.- Parameters:
delegate- the subscriber to delegate to.- Throws:
java.lang.NullPointerException- if delegate is null- Since:
- 1.1.0
-
TestSubscriber
public TestSubscriber(Observer<T> delegate)
Constructs a TestSubscriber which requests Long.MAX_VALUE and delegates events to the given Observer.- Parameters:
delegate- the observer to delegate to.- Throws:
java.lang.NullPointerException- if delegate is null- Since:
- 1.1.0
-
TestSubscriber
public TestSubscriber()
Constructs a TestSubscriber with an initial request of Long.MAX_VALUE and no delegation.
-
-
Method Detail
-
create
public static <T> TestSubscriber<T> create()
Factory method to construct a TestSubscriber with an initial request of Long.MAX_VALUE and no delegation.- Type Parameters:
T- the value type- Returns:
- the created TestSubscriber instance
- Since:
- 1.1.0
-
create
public static <T> TestSubscriber<T> create(long initialRequest)
Factory method to construct a TestSubscriber with the given initial request amount and no delegation.- Type Parameters:
T- the value type- Parameters:
initialRequest- the initial request amount, negative values revert to the default unbounded mode- Returns:
- the created TestSubscriber instance
- Since:
- 1.1.0
-
create
public static <T> TestSubscriber<T> create(Observer<T> delegate, long initialRequest)
Factory method to construct a TestSubscriber which delegates events to the given Observer and issues the given initial request amount.- Type Parameters:
T- the value type- Parameters:
delegate- the observer to delegate events toinitialRequest- the initial request amount, negative values revert to the default unbounded mode- Returns:
- the created TestSubscriber instance
- Throws:
java.lang.NullPointerException- if delegate is null- Since:
- 1.1.0
-
create
public static <T> TestSubscriber<T> create(Subscriber<T> delegate)
Factory method to construct a TestSubscriber which delegates events to the given Subscriber and an issues an initial request of Long.MAX_VALUE.- Type Parameters:
T- the value type- Parameters:
delegate- the subscriber to delegate events to- Returns:
- the created TestSubscriber instance
- Throws:
java.lang.NullPointerException- if delegate is null- Since:
- 1.1.0
-
create
public static <T> TestSubscriber<T> create(Observer<T> delegate)
Factory method to construct a TestSubscriber which delegates events to the given Observer and an issues an initial request of Long.MAX_VALUE.- Type Parameters:
T- the value type- Parameters:
delegate- the observer to delegate events to- Returns:
- the created TestSubscriber instance
- Throws:
java.lang.NullPointerException- if delegate is null- Since:
- 1.1.0
-
onCompleted
public void onCompleted()
Notifies the Subscriber that theObservablehas finished sending push-based notifications.The
Observablewill not call this method if it callsonError(java.lang.Throwable).
-
getOnCompletedEvents
@Deprecated public java.util.List<Notification<T>> getOnCompletedEvents()
Deprecated.usegetCompletions()instead.Returns theNotifications representing each time thisSubscriberwas notified of sequence completion viaonCompleted(), as aList.- Returns:
- a list of Notifications representing calls to this Subscriber's
onCompleted()method
-
getCompletions
@Experimental public final int getCompletions()
Returns the number of times onCompleted was called on this TestSubscriber.- Returns:
- the number of times onCompleted was called on this TestSubscriber.
- Since:
- (if this graduates from Experimental/Beta to supported, replace this parenthetical with the release number)
-
onError
public void onError(java.lang.Throwable e)
Notifies the Subscriber that theObservablehas experienced an error condition.If the
Observablecalls this method, it will not thereafter callonNext(T)oronCompleted().- Parameters:
e- the exception encountered by the Observable
-
getOnErrorEvents
public java.util.List<java.lang.Throwable> getOnErrorEvents()
- Returns:
- a list of the Throwables that were passed to this Subscriber's
onError(java.lang.Throwable)method
-
onNext
public void onNext(T t)
Provides the Subscriber with a new item to observe.The
Observablemay call this method 0 or more times.The
Observablewill not call this method again after it calls eitheronCompleted()oronError(java.lang.Throwable).- Parameters:
t- the item emitted by the Observable
-
getValueCount
public final int getValueCount()
Returns the committed number of onNext elements that are safe to be read fromgetOnNextEvents()other threads.- Returns:
- the committed number of onNext elements
-
requestMore
public void requestMore(long n)
Allows calling the protectedSubscriber.request(long)from unit tests.- Parameters:
n- the maximum number of items you want the Observable to emit to the Subscriber at this time, orLong.MAX_VALUEif you want the Observable to emit items at its own pace
-
getOnNextEvents
public java.util.List<T> getOnNextEvents()
Returns the sequence of items observed by thisSubscriber, as an orderedList.- Returns:
- a list of items observed by this Subscriber, in the order in which they were observed
-
assertReceivedOnNext
public void assertReceivedOnNext(java.util.List<T> items)
Asserts that a particular sequence of items was received by thisSubscriberin order.- Parameters:
items- the sequence of items expected to have been observed- Throws:
java.lang.AssertionError- if the sequence of items observed does not exactly matchitems
-
awaitValueCount
@Experimental public final boolean awaitValueCount(int expected, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
Wait until the current committed value count is less than the expected amount by sleeping 1 unit at most timeout times and return true if at least the required amount of onNext values have been received.- Parameters:
expected- the expected number of onNext eventstimeout- the time to wait for the eventsunit- the time unit of waiting- Returns:
- true if the expected number of onNext events happened
- Throws:
java.lang.InterruptedException- if the sleep is interrupted- Since:
- (if this graduates from Experimental/Beta to supported, replace this parenthetical with the release number)
-
assertTerminalEvent
public void assertTerminalEvent()
Asserts that a single terminal event occurred, eitheronCompleted()oronError(java.lang.Throwable).- Throws:
java.lang.AssertionError- if not exactly one terminal event notification was received
-
assertUnsubscribed
public void assertUnsubscribed()
Asserts that thisSubscriberis unsubscribed.- Throws:
java.lang.AssertionError- if thisSubscriberis not unsubscribed
-
assertNoErrors
public void assertNoErrors()
Asserts that thisSubscriberhas received noonErrornotifications.- Throws:
java.lang.AssertionError- if thisSubscriberhas received one or moreonErrornotifications
-
awaitTerminalEvent
public void awaitTerminalEvent()
Blocks until thisSubscriberreceives a notification that theObservableis complete (either anonCompletedoronErrornotification).- Throws:
java.lang.RuntimeException- if the Subscriber is interrupted before the Observable is able to complete
-
awaitTerminalEvent
public void awaitTerminalEvent(long timeout, java.util.concurrent.TimeUnit unit)Blocks until thisSubscriberreceives a notification that theObservableis complete (either anonCompletedoronErrornotification), or until a timeout expires.- Parameters:
timeout- the duration of the timeoutunit- the units in whichtimeoutis expressed- Throws:
java.lang.RuntimeException- if the Subscriber is interrupted before the Observable is able to complete
-
awaitTerminalEventAndUnsubscribeOnTimeout
public void awaitTerminalEventAndUnsubscribeOnTimeout(long timeout, java.util.concurrent.TimeUnit unit)Blocks until thisSubscriberreceives a notification that theObservableis complete (either anonCompletedoronErrornotification), or until a timeout expires; if the Subscriber is interrupted before either of these events take place, this method unsubscribes the Subscriber from the Observable). If timeout expires then the Subscriber is unsubscribed from the Observable.- Parameters:
timeout- the duration of the timeoutunit- the units in whichtimeoutis expressed
-
getLastSeenThread
public java.lang.Thread getLastSeenThread()
Returns the last thread that was in use when an item or notification was received by thisSubscriber.- Returns:
- the
Threadon which this Subscriber last received an item or notification from the Observable it is subscribed to
-
assertCompleted
public void assertCompleted()
Asserts that there is exactly one completion event.- Throws:
java.lang.AssertionError- if there were zero, or more than one, onCompleted events- Since:
- 1.1.0
-
assertNotCompleted
public void assertNotCompleted()
Asserts that there is no completion event.- Throws:
java.lang.AssertionError- if there were one or more than one onCompleted events- Since:
- 1.1.0
-
assertError
public void assertError(java.lang.Class<? extends java.lang.Throwable> clazz)
Asserts that there is exactly one error event which is a subclass of the given class.- Parameters:
clazz- the class to check the error against.- Throws:
java.lang.AssertionError- if there were zero, or more than one, onError events, or if the single onError event did not carry an error of a subclass of the given class- Since:
- 1.1.0
-
assertError
public void assertError(java.lang.Throwable throwable)
Asserts that there is a single onError event with the exact exception.- Parameters:
throwable- the throwable to check- Throws:
java.lang.AssertionError- if there were zero, or more than one, onError events, or if the single onError event did not carry an error that matches the specified throwable- Since:
- 1.1.0
-
assertNoTerminalEvent
public void assertNoTerminalEvent()
Asserts that there are no onError and onCompleted events.- Throws:
java.lang.AssertionError- if there was either an onError or onCompleted event- Since:
- 1.1.0
-
assertNoValues
public void assertNoValues()
Asserts that there are no onNext events received.- Throws:
java.lang.AssertionError- if there were any onNext events- Since:
- 1.1.0
-
assertValueCount
public void assertValueCount(int count)
Asserts that the given number of onNext events are received.- Parameters:
count- the expected number of onNext events- Throws:
java.lang.AssertionError- if there were more or fewer onNext events than specified bycount- Since:
- 1.1.0
-
assertValues
public void assertValues(T... values)
Asserts that the received onNext events, in order, are the specified items.- Parameters:
values- the items to check- Throws:
java.lang.AssertionError- if the items emitted do not exactly match those specified byvalues- Since:
- 1.1.0
-
assertValue
public void assertValue(T value)
Asserts that there is only a single received onNext event and that it marks the emission of a specific item.- Parameters:
value- the item to check- Throws:
java.lang.AssertionError- if the Observable does not emit only the single item specified byvalue- Since:
- 1.1.0
-
assertionError
final void assertionError(java.lang.String message)
Combines an assertion error message with the current completion and error state of this TestSubscriber, giving more information when some assertXXX check fails.- Parameters:
message- the message to use for the error
-
-