reactor.core.publisher.EmitterProcessor Java Examples

The following examples show how to use reactor.core.publisher.EmitterProcessor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReactiveTypeHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void writeStreamJson() throws Exception {

	this.servletRequest.addHeader("Accept", "application/stream+json");

	EmitterProcessor<Bar> processor = EmitterProcessor.create();
	ResponseBodyEmitter emitter = handleValue(processor, Flux.class, forClass(Bar.class));

	EmitterHandler emitterHandler = new EmitterHandler();
	emitter.initialize(emitterHandler);

	ServletServerHttpResponse message = new ServletServerHttpResponse(this.servletResponse);
	emitter.extendResponse(message);

	Bar bar1 = new Bar("foo");
	Bar bar2 = new Bar("bar");

	processor.onNext(bar1);
	processor.onNext(bar2);
	processor.onComplete();

	assertEquals("application/stream+json", message.getHeaders().getContentType().toString());
	assertEquals(Arrays.asList(bar1, "\n", bar2, "\n"), emitterHandler.getValues());
}
 
Example #2
Source File: JetLinksConfiguration.java    From jetlinks-community with Apache License 2.0 6 votes vote down vote up
@Bean(destroyMethod = "shutdown")
public DefaultDecodedClientMessageHandler defaultDecodedClientMessageHandler(MessageHandler handler,
                                                                             DeviceMessageConnector messageConnector,
                                                                             DeviceSessionManager deviceSessionManager,
                                                                             ApplicationEventPublisher eventPublisher) {
    DefaultDecodedClientMessageHandler clientMessageHandler = new DefaultDecodedClientMessageHandler(handler, deviceSessionManager,
        EmitterProcessor.create(false)
    );
    // TODO: 2019/12/31 应该统一由消息网关处理
    clientMessageHandler
        .subscribe()
        .parallel()
        .runOn(Schedulers.parallel())
        .flatMap(msg -> messageConnector.onMessage(msg).onErrorContinue((err, r) -> log.error(err.getMessage(), err)))
        .subscribe();

    return clientMessageHandler;
}
 
Example #3
Source File: QueryFlow.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
static Flux<Flux<ServerMessage>> execute(Client client, TextQuery query, List<Binding> bindings) {
    switch (bindings.size()) {
        case 1: // Most case.
            return Flux.defer(() -> execute0(client, query, bindings.get(0)).windowUntil(RESULT_DONE));
        case 0:
            return Flux.empty();
        default:
            Iterator<Binding> iter = bindings.iterator();
            EmitterProcessor<Binding> processor = EmitterProcessor.create(1, true);
            Runnable complete = () -> OperatorUtils.emitIterator(processor, iter);

            processor.onNext(iter.next());

            return processor.concatMap(it -> execute0(client, query, it).doOnComplete(complete))
                .doOnCancel(() -> Binding.clearSubsequent(iter))
                .doOnError(ignored -> Binding.clearSubsequent(iter))
                .windowUntil(RESULT_DONE);
    }
}
 
Example #4
Source File: ResponseBodyEmitterReturnValueHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test // gh-21972
public void responseBodyFluxWithError() throws Exception {

	this.request.addHeader("Accept", "text/event-stream");

	MethodParameter type = on(TestController.class).resolveReturnType(Flux.class, String.class);
	EmitterProcessor<String> processor = EmitterProcessor.create();
	this.handler.handleReturnValue(processor, type, this.mavContainer, this.webRequest);

	assertTrue(this.request.isAsyncStarted());

	IllegalStateException ex = new IllegalStateException("wah wah");
	processor.onError(ex);
	processor.onComplete();

	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.webRequest);
	assertSame(ex, asyncManager.getConcurrentResult());
	assertNull(this.response.getContentType());
}
 
Example #5
Source File: ResponseBodyEmitterReturnValueHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void responseEntityFlux() throws Exception {

	EmitterProcessor<String> processor = EmitterProcessor.create();
	ResponseEntity<Flux<String>> entity = ResponseEntity.ok().body(processor);
	ResolvableType bodyType = forClassWithGenerics(Flux.class, String.class);
	MethodParameter type = on(TestController.class).resolveReturnType(ResponseEntity.class, bodyType);
	this.handler.handleReturnValue(entity, type, this.mavContainer, this.webRequest);

	assertTrue(this.request.isAsyncStarted());
	assertEquals(200, this.response.getStatus());

	processor.onNext("foo");
	processor.onNext("bar");
	processor.onNext("baz");
	processor.onComplete();

	assertEquals("text/plain", this.response.getContentType());
	assertEquals("foobarbaz", this.response.getContentAsString());
}
 
Example #6
Source File: OperatorUtils.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
/**
 * Emit next value from an {@link Iterator} to an {@link EmitterProcessor} if it
 * has not been cancelled or terminated.
 *
 * @param processor want to emit next value to this processor.
 * @param iterator  source values' iterator, can be intermediate state.
 * @param <T>       the type of values in {@link Iterator} sources.
 */
public static <T> void emitIterator(EmitterProcessor<T> processor, Iterator<T> iterator) {
    if (processor.isCancelled() || processor.isTerminated()) {
        return;
    }

    try {
        if (iterator.hasNext()) {
            processor.onNext(iterator.next());
        } else {
            processor.onComplete();
        }
    } catch (Throwable e) {
        processor.onError(e);
    }
}
 
Example #7
Source File: ReactiveTypeHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void writeText() throws Exception {

	EmitterProcessor<String> processor = EmitterProcessor.create();
	ResponseBodyEmitter emitter = handleValue(processor, Flux.class, forClass(String.class));

	EmitterHandler emitterHandler = new EmitterHandler();
	emitter.initialize(emitterHandler);

	processor.onNext("The quick");
	processor.onNext(" brown fox jumps over ");
	processor.onNext("the lazy dog");
	processor.onComplete();

	assertEquals("The quick brown fox jumps over the lazy dog", emitterHandler.getValuesAsText());
}
 
Example #8
Source File: ReactiveTypeHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void writeStreamJson() throws Exception {

	this.servletRequest.addHeader("Accept", "application/stream+json");

	EmitterProcessor<Bar> processor = EmitterProcessor.create();
	ResponseBodyEmitter emitter = handleValue(processor, Flux.class, forClass(Bar.class));

	EmitterHandler emitterHandler = new EmitterHandler();
	emitter.initialize(emitterHandler);

	ServletServerHttpResponse message = new ServletServerHttpResponse(this.servletResponse);
	emitter.extendResponse(message);

	Bar bar1 = new Bar("foo");
	Bar bar2 = new Bar("bar");

	processor.onNext(bar1);
	processor.onNext(bar2);
	processor.onComplete();

	assertEquals("application/stream+json", message.getHeaders().getContentType().toString());
	assertEquals(Arrays.asList(bar1, "\n", bar2, "\n"), emitterHandler.getValues());
}
 
Example #9
Source File: ReactiveTypeHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void deferredResultSubscriberWithMultipleValues() throws Exception {

	// JSON must be preferred for Flux<String> -> List<String> or else we stream
	this.servletRequest.addHeader("Accept", "application/json");

	Bar bar1 = new Bar("foo");
	Bar bar2 = new Bar("bar");

	EmitterProcessor<Bar> emitter = EmitterProcessor.create();
	testDeferredResultSubscriber(emitter, Flux.class, forClass(Bar.class), () -> {
		emitter.onNext(bar1);
		emitter.onNext(bar2);
		emitter.onComplete();
	}, Arrays.asList(bar1, bar2));
}
 
Example #10
Source File: ReactiveTypeHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void writeServerSentEventsWithBuilder() throws Exception {

	ResolvableType type = ResolvableType.forClassWithGenerics(ServerSentEvent.class, String.class);

	EmitterProcessor<ServerSentEvent<?>> processor = EmitterProcessor.create();
	SseEmitter sseEmitter = (SseEmitter) handleValue(processor, Flux.class, type);

	EmitterHandler emitterHandler = new EmitterHandler();
	sseEmitter.initialize(emitterHandler);

	processor.onNext(ServerSentEvent.builder("foo").id("1").build());
	processor.onNext(ServerSentEvent.builder("bar").id("2").build());
	processor.onNext(ServerSentEvent.builder("baz").id("3").build());
	processor.onComplete();

	assertEquals("id:1\ndata:foo\n\nid:2\ndata:bar\n\nid:3\ndata:baz\n\n",
			emitterHandler.getValuesAsText());
}
 
Example #11
Source File: ReactiveTypeHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void writeServerSentEvents() throws Exception {

	this.servletRequest.addHeader("Accept", "text/event-stream");
	EmitterProcessor<String> processor = EmitterProcessor.create();
	SseEmitter sseEmitter = (SseEmitter) handleValue(processor, Flux.class, forClass(String.class));

	EmitterHandler emitterHandler = new EmitterHandler();
	sseEmitter.initialize(emitterHandler);

	processor.onNext("foo");
	processor.onNext("bar");
	processor.onNext("baz");
	processor.onComplete();

	assertEquals("data:foo\n\ndata:bar\n\ndata:baz\n\n", emitterHandler.getValuesAsText());
}
 
Example #12
Source File: ResponseBodyEmitterReturnValueHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void responseEntityFlux() throws Exception {

	EmitterProcessor<String> processor = EmitterProcessor.create();
	ResponseEntity<Flux<String>> entity = ResponseEntity.ok().body(processor);
	ResolvableType bodyType = forClassWithGenerics(Flux.class, String.class);
	MethodParameter type = on(TestController.class).resolveReturnType(ResponseEntity.class, bodyType);
	this.handler.handleReturnValue(entity, type, this.mavContainer, this.webRequest);

	assertTrue(this.request.isAsyncStarted());
	assertEquals(200, this.response.getStatus());
	assertEquals("text/plain", this.response.getContentType());

	processor.onNext("foo");
	processor.onNext("bar");
	processor.onNext("baz");
	processor.onComplete();

	assertEquals("foobarbaz", this.response.getContentAsString());
}
 
Example #13
Source File: ReactiveTypeHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void deferredResultSubscriberWithMultipleValues() throws Exception {

	// JSON must be preferred for Flux<String> -> List<String> or else we stream
	this.servletRequest.addHeader("Accept", "application/json");

	Bar bar1 = new Bar("foo");
	Bar bar2 = new Bar("bar");

	EmitterProcessor<Bar> emitter = EmitterProcessor.create();
	testDeferredResultSubscriber(emitter, Flux.class, forClass(Bar.class), () -> {
		emitter.onNext(bar1);
		emitter.onNext(bar2);
		emitter.onComplete();
	}, Arrays.asList(bar1, bar2));
}
 
Example #14
Source File: ReactiveTypeHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void writeServerSentEvents() throws Exception {

	this.servletRequest.addHeader("Accept", "text/event-stream");
	EmitterProcessor<String> processor = EmitterProcessor.create();
	SseEmitter sseEmitter = (SseEmitter) handleValue(processor, Flux.class, forClass(String.class));

	EmitterHandler emitterHandler = new EmitterHandler();
	sseEmitter.initialize(emitterHandler);

	processor.onNext("foo");
	processor.onNext("bar");
	processor.onNext("baz");
	processor.onComplete();

	assertEquals("data:foo\n\ndata:bar\n\ndata:baz\n\n", emitterHandler.getValuesAsText());
}
 
Example #15
Source File: ReactiveTypeHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void writeServerSentEventsWithBuilder() throws Exception {

	ResolvableType type = ResolvableType.forClassWithGenerics(ServerSentEvent.class, String.class);

	EmitterProcessor<ServerSentEvent<?>> processor = EmitterProcessor.create();
	SseEmitter sseEmitter = (SseEmitter) handleValue(processor, Flux.class, type);

	EmitterHandler emitterHandler = new EmitterHandler();
	sseEmitter.initialize(emitterHandler);

	processor.onNext(ServerSentEvent.builder("foo").id("1").build());
	processor.onNext(ServerSentEvent.builder("bar").id("2").build());
	processor.onNext(ServerSentEvent.builder("baz").id("3").build());
	processor.onComplete();

	assertEquals("id:1\ndata:foo\n\nid:2\ndata:bar\n\nid:3\ndata:baz\n\n",
			emitterHandler.getValuesAsText());
}
 
Example #16
Source File: ResponseBodyEmitterReturnValueHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void responseBodyFlux() throws Exception {

	this.request.addHeader("Accept", "text/event-stream");

	MethodParameter type = on(TestController.class).resolveReturnType(Flux.class, String.class);
	EmitterProcessor<String> processor = EmitterProcessor.create();
	this.handler.handleReturnValue(processor, type, this.mavContainer, this.webRequest);

	assertTrue(this.request.isAsyncStarted());
	assertEquals(200, this.response.getStatus());

	processor.onNext("foo");
	processor.onNext("bar");
	processor.onNext("baz");
	processor.onComplete();

	assertEquals("text/event-stream;charset=UTF-8", this.response.getContentType());
	assertEquals("data:foo\n\ndata:bar\n\ndata:baz\n\n", this.response.getContentAsString());
}
 
Example #17
Source File: ReactiveTypeHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void writeText() throws Exception {

	EmitterProcessor<String> processor = EmitterProcessor.create();
	ResponseBodyEmitter emitter = handleValue(processor, Flux.class, forClass(String.class));

	EmitterHandler emitterHandler = new EmitterHandler();
	emitter.initialize(emitterHandler);

	processor.onNext("The quick");
	processor.onNext(" brown fox jumps over ");
	processor.onNext("the lazy dog");
	processor.onComplete();

	assertEquals("The quick brown fox jumps over the lazy dog", emitterHandler.getValuesAsText());
}
 
Example #18
Source File: OperatorUtils.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
/**
 * Emit next value from an {@link Iterator} to an {@link EmitterProcessor} if it
 * has not been cancelled or terminated.
 *
 * @param processor want to emit next value to this processor.
 * @param iterator  source values' iterator, can be intermediate state.
 * @param <T>       the type of values in {@link Iterator} sources.
 */
public static <T> void emitIterator(EmitterProcessor<T> processor, Iterator<T> iterator) {
    if (processor.isCancelled() || processor.isTerminated()) {
        return;
    }

    try {
        if (iterator.hasNext()) {
            processor.onNext(iterator.next());
        } else {
            processor.onComplete();
        }
    } catch (Throwable e) {
        processor.onError(e);
    }
}
 
Example #19
Source File: QueryFlow.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
static Flux<Flux<ServerMessage>> execute(Client client, TextQuery query, List<Binding> bindings) {
    switch (bindings.size()) {
        case 1: // Most case.
            return Flux.defer(() -> execute0(client, query, bindings.get(0)).windowUntil(RESULT_DONE));
        case 0:
            return Flux.empty();
        default:
            Iterator<Binding> iter = bindings.iterator();
            EmitterProcessor<Binding> processor = EmitterProcessor.create(1, true);
            Runnable complete = () -> OperatorUtils.emitIterator(processor, iter);

            processor.onNext(iter.next());

            return processor.concatMap(it -> execute0(client, query, it).doOnComplete(complete))
                .doOnCancel(() -> Binding.clearSubsequent(iter))
                .doOnError(ignored -> Binding.clearSubsequent(iter))
                .windowUntil(RESULT_DONE);
    }
}
 
Example #20
Source File: LoginFlow.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
static Mono<Client> login(Client client, SslMode sslMode, String database, ConnectionContext context, String user, @Nullable CharSequence password) {
    LoginFlow flow = new LoginFlow(client, sslMode, database, context, user, password);
    EmitterProcessor<State> stateMachine = EmitterProcessor.create(1, true);

    stateMachine.onNext(State.INIT);

    Consumer<State> onStateNext = state -> {
        if (state == State.COMPLETED) {
            logger.debug("Login succeed, cleanup intermediate variables");
            flow.loginSuccess();
            stateMachine.onComplete();
        } else {
            stateMachine.onNext(state);
        }
    };
    Consumer<Throwable> onStateError = stateMachine::onError;

    return stateMachine.doOnNext(state -> {
        logger.debug("Login state {} handling", state);
        state.handle(flow).subscribe(onStateNext, onStateError);
    }).doOnError(ignored -> flow.loginFailed())
        .then(Mono.just(client));
}
 
Example #21
Source File: R091_EmitterProcessor.java    From reactor-workshop with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void multipleSubscribers() throws Exception {
    //given
    final EmitterProcessor<Long> proc = EmitterProcessor.create(10);

    //when
    pushSomeEvents(proc, 0, 3);

    //then
    proc
            .publishOn(Schedulers.elastic())
            .subscribe(
                    x -> log.info("A: Got {}", x),
                    e -> log.error("A: Error", e));
    proc
            .publishOn(Schedulers.elastic())
            .subscribe(
                    x -> log.info("B: Got {}", x),
                    e -> log.error("B: Error", e));

    pushSomeEvents(proc, 10, 3);
    TimeUnit.SECONDS.sleep(1);
}
 
Example #22
Source File: StreamingLogWebSocketHandler.java    From spring-cloud-app-broker with Apache License 2.0 6 votes vote down vote up
@Override
public Mono<Void> handle(WebSocketSession session) {
	String serviceInstanceId = getServiceInstanceId(session);
	LOG.info("Connection established [{}}], service instance {}",
		session.getHandshakeInfo().getRemoteAddress(),
		serviceInstanceId);

	EmitterProcessor<Envelope> processor = processors
		.computeIfAbsent(serviceInstanceId, s -> EmitterProcessor.create());

	eventPublisher.publishEvent(new StartServiceInstanceLoggingEvent(this, serviceInstanceId));
	LOG.info("Published event to start streaming logs for service instance with ID {}", serviceInstanceId);

	return session
		.send(processor.map(envelope -> session
			.binaryMessage(dataBufferFactory -> dataBufferFactory.wrap(Envelope.ADAPTER.encode(envelope)))))
		.then()
		.doFinally(signalType -> afterConnectionClosed(session))
		.doOnError(throwable -> LOG
			.error("Error handling logging stream for service instance " + serviceInstanceId, throwable));
}
 
Example #23
Source File: StreamingLogWebSocketHandler.java    From spring-cloud-app-broker with Apache License 2.0 6 votes vote down vote up
public void broadcastLogMessage(ServiceInstanceLogEvent event) {
	if (LOG.isDebugEnabled()) {
		LOG.debug("Received event to broadcast log message for " + event.getServiceInstanceId());
	}

	EmitterProcessor<Envelope> processor = this.processors.get(event.getServiceInstanceId());
	if (processor == null) {
		if (LOG.isWarnEnabled()) {
			LOG.warn("No processor found for {}, stopping log streaming", event.getServiceInstanceId());
		}

		eventPublisher.publishEvent(new StopServiceInstanceLoggingEvent(this, event.getServiceInstanceId()));

		return;
	}

	if (LOG.isDebugEnabled()) {
		LOG.debug("Sending message to client for {}", event.getServiceInstanceId());
	}

	processor.onNext(event.getEnvelope());
}
 
Example #24
Source File: SimpleConnectionPoolTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void limitsPendingConnectionsDueToConnectionEstablishment() {
    when(connectionFactory.createConnection(any(Origin.class), any(ConnectionSettings.class)))
            .thenReturn(Mono.from(EmitterProcessor.create()))
            .thenReturn(Mono.from(EmitterProcessor.create()))
            .thenReturn(Mono.from(EmitterProcessor.create()));

    ConnectionPoolSettings poolSettings = new ConnectionPoolSettings.Builder()
            .maxPendingConnectionsPerHost(2)
            .build();

    SimpleConnectionPool pool = new SimpleConnectionPool(origin, poolSettings, connectionFactory);

    Mono.from(pool.borrowConnection()).toFuture();
    Mono.from(pool.borrowConnection()).toFuture();

    StepVerifier.create(pool.borrowConnection())
            .expectError(MaxPendingConnectionsExceededException.class)
            .verify();

    assertEquals(pool.stats().connectionAttempts(), 2);
    assertEquals(pool.stats().pendingConnectionCount(), 2);

}
 
Example #25
Source File: SimpleConnectionPoolTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void emitsExceptionWhenPendingConnectionTimesOut() {
    EmitterProcessor<Connection> processor = EmitterProcessor.create();
    when(connectionFactory.createConnection(any(Origin.class), any(ConnectionSettings.class)))
            .thenReturn(Mono.from(processor));

    ConnectionPoolSettings poolSettings = new ConnectionPoolSettings.Builder()
            .pendingConnectionTimeout(500, MILLISECONDS)
            .build();

    SimpleConnectionPool pool = new SimpleConnectionPool(origin, poolSettings, connectionFactory);

    StepVerifier.create(pool.borrowConnection())
            .expectError(MaxPendingConnectionTimeoutException.class)
            .verifyThenAssertThat()
            .tookMoreThan(Duration.ofMillis(500));

    // And then ensure connection is placed in the active queue:
    processor.onNext(mock(Connection.class));

    assertEquals(pool.stats().availableConnectionCount(), 1);
    assertEquals(pool.stats().pendingConnectionCount(), 0);
    assertEquals(pool.stats().busyConnectionCount(), 0);
    assertEquals(pool.stats().connectionAttempts(), 1);
}
 
Example #26
Source File: StyxHostHttpClientTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void releasesIfRequestIsCancelledBeforeHeaders() {
    Connection connection = mockConnection(EmitterProcessor.create());
    ConnectionPool pool = mockPool(connection);
    Context context = mockContext();

    StyxHostHttpClient hostClient = new StyxHostHttpClient(pool);
    AtomicReference<Subscription> subscription = new AtomicReference<>();

    Flux.from(hostClient.sendRequest(request, context))
            .subscribe(new BaseSubscriber<LiveHttpResponse>() {
                @Override
                protected void hookOnSubscribe(Subscription s) {
                    super.hookOnSubscribe(s);
                    s.request(1);
                    subscription.set(s);
                }
            });

    subscription.get().cancel();
    verify(pool).closeConnection(any(Connection.class));
    verify(context).add(ORIGINID_CONTEXT_KEY, Id.id("mockorigin"));
}
 
Example #27
Source File: HttpPipelineHandlerTest.java    From styx with Apache License 2.0 6 votes vote down vote up
private void setUpFor2Requests() throws Exception {
    responseObservable2 = EmitterProcessor.create();
    responseUnsubscribed2 = new AtomicBoolean(false);
    response2 = response().build();

    CompletableFuture<Void> writerFuture2 = new CompletableFuture<>();

    HttpResponseWriter responseWriter2 = mock(HttpResponseWriter.class);
    when(responseWriter2.write(any(LiveHttpResponse.class))).thenReturn(writerFuture2);

    responseWriterFactory = mock(HttpResponseWriterFactory.class);
    when(responseWriterFactory.create(anyObject()))
            .thenReturn(responseWriter)
            .thenReturn(responseWriter2);

    pipeline = mock(HttpHandler.class);
    when(pipeline.handle(anyObject(), any(HttpInterceptor.Context.class)))
            .thenReturn(new Eventual<>(responseObservable.doOnCancel(() -> responseUnsubscribed.set(true))))
            .thenReturn(new Eventual<>(responseObservable2.doOnCancel(() -> responseUnsubscribed2.set(true))));

    request2 = get("/bar").id("REQUEST-2-ID").build();

    setupHandlerTo(ACCEPTING_REQUESTS);
}
 
Example #28
Source File: ResponseEventListenerTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void firesWhenResponseIsCancelledBeforeHeaders() {
    EmitterProcessor<LiveHttpResponse> publisher = EmitterProcessor.create();

    Flux<LiveHttpResponse> listener = ResponseEventListener.from(publisher)
            .whenCancelled(() -> cancelled.set(true))
            .whenHeadersComplete(() -> headers.set(true))
            .whenFinished(() -> finished.set(true))
            .apply();

    StepVerifier.create(listener)
            .expectNextCount(0)
            .thenCancel()
            .verify();

    assertFalse(headers.get());
    assertTrue(cancelled.get());
    assertTrue(finished.get());
}
 
Example #29
Source File: ResponseEventListenerTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void firesWhenContentCancelled() {
    EmitterProcessor<Buffer> contentPublisher = EmitterProcessor.create();

    Flux<LiveHttpResponse> listener = ResponseEventListener.from(
            Flux.just(response(OK)
                    .body(new ByteStream(contentPublisher))
                    .build()))
            .whenCancelled(() -> cancelled.set(true))
            .whenFinished(() -> finished.set(true))
            .apply();

    StepVerifier.create(listener)
            .consumeNextWith(response ->
                    StepVerifier.create(response.body())
                            .then(() -> assertFalse(cancelled.get()))
                            .thenCancel()
                            .verify())
            .verifyComplete();

    assertTrue(cancelled.get());
    assertTrue(finished.get());
}
 
Example #30
Source File: ZipOperatorTest.java    From cyclops with Apache License 2.0 6 votes vote down vote up
@Test
public void reactorTest() {

    EmitterProcessor<Integer> source1 = EmitterProcessor.create();
    EmitterProcessor<Integer> source2 = EmitterProcessor.create();
    ReactiveSeq<Integer> zippedFlux = Spouts.from(source1)
                                            .zip((t1, t2) -> t1 + t2,source2);
    AtomicReference<Integer> tap = new AtomicReference<>();
    zippedFlux.forEachAsync(it -> tap.set(it));

    source1.onNext(1);
    source2.onNext(2);
    source2.onNext(3);
    source2.onNext(4);

    assertThat(tap.get(),equalTo(3));

    source2.onNext(5);
    source1.onNext(6);

    assertThat(tap.get(),equalTo(9));
}