/*
 * Copyright 2011-2016 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.server;

import java.util.concurrent.TimeUnit;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * Tiny netty server to generate random base64 data on message reception.
 *
 * @author Mark Paluch
 */
public class RandomResponseServer {

    private EventLoopGroup bossGroup;
    private EventLoopGroup workerGroup;
    private Channel channel;

    public void initialize(int port) throws InterruptedException {

        bossGroup = new NioEventLoopGroup(1);
        workerGroup = new NioEventLoopGroup();

        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        // p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new RandomServerHandler());
                    }
                });

        // Start the server.
        ChannelFuture f = b.bind(port).sync();

        channel = f.channel();
    }

    public void shutdown() {
        channel.close();
        bossGroup.shutdownGracefully(100, 100, TimeUnit.MILLISECONDS);
        workerGroup.shutdownGracefully(100, 100, TimeUnit.MILLISECONDS);
    }
}

@@ -1254,32 +1254,27 @@
 p = 
-new NioEventLoop
+Resources.boss
 Group
-(1)
 ;%0A  
@@ -1297,31 +1297,29 @@
 p = 
-new NioEventLoop
+Resources.worker
 Group
-()
 ;%0A%0A 
@@ -2069,24 +2069,377 @@
 el.close();%0A
+    %7D%0A%0A    private static class Resources %7B%0A%0A        private static final EventLoopGroup bossGroup;%0A        private static final EventLoopGroup workerGroup;%0A%0A        static %7B%0A            bossGroup = new NioEventLoopGroup(1);%0A            workerGroup = new NioEventLoopGroup();%0A%0A            Runtime.getRuntime().addShutdownHook(new Thread(() -%3E %7B%0A        
         boss
@@ -2455,39 +2455,35 @@
 tdownGracefully(
-10
 0, 
-10
 0, TimeUnit.MILL
@@ -2489,24 +2489,32 @@
 LISECONDS);%0A
+        
         work
@@ -2544,15 +2544,11 @@
 lly(
-10
 0, 
-10
 0, T
@@ -2566,20 +2566,82 @@
 LISECONDS);%0A
+%0A            %7D, %22RandomResponseServer-shutdown%22));%0A        %7D%0A%0A
     %7D%0A%7D%0A

Improve test server resource handling