/** * Wraps this Subject and serializes the calls to the onSubscribe, onNext, onError and * onComplete methods, making them thread-safe. * <p>The method is thread-safe. * @return the wrapped and serialized subject */ @NonNull public final Subject<T> toSerialized() { if (this instanceof SerializedSubject) { return this; } return new SerializedSubject<T>(this); } }
/** * Wraps this Subject and serializes the calls to the onSubscribe, onNext, onError and * onComplete methods, making them thread-safe. * <p>The method is thread-safe. * @return the wrapped and serialized subject */ @NonNull public final Subject<T> toSerialized() { if (this instanceof SerializedSubject) { return this; } return new SerializedSubject<T>(this); } }
@Test public void testBasic() { SerializedSubject<String> subject = new SerializedSubject<String>(PublishSubject.<String> create()); TestObserver<String> to = new TestObserver<String>(); subject.subscribe(to); subject.onNext("hello"); subject.onComplete(); to.awaitTerminalEvent(); to.assertValue("hello"); }