/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.lettuce.core.cluster;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import io.lettuce.core.RedisURI;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.metrics.DefaultCommandLatencyCollectorOptions;
import io.lettuce.core.resource.DefaultClientResources;
import io.lettuce.core.resource.EventLoopGroupProvider;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.EventExecutorGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.ImmediateEventExecutor;
import io.netty.util.concurrent.SucceededFuture;

/**
 * @author Mark Paluch
 */
public class SingleThreadedReactiveClusterClientTest {

    RedisClusterClient client;

    @Before
    public void before() {

        NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(1);
        DefaultClientResources clientResources = DefaultClientResources.builder()
                .eventExecutorGroup(ImmediateEventExecutor.INSTANCE).eventLoopGroupProvider(new EventLoopGroupProvider() {
                    @Override
                    public <T extends EventLoopGroup> T allocate(Class<T> type) {
                        return (T) nioEventLoopGroup;
                    }

                    @Override
                    public int threadPoolSize() {
                        return 0;
                    }

                    @Override
                    public Future<Boolean> release(EventExecutorGroup eventLoopGroup, long quietPeriod, long timeout,
                            TimeUnit unit) {
                        return new SucceededFuture<>(ImmediateEventExecutor.INSTANCE, true);
                    }

                    @Override
                    public Future<Boolean> shutdown(long quietPeriod, long timeout, TimeUnit timeUnit) {
                        nioEventLoopGroup.shutdownGracefully(quietPeriod, timeout, timeUnit);
                        return new SucceededFuture<>(ImmediateEventExecutor.INSTANCE, true);
                    }

                }).commandLatencyCollectorOptions(DefaultCommandLatencyCollectorOptions.disabled()).build();

        client = RedisClusterClient.create(clientResources, RedisURI.create("localhost", 7379));
    }

    @After
    public void tearDown() {

        client.shutdown();
        client.getResources().shutdown();
    }

    @Test
    public void shouldPropagateAsynchronousConnections() {

        StatefulRedisClusterConnection<String, String> connect = client.connect();
        connect.sync().flushall();

        List<String> keys = connect.reactive().set("key", "value").flatMap(s -> connect.reactive().set("foo", "bar"))
                .flatMapMany(s -> connect.reactive().keys("*")) //
                .doOnError(Throwable::printStackTrace) //
                .collectList() //
                .block();

        assertThat(keys).contains("key", "foo");
    }
}

@@ -762,16 +762,68 @@
 imeUnit;
+%0Aimport java.util.concurrent.atomic.AtomicReference;
 %0A%0Aimport
@@ -1203,32 +1203,83 @@
 EventLoopGroup;%0A
+import io.netty.channel.epoll.EpollEventLoopGroup;%0A
 import io.netty.
@@ -1678,29 +1678,24 @@
     
-NioEventLoopGroup nio
+AtomicReference%3C
 Even
@@ -1708,34 +1708,38 @@
 roup
+%3E ref
  = new 
-NioEventLoopGroup(1
+AtomicReference%3C%3E(
 );%0A 
@@ -2050,24 +2050,25 @@
 %3CT%3E type) %7B%0A
+%0A
             
@@ -2083,36 +2083,377 @@
     
-return (T) nioEventLoopGroup
+if (ref.get() == null) %7B%0A%0A                            if (type == EpollEventLoopGroup.class) %7B%0A                                ref.set(new EpollEventLoopGroup(1));%0A                            %7D else %7B%0A                                ref.set(new NioEventLoopGroup(1));%0A                            %7D%0A                        %7D%0A%0A                        return (T) ref.get()
 ;%0A  
@@ -3082,25 +3082,17 @@
     
-nioEventLoopGroup
+ref.get()
 .shu

Use appropriate EventLoopGroup in SingleThreadedReactiveClusterClientTest #486