@Override
  public void afterChannelInitialized(final Channel channel) {

    if (eventBus == null) {
      throw new IllegalStateException("Event bus must be set before channel customization can occur");
    }

    final ChannelCircuitBreakerHandler channelCircuitBreakerHandler = new ChannelCircuitBreakerHandler(clusterName,
        circuitBreakerConfig, upstreamAddresses, eventBus, scheduler);

    final String commandHandlerName = StreamSupport.stream(channel.pipeline().spliterator(), false)
        .filter(entry -> entry.getValue() instanceof CommandHandler)
        .map(Map.Entry::getKey)
        .findFirst()
        .orElseThrow();
    channel.pipeline().addBefore(commandHandlerName, null, channelCircuitBreakerHandler);
  }
@Test
  void testAfterChannelInitialized() {

    final LettuceShardCircuitBreaker lettuceShardCircuitBreaker = new LettuceShardCircuitBreaker("test",
        new CircuitBreakerConfiguration().toCircuitBreakerConfig(), Schedulers.immediate());
    lettuceShardCircuitBreaker.setEventBus(eventBus);

    final Channel channel = new EmbeddedChannel(
        new CommandHandler(ClientOptions.create(), ClientResources.create(), mock(Endpoint.class)));

    lettuceShardCircuitBreaker.afterChannelInitialized(channel);

    final AtomicBoolean foundCommandHandler = new AtomicBoolean(false);
    final AtomicBoolean foundChannelCircuitBreakerHandler = new AtomicBoolean(false);
    StreamSupport.stream(channel.pipeline().spliterator(), false)
        .forEach(nameAndHandler -> {
          if (nameAndHandler.getValue() instanceof CommandHandler) {
            foundCommandHandler.set(true);
          }
          if (nameAndHandler.getValue() instanceof LettuceShardCircuitBreaker.ChannelCircuitBreakerHandler) {
            foundChannelCircuitBreakerHandler.set(true);
          }
          if (foundCommandHandler.get()) {
            assertTrue(foundChannelCircuitBreakerHandler.get(),
                "circuit breaker handler should be before the command handler");
          }
        });

    assertTrue(foundChannelCircuitBreakerHandler.get());
    assertTrue(foundCommandHandler.get());
  }