Skip to content

Instantly share code, notes, and snippets.

@ceharris
Last active December 16, 2015 22:39
Show Gist options
  • Save ceharris/5508524 to your computer and use it in GitHub Desktop.
Save ceharris/5508524 to your computer and use it in GitHub Desktop.
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 {
...
}
}
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");
}
}
@ceharris
Copy link
Author

ceharris commented May 3, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment