Last active
December 16, 2015 22:39
-
-
Save ceharris/5508524 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AbstractSocketAppender extends ... | |
implements EventDispatcher, ExceptionHandler { | |
private SocketConnectorFactory connectorFactory; | |
private ConnectionRunner connectionRunner; | |
private Future<?> runnerTask; | |
public void start() { | |
... | |
// extract method to create connector factory... | |
connectorFactory = new DefaultSocketConnectorFactory(); | |
connectorFactory.setInetAddress(address); | |
connectorFactory.setPort(port); | |
connectorFactory.setDelayStrategy(...); | |
connectorFactory.setExceptionHandler(this); | |
// extract method to create connection runner... | |
connectionRunner = new ConnectionRunner(connectorFactroy, this); | |
connectionRunner.setContext(getContext()); | |
runnerTask = getContext().getExecutorService().submit(connectionRunner); | |
... | |
} | |
public void dispatchEvents(Socket socket) throws InterruptedException { | |
... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ConnectionRunner extends ContextAwareBase implements Runnable { | |
private final SocketConnectorFactory connectorFactory; | |
private final EventDispatcher eventDispatcher; | |
public final void run() { | |
try { | |
while (!Thread.currentThread().isInterrupted()) { | |
SocketConnector connector = connectorFactory.createConnector(); | |
Future<Socket> socketFuture = getContext().getExecutorService().submit(connector); | |
Socket socket = socketFuture.get(); | |
eventDispatcher.dispatchEvents(socket); | |
} | |
} catch (InterruptedException ex) { | |
assert true; // ok... we'll exit now | |
} catch (ExecutionException ex) { | |
addError("connector error: " + ex); | |
} | |
addInfo("shutting down"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One of the deficiencies in the existing tests of AbstractSocketAppender and SocketReceiver tests is the design prevents path testing in the run method. The tests use socket I/O to validate that events are being dispatched properly, but that's more of an integration test than a unit test, and is more fragile across platforms.