Code cleanups.

This commit is contained in:
Simone Bordet 2016-03-08 11:32:17 +01:00
parent 07d54224c7
commit 18b689b820

View File

@ -28,7 +28,6 @@ import java.net.Socket;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future; import java.util.concurrent.Future;
@ -49,16 +48,16 @@ public abstract class SslBytesTest
public static class TLSRecord public static class TLSRecord
{ {
private final SslBytesServerTest.TLSRecord.Type type; private final Type type;
private final byte[] bytes; private final byte[] bytes;
public TLSRecord(SslBytesServerTest.TLSRecord.Type type, byte[] bytes) public TLSRecord(Type type, byte[] bytes)
{ {
this.type = type; this.type = type;
this.bytes = bytes; this.bytes = bytes;
} }
public SslBytesServerTest.TLSRecord.Type getType() public Type getType()
{ {
return type; return type;
} }
@ -80,15 +79,15 @@ public abstract class SslBytesTest
private int code; private int code;
private Type(int code) Type(int code)
{ {
this.code = code; this.code = code;
SslBytesServerTest.TLSRecord.Type.Mapper.codes.put(this.code, this); Mapper.codes.put(this.code, this);
} }
public static SslBytesServerTest.TLSRecord.Type from(int code) public static Type from(int code)
{ {
SslBytesServerTest.TLSRecord.Type result = SslBytesServerTest.TLSRecord.Type.Mapper.codes.get(code); Type result = Mapper.codes.get(code);
if (result == null) if (result == null)
throw new IllegalArgumentException("Invalid TLSRecord.Type " + code); throw new IllegalArgumentException("Invalid TLSRecord.Type " + code);
return result; return result;
@ -96,7 +95,7 @@ public abstract class SslBytesTest
private static class Mapper private static class Mapper
{ {
private static final Map<Integer, SslBytesServerTest.TLSRecord.Type> codes = new HashMap<>(); private static final Map<Integer, Type> codes = new HashMap<>();
} }
} }
} }
@ -218,7 +217,7 @@ public abstract class SslBytesTest
} }
} }
private TLSRecord read(SslBytesServerTest.TLSRecord.Type type, InputStream input, byte[] bytes, int offset, int length) throws IOException private TLSRecord read(TLSRecord.Type type, InputStream input, byte[] bytes, int offset, int length) throws IOException
{ {
while (length > 0) while (length > 0)
{ {
@ -291,57 +290,51 @@ public abstract class SslBytesTest
} }
} }
public SslBytesServerTest.SimpleProxy.AutomaticFlow startAutomaticFlow() throws InterruptedException public SslBytesTest.SimpleProxy.AutomaticFlow startAutomaticFlow() throws InterruptedException
{ {
final CountDownLatch startLatch = new CountDownLatch(2); final CountDownLatch startLatch = new CountDownLatch(2);
final CountDownLatch stopLatch = new CountDownLatch(2); final CountDownLatch stopLatch = new CountDownLatch(2);
Future<Object> clientToServer = threadPool.submit(new Callable<Object>() Future<Object> clientToServer = threadPool.submit(() ->
{ {
public Object call() throws Exception startLatch.countDown();
logger.debug("Automatic flow C --> S started");
try
{ {
startLatch.countDown(); while (true)
logger.debug("Automatic flow C --> S started");
try
{ {
while (true) flushToServer(readFromClient(), 0);
{
flushToServer(readFromClient(), 0);
}
}
catch (InterruptedIOException x)
{
return null;
}
finally
{
stopLatch.countDown();
logger.debug("Automatic flow C --> S finished");
} }
} }
}); catch (InterruptedIOException x)
Future<Object> serverToClient = threadPool.submit(new Callable<Object>()
{
public Object call() throws Exception
{ {
startLatch.countDown(); return null;
logger.debug("Automatic flow C <-- S started"); }
try finally
{
stopLatch.countDown();
logger.debug("Automatic flow C --> S finished");
}
});
Future<Object> serverToClient = threadPool.submit(() ->
{
startLatch.countDown();
logger.debug("Automatic flow C <-- S started");
try
{
while (true)
{ {
while (true) flushToClient(readFromServer());
{
flushToClient(readFromServer());
}
}
catch (InterruptedIOException x)
{
return null;
}
finally
{
stopLatch.countDown();
logger.debug("Automatic flow C <-- S finished");
} }
} }
catch (InterruptedIOException x)
{
return null;
}
finally
{
stopLatch.countDown();
logger.debug("Automatic flow C <-- S finished");
}
}); });
Assert.assertTrue(startLatch.await(5, TimeUnit.SECONDS)); Assert.assertTrue(startLatch.await(5, TimeUnit.SECONDS));
return new SslBytesServerTest.SimpleProxy.AutomaticFlow(stopLatch, clientToServer, serverToClient); return new SslBytesServerTest.SimpleProxy.AutomaticFlow(stopLatch, clientToServer, serverToClient);