/******************************************************************************
 *                                                                            *
 *                    Copyright 2017 Subterranean Security                    *
 *                                                                            *
 *  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 com.sandpolis.core.net.loop;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LoggingHandler;

class ConnectionLoopTest {

	private EventLoopGroup group;

	@BeforeEach
	private void setup() {
		group = new NioEventLoopGroup();
	}

	@AfterEach
	private void cleanup() {
		group.shutdownGracefully();
	}

	@Test
	void testConnectionFailure() throws InterruptedException {
		ConnectionLoop loop = new ConnectionLoop("127.0.0.1", 6001, 100,
				new Bootstrap().channel(NioSocketChannel.class).group(group).handler(new LoggingHandler()));

		long t1 = System.currentTimeMillis();
		loop.start();
		loop.await();
		assertTrue(System.currentTimeMillis() - t1 >= 100, "Timeout not reached");

		assertNull(loop.getResult());
	}

	@Test
	void testSingletonSuccess() throws InterruptedException, IOException {

		new ServerBootstrap().group(group).channel(NioServerSocketChannel.class).childHandler(new LoggingHandler())
				.bind(6001);

		ConnectionLoop loop = new ConnectionLoop("127.0.0.1", 6001, 100,
				new Bootstrap().channel(NioSocketChannel.class).group(group).handler(new LoggingHandler()));

		long t1 = System.currentTimeMillis();
		loop.start();
		loop.await();
		assertTrue(System.currentTimeMillis() - t1 < 100, "Timeout exceeded");

		assertNotNull(loop.getResult());
	}
}

@@ -1516,177 +1516,37 @@
 ll;%0A
-import static org.junit.jupiter.api.Assertions.assertTrue;%0A%0Aimport java.io.IOException;%0A%0Aimport org.junit.jupiter.api.AfterEach;%0Aimport org.junit.jupiter.api.BeforeEach;
+%0Aimport java.io.IOException;%0A
 %0Aimp
@@ -1662,48 +1662,8 @@
 ap;%0A
-import io.netty.channel.EventLoopGroup;%0A
 impo
@@ -1898,188 +1898,8 @@
  %7B%0A%0A
-%09private EventLoopGroup group;%0A%0A%09@BeforeEach%0A%09private void setup() %7B%0A%09%09group = new NioEventLoopGroup();%0A%09%7D%0A%0A%09@AfterEach%0A%09private void cleanup() %7B%0A%09%09group.shutdownGracefully();%0A%09%7D%0A%0A
 %09@Te
@@ -2013,39 +2013,36 @@
 127.0.0.1%22, 
-6001
+38903
 , 100,
-%0A%09%09%09%09
+ 
 new Bootstra
@@ -2036,32 +2036,37 @@
  new Bootstrap()
+%0A%09%09%09%09
 .channel(NioSock
@@ -2080,37 +2080,55 @@
 el.class).group(
-g
+new NioEventLoopG
 roup
+()
 ).handler(new Lo
@@ -2154,150 +2154,33 @@
 %09%09lo
-ng t1 = System.currentTimeMillis();%0A%09%09loop.start();%0A%09%09loop.await();%0A%09%09assertTrue(System.currentTimeMillis() - t1 %3E= 100, %22Timeout not reached%22
+op.start();%0A%09%09loop.await(
 );%0A%0A
@@ -2325,21 +2325,39 @@
 ).group(
-g
+new NioEventLoopG
 roup
+()
 ).channe
@@ -2387,16 +2387,21 @@
 l.class)
+%0A%09%09%09%09
 .childHa
@@ -2431,23 +2431,19 @@
 r())
-%0A%09%09%09%09
 .bind(
-6001
+23374
 );%0A%0A
@@ -2502,23 +2502,20 @@
 1%22, 
-6001
+23374
 , 100,
-%0A%09%09%09%09
+ 
 new 
@@ -2525,16 +2525,21 @@
 tstrap()
+%0A%09%09%09%09
 .channel
@@ -2569,21 +2569,39 @@
 ).group(
-g
+new NioEventLoopG
 roup
+()
 ).handle
@@ -2635,146 +2635,33 @@
 %09%09lo
-ng t1 = System.currentTimeMillis();%0A%09%09loop.start();%0A%09%09loop.await();%0A%09%09assertTrue(System.currentTimeMillis() - t1 %3C 100, %22Timeout exceeded%22
+op.start();%0A%09%09loop.await(
 );%0A%0A

Remove unreliable timestamp check