title: Spring WebFlux: Testing WebClient server send events
body: <p>Actually I am not even sure if it is possible to do what I want to: I am consuming an event stream (<code>Event.class</code>) from a Broker (MQTT) using Spring Integration and want to forward the stream to another microservice. Both services are supposed to be horizontally scalable. Thus, in the other service I have a POST endpoint that excepts a <code>Flux&lt;Event&gt;</code> as body (using Spring Cloud Functions: <code>spring-cloud-starter-function-webflux</code>). In the forwarding service I want to use Ribbon and <code>WebClient</code>.</p>
  
  <p>What I have so far (simplified):</p>
  
  <pre><code>@Configuration
  public class EventFlowConfiguration{ 
  
  @Autowired
  private Webclient webClient;
  
  @Bean
  public MessageProducerSupport inboundAdapter() {
      final MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(MqttAsyncClient.generateClientId(), this.mqttClientFactory, "/topic");
      adapter.setConverter(new DefaultPahoMessageConverter());
      return adapter;
  }
  
  @Bean
  Publisher&lt;Message&lt;Event&gt;&gt; eventFlow() {
      return IntegrationFlows
              .from(inboundAdapter())
              .handle((payload, header) -&gt; this.eventHandler((String) payload))
              .toReactivePublisher();
  }
  
  private Event eventHandler(String payload) {
      // parsing, store in database ...
      return event;
  }
  
  @PostConstruct
  public void setTrigger() {
      buildTrigger(webClient, "/receiveEventStream", eventFlow(), Event.class);
  }
  
  private &lt;T, E extends Event&gt; void buildTrigger(WebClient webClient, String uri, Publisher&lt;Message&lt;T&gt;&gt; publisher, Class&lt;E&gt; eventClass) {
      webClient
              .post()
              .uri(uri)
              .contentType(MediaType.APPLICATION_STREAM_JSON)
              .body(
                      Flux.from(publisher)
                              .retry()
                              .map(Message::getPayload)
                      ,
                      eventClass
              )
              .retrieve()
              .bodyToMono(Void.class)
              .retry()
              .subscribe();
  }
  }
  </code></pre>
  
  <p>My questions:</p>
  
  <p>(1) Is their any chance to test this in general? How would I do that? I that about OkHttp, WireMock, an internal spring boot application in the test, that has the <code>/eventReceiverEndpoint</code> but just does asserts. However, didn't really get anywhere. Actually atm I don't even get an error. I would have expected a 4xx since no in memeory server is around at start-up.</p>
  
  <p>(2) Do the two <code>retry()</code> calls really retry connection issues on both sides? Do I loose messages in case of disconnections? How can I test that?</p>
  
  <p>(3) Is Ribbon in combination with this long living Flux still able to do load balancing, after the connection was established (like when the load of the final consumer gets to high or so). I suppose it would have to automatically reconnect the Flux to another instance?</p>