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));
}
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));
}
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);
}
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);
}
Aggregations