Source:
void singleUnnamedSession(MessagingEntityType entityType) { final int entityIndex = 0; final String messageId = "singleUnnamedSession"; final String sessionId = "singleUnnamedSession-" + Instant.now().toString(); final String contents = "Some-contents"; final int numberToSend = 5; final List<String> lockTokens = new ArrayList<>(); setSenderAndReceiver(entityType, entityIndex, TIMEOUT, builder -> builder.maxAutoLockRenewalDuration(Duration.ofMinutes(2))); final Disposable subscription = Flux.interval(Duration.ofMillis(500)) .take(numberToSend) .flatMap(index -> { final ServiceBusMessage message = getServiceBusMessage(contents, messageId) .setSessionId(sessionId); messagesPending.incrementAndGet(); return sender.send(message).thenReturn(index); }).subscribe( number -> logger.info("sessionId[{}] sent[{}] Message sent.", sessionId, number), error -> logger.error("sessionId[{}] Error encountered.", sessionId, error), () -> logger.info("sessionId[{}] Finished sending.", sessionId)); try { StepVerifier.create(receiver.receive()) .assertNext(context -> assertMessageEquals(sessionId, messageId, contents, context)) .assertNext(context -> assertMessageEquals(sessionId, messageId, contents, context)) .assertNext(context -> assertMessageEquals(sessionId, messageId, contents, context)) .assertNext(context -> assertMessageEquals(sessionId, messageId, contents, context)) .assertNext(context -> assertMessageEquals(sessionId, messageId, contents, context)) .thenCancel() .verify(Duration.ofMinutes(2)); } finally { subscription.dispose(); completeMessages(receiver, lockTokens, sessionId); } }

Target:
void singleUnnamedSession(MessagingEntityType entityType) { final int entityIndex = TestUtils.USE_CASE_SINGLE_SESSION; final String messageId = "singleUnnamedSession"; final String sessionId = "singleUnnamedSession-" + Instant.now().toString(); final String contents = "Some-contents"; final int numberToSend = 5; final List<String> lockTokens = new ArrayList<>(); setSenderAndReceiver(entityType, entityIndex, TIMEOUT, builder -> builder.maxAutoLockRenewalDuration(Duration.ofMinutes(2))); final Disposable subscription = Flux.interval(Duration.ofMillis(500)) .take(numberToSend) .flatMap(index -> { final ServiceBusMessage message = getServiceBusMessage(contents, messageId) .setSessionId(sessionId); messagesPending.incrementAndGet(); return sender.send(message).thenReturn(index); }).subscribe( number -> logger.info("sessionId[{}] sent[{}] Message sent.", sessionId, number), error -> logger.error("sessionId[{}] Error encountered.", sessionId, error), () -> logger.info("sessionId[{}] Finished sending.", sessionId)); try { StepVerifier.create(receiver.receive()) .assertNext(context -> assertMessageEquals(sessionId, messageId, contents, context)) .assertNext(context -> assertMessageEquals(sessionId, messageId, contents, context)) .assertNext(context -> assertMessageEquals(sessionId, messageId, contents, context)) .assertNext(context -> assertMessageEquals(sessionId, messageId, contents, context)) .assertNext(context -> assertMessageEquals(sessionId, messageId, contents, context)) .thenCancel() .verify(Duration.ofMinutes(2)); } finally { subscription.dispose(); Mono.when(lockTokens.stream().map(e -> receiver.complete(MessageLockToken.fromString(e), sessionId)) .collect(Collectors.toList())) .block(TIMEOUT); } }