Search in sources :

Example 1 with ConsumeOneAndNeverCompleteInterceptor

use of org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor in project spring-graphql by spring-projects.

the class GraphQlWebSocketHandlerTests method clientCompletion.

@Test
void clientCompletion() {
    Sinks.Many<WebSocketMessage> input = Sinks.many().unicast().onBackpressureBuffer();
    input.tryEmitNext(toWebSocketMessage("{\"type\":\"connection_init\"}"));
    input.tryEmitNext(toWebSocketMessage(BOOK_SUBSCRIPTION));
    TestWebSocketSession session = handle(input.asFlux(), new ConsumeOneAndNeverCompleteInterceptor());
    String completeMessage = "{\"id\":\"" + SUBSCRIPTION_ID + "\",\"type\":\"complete\"}";
    StepVerifier.create(session.getOutput()).consumeNextWith((message) -> assertMessageType(message, GraphQlMessageType.CONNECTION_ACK)).consumeNextWith((message) -> assertMessageType(message, GraphQlMessageType.NEXT)).then(() -> input.tryEmitNext(toWebSocketMessage(completeMessage))).as("Second subscription with same id is possible only if the first was properly removed").then(() -> input.tryEmitNext(toWebSocketMessage(BOOK_SUBSCRIPTION))).consumeNextWith((message) -> assertMessageType(message, GraphQlMessageType.NEXT)).then(() -> input.tryEmitNext(toWebSocketMessage(completeMessage))).verifyTimeout(Duration.ofMillis(500));
}
Also used : Sinks(reactor.core.publisher.Sinks) GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) CloseStatus(org.springframework.web.reactive.socket.CloseStatus) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GraphQlSetup(org.springframework.graphql.GraphQlSetup) ArrayList(java.util.ArrayList) Assertions.as(org.assertj.core.api.Assertions.as) Duration(java.time.Duration) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) WebInterceptor(org.springframework.graphql.web.WebInterceptor) ResolvableType(org.springframework.core.ResolvableType) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) ConsumeOneAndNeverCompleteInterceptor(org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor) Mono(reactor.core.publisher.Mono) WebSocketHandlerTestSupport(org.springframework.graphql.web.WebSocketHandlerTestSupport) DataBuffer(org.springframework.core.io.buffer.DataBuffer) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) GraphQlMessageType(org.springframework.graphql.web.support.GraphQlMessageType) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) Collections(java.util.Collections) WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) Sinks(reactor.core.publisher.Sinks) ConsumeOneAndNeverCompleteInterceptor(org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) Test(org.junit.jupiter.api.Test)

Example 2 with ConsumeOneAndNeverCompleteInterceptor

use of org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor in project spring-graphql by spring-projects.

the class GraphQlWebSocketHandlerTests method clientCompletion.

@Test
void clientCompletion() throws Exception {
    GraphQlWebSocketHandler handler = initWebSocketHandler(new ConsumeOneAndNeverCompleteInterceptor());
    handle(handler, new TextMessage("{\"type\":\"connection_init\"}"), new TextMessage(BOOK_SUBSCRIPTION));
    String completeMessage = "{\"id\":\"" + SUBSCRIPTION_ID + "\",\"type\":\"complete\"}";
    Consumer<String> messageSender = (body) -> {
        try {
            handler.handleTextMessage(this.session, new TextMessage(body));
        } catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
    };
    StepVerifier.create(this.session.getOutput()).consumeNextWith((message) -> assertMessageType(message, GraphQlMessageType.CONNECTION_ACK)).consumeNextWith((message) -> assertMessageType(message, GraphQlMessageType.NEXT)).then(() -> messageSender.accept(completeMessage)).as("Second subscription with same id is possible only if the first was properly removed").then(() -> messageSender.accept(BOOK_SUBSCRIPTION)).consumeNextWith((message) -> assertMessageType(message, GraphQlMessageType.NEXT)).then(() -> messageSender.accept(completeMessage)).verifyTimeout(Duration.ofMillis(500));
}
Also used : GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) StepVerifier(reactor.test.StepVerifier) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GraphQlSetup(org.springframework.graphql.GraphQlSetup) CloseStatus(org.springframework.web.socket.CloseStatus) ArrayList(java.util.ArrayList) TextMessage(org.springframework.web.socket.TextMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) Assertions.as(org.assertj.core.api.Assertions.as) Duration(java.time.Duration) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) WebInterceptor(org.springframework.graphql.web.WebInterceptor) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) HttpHeaders(org.springframework.http.HttpHeaders) ConsumeOneAndNeverCompleteInterceptor(org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) WebSocketHandlerTestSupport(org.springframework.graphql.web.WebSocketHandlerTestSupport) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) GraphQlMessageType(org.springframework.graphql.web.support.GraphQlMessageType) HttpInputMessage(org.springframework.http.HttpInputMessage) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Collections(java.util.Collections) WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) InputStream(java.io.InputStream) GenericHttpMessageConverter(org.springframework.http.converter.GenericHttpMessageConverter) ConsumeOneAndNeverCompleteInterceptor(org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor) TextMessage(org.springframework.web.socket.TextMessage) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 3 with ConsumeOneAndNeverCompleteInterceptor

use of org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor in project spring-graphql by spring-projects.

the class GraphQlWebSocketHandlerTests method subscriptionExists.

@Test
void subscriptionExists() {
    Flux<WebSocketMessage> messageFlux = Flux.just(toWebSocketMessage("{\"type\":\"connection_init\"}"), toWebSocketMessage(BOOK_SUBSCRIPTION), toWebSocketMessage(BOOK_SUBSCRIPTION));
    TestWebSocketSession session = handle(messageFlux, new ConsumeOneAndNeverCompleteInterceptor());
    // Collect messages until session closed
    List<GraphQlMessage> messages = new ArrayList<>();
    session.getOutput().subscribe((message) -> messages.add(decode(message)));
    StepVerifier.create(session.closeStatus()).expectNext(new CloseStatus(4409, "Subscriber for " + SUBSCRIPTION_ID + " already exists")).expectComplete().verify(TIMEOUT);
    assertThat(messages.size()).isEqualTo(2);
    assertThat(messages.get(0).resolvedType()).isEqualTo(GraphQlMessageType.CONNECTION_ACK);
    assertThat(messages.get(1).resolvedType()).isEqualTo(GraphQlMessageType.NEXT);
}
Also used : ConsumeOneAndNeverCompleteInterceptor(org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor) GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) ArrayList(java.util.ArrayList) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) CloseStatus(org.springframework.web.reactive.socket.CloseStatus) Test(org.junit.jupiter.api.Test)

Example 4 with ConsumeOneAndNeverCompleteInterceptor

use of org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor in project spring-graphql by spring-projects.

the class GraphQlWebSocketHandlerTests method subscriptionExists.

@Test
void subscriptionExists() throws Exception {
    handle(initWebSocketHandler(new ConsumeOneAndNeverCompleteInterceptor()), new TextMessage("{\"type\":\"connection_init\"}"), new TextMessage(BOOK_SUBSCRIPTION), new TextMessage(BOOK_SUBSCRIPTION));
    // Collect messages until session closed
    List<GraphQlMessage> messages = new ArrayList<>();
    this.session.getOutput().subscribe((message) -> messages.add(decode(message)));
    StepVerifier.create(this.session.closeStatus()).expectNext(new CloseStatus(4409, "Subscriber for " + SUBSCRIPTION_ID + " already exists")).expectComplete().verify(TIMEOUT);
    assertThat(messages.size()).isEqualTo(2);
    assertThat(messages.get(0).resolvedType()).isEqualTo(GraphQlMessageType.CONNECTION_ACK);
    assertThat(messages.get(1).resolvedType()).isEqualTo(GraphQlMessageType.NEXT);
}
Also used : ConsumeOneAndNeverCompleteInterceptor(org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor) GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) ArrayList(java.util.ArrayList) CloseStatus(org.springframework.web.socket.CloseStatus) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Aggregations

ArrayList (java.util.ArrayList)4 Test (org.junit.jupiter.api.Test)4 ConsumeOneAndNeverCompleteInterceptor (org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor)4 GraphQlMessage (org.springframework.graphql.web.support.GraphQlMessage)4 Duration (java.time.Duration)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 BiConsumer (java.util.function.BiConsumer)2 Assertions.as (org.assertj.core.api.Assertions.as)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 InstanceOfAssertFactories (org.assertj.core.api.InstanceOfAssertFactories)2 GraphQlSetup (org.springframework.graphql.GraphQlSetup)2 WebGraphQlHandler (org.springframework.graphql.web.WebGraphQlHandler)2 WebInterceptor (org.springframework.graphql.web.WebInterceptor)2 WebSocketHandlerTestSupport (org.springframework.graphql.web.WebSocketHandlerTestSupport)2 WebSocketInterceptor (org.springframework.graphql.web.WebSocketInterceptor)2 GraphQlMessageType (org.springframework.graphql.web.support.GraphQlMessageType)2 CloseStatus (org.springframework.web.reactive.socket.CloseStatus)2