use of org.reactivestreams.Subscriber in project vertx-proton by vert-x3.
the class TrackerSubscriberWhiteboxVerificationTckTest method createSubscriber.
@Override
public Subscriber<Tracker> createSubscriber(WhiteboxSubscriberProbe<Tracker> probe) {
int actualPort = server.actualPort();
ProtonClient client = ProtonClient.create(vertx);
AtomicReference<Subscriber<Tracker>> ref = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
client.connect("localhost", actualPort, result -> {
if (result.succeeded()) {
ProtonConnection conn = result.result();
conn.open();
// Modified stream impl, overriding methods to add test probe instrumentation
ProtonSubscriber<Tracker> stream = new ProtonSubscriberImpl("myAddress", (ProtonConnectionImpl) conn) {
@Override
public void onSubscribe(final Subscription s) {
super.onSubscribe(s);
probe.registerOnSubscribe(new SubscriberPuppet() {
@Override
public void triggerRequest(long n) {
s.request(n);
}
@Override
public void signalCancel() {
s.cancel();
}
});
}
@Override
public void onNext(Tracker value) {
probe.registerOnNext(value);
super.onNext(value);
}
@Override
public void onError(Throwable t) {
probe.registerOnError(t);
super.onError(t);
}
@Override
public void onComplete() {
probe.registerOnComplete();
super.onComplete();
}
};
ref.set(stream);
} else {
LOG.error("Connection failed");
}
latch.countDown();
});
try {
LOG.trace("Awaiting connection");
boolean res = latch.await(2, TimeUnit.SECONDS);
LOG.trace("Client connected: " + res);
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while creating subscriber", e);
}
return ref.get();
}
use of org.reactivestreams.Subscriber in project ratpack by ratpack.
the class ByteBufComposingPublisher method subscribe.
@Override
public void subscribe(Subscriber<? super CompositeByteBuf> subscriber) {
subscriber.onSubscribe(new ManagedSubscription<CompositeByteBuf>(subscriber, ByteBuf::release) {
private Subscription subscription;
private CompositeByteBuf composite;
private volatile State state;
@Override
protected void onRequest(long n) {
if (subscription == null) {
upstream.subscribe(new Subscriber<ByteBuf>() {
@Override
public void onSubscribe(Subscription s) {
subscription = s;
state = State.Fetching;
s.request(1);
}
@Override
public void onNext(ByteBuf t) {
if (state == State.Closed) {
t.release();
return;
}
if (composite == null) {
composite = alloc.compositeBuffer(maxNum);
}
composite.addComponent(true, t);
if (composite.numComponents() == maxNum || composite.readableBytes() >= watermark) {
state = State.Writing;
emitNext(composite);
composite = null;
maybeFetch();
} else {
subscription.request(1);
}
}
@Override
public void onError(Throwable t) {
state = State.Closed;
ReferenceCountUtil.release(composite);
emitError(t);
}
@Override
public void onComplete() {
state = State.Closed;
if (composite != null) {
emitNext(composite);
}
emitComplete();
}
});
} else {
maybeFetch();
}
}
private void maybeFetch() {
if (getDemand() > 0 && state != State.Fetching) {
state = State.Fetching;
subscription.request(1);
}
}
@Override
protected void onCancel() {
state = State.Closed;
ReferenceCountUtil.release(composite);
if (subscription != null) {
subscription.cancel();
}
}
});
}
use of org.reactivestreams.Subscriber in project ratpack by ratpack.
the class ConcatPublisher method subscribe.
@Override
public void subscribe(Subscriber<? super T> s) {
s.onSubscribe(new ManagedSubscription<T>(s, disposer) {
Iterator<? extends Publisher<? extends T>> iterator = publishers.iterator();
Subscription current;
@Override
protected void onRequest(long n) {
if (current == null) {
if (iterator.hasNext()) {
Publisher<? extends T> publisher = iterator.next();
publisher.subscribe(new Subscriber<T>() {
@Override
public void onSubscribe(Subscription s) {
current = s;
s.request(n);
}
@Override
public void onNext(T t) {
emitNext(t);
}
@Override
public void onError(Throwable t) {
emitError(t);
}
@Override
public void onComplete() {
current = null;
long demand = getDemand();
if (demand > 0) {
onRequest(demand);
}
}
});
} else {
emitComplete();
}
} else {
current.request(n);
}
}
@Override
protected void onCancel() {
if (current != null) {
current.cancel();
}
}
});
}
use of org.reactivestreams.Subscriber in project ratpack by ratpack.
the class FlatMapPublisher method subscribe.
@Override
public void subscribe(final Subscriber<? super O> outSubscriber) {
input.subscribe(new Subscriber<I>() {
private Subscription subscription;
private final AtomicBoolean done = new AtomicBoolean();
@Override
public void onSubscribe(Subscription subscription) {
this.subscription = new Subscription() {
@Override
public void request(long n) {
subscription.request(n);
}
@Override
public void cancel() {
subscription.cancel();
}
};
outSubscriber.onSubscribe(this.subscription);
}
@Override
public void onNext(I in) {
if (done.get()) {
return;
}
Promise<? extends O> out;
try {
out = function.apply(in);
} catch (Throwable throwable) {
subscription.cancel();
innerOnError(throwable);
return;
}
out.onError(e -> {
subscription.cancel();
innerOnError(e);
}).then(v -> {
if (!done.get()) {
outSubscriber.onNext(v);
}
});
}
@Override
public void onError(Throwable t) {
Promise.value(t).then(this::innerOnError);
}
public void innerOnError(Throwable t) {
if (done.compareAndSet(false, true)) {
outSubscriber.onError(t);
}
}
@Override
public void onComplete() {
Operation.noop().then(() -> {
if (done.compareAndSet(false, true)) {
outSubscriber.onComplete();
}
});
}
});
}
use of org.reactivestreams.Subscriber in project RxJava by ReactiveX.
the class FlowableSequenceEqualTest method doubleErrorFlowable.
@Test
public void doubleErrorFlowable() {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
Flowable.sequenceEqual(Flowable.never(), new Flowable<Object>() {
@Override
protected void subscribeActual(Subscriber<? super Object> s) {
s.onSubscribe(new BooleanSubscription());
s.onError(new TestException("First"));
s.onError(new TestException("Second"));
}
}, 8).toFlowable().test().assertFailureAndMessage(TestException.class, "First");
TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
} finally {
RxJavaPlugins.reset();
}
}
Aggregations