JMSCore { public boolean exitIfConnectionFails(ExtensionWebSocketClient client, int timeout) { boolean sourcesSucceeded = false; try { sourcesSucceeded = client.getSourceConnectionFuture().get(timeout, TimeUnit.SECONDS); } catch (TimeoutException e) { log.error("Timeout: full connection did not succeed within {} seconds: {}", timeout, e); } catch (Exception e) { log.error("Exception occurred while waiting for webSocket connection", e); } if (!sourcesSucceeded) { log.error("Failed to connect to all sources."); if (!client.isOpen()) { log.error("Failed to connect to server url '" + targetVantiqServer + "'."); } else if (!client.isAuthed()) { log.error("Failed to authenticate within " + timeout + " seconds using the given authentication data."); } else { log.error("Failed to connect within 10 seconds"); } return false; } return true; } JMSCore(String sourceName, String authToken, String targetVantiqServer); String getSourceName(); boolean start(int timeout); void sendJMSMessage(ExtensionServiceMessage message); void readQueueMessage(ExtensionServiceMessage message); void close(); void stop(); boolean exitIfConnectionFails(ExtensionWebSocketClient client, int timeout); final Handler<ExtensionServiceMessage> reconnectHandler; final Handler<ExtensionWebSocketClient> closeHandler; }
@Test public void testExitIfConnectionFails() { core.start(3); assertTrue("Should have succeeded", core.exitIfConnectionFails(core.client, 3)); assertFalse("Success means it shouldn't be closed", core.isClosed()); core.close(); core = new NoSendJMSCore(sourceName, authToken, targetVantiqServer); FalseClient fc = new FalseClient(sourceName); core.client = core.fClient = fc; fc.initiateFullConnection(targetVantiqServer, authToken); fc.completeWebSocketConnection(true); fc.completeAuthentication(false); assertFalse("Should fail due to authentication failing", core.exitIfConnectionFails(core.client, 3)); assertFalse("Failure does not mean it should be closed", core.isClosed()); core.close(); core = new NoSendJMSCore(sourceName, authToken, targetVantiqServer); fc = new FalseClient(sourceName); core.client = core.fClient = fc; fc.initiateFullConnection(targetVantiqServer, authToken); fc.completeWebSocketConnection(false); assertFalse("Should fail due to WebSocket failing", core.exitIfConnectionFails(core.client, 3)); assertFalse("Failure does not mean it should be closed", core.isClosed()); core.close(); core = new NoSendJMSCore(sourceName, authToken, targetVantiqServer); fc = new FalseClient(sourceName); core.client = core.fClient = fc; fc.initiateFullConnection(targetVantiqServer, authToken); fc.completeWebSocketConnection(true); fc.completeAuthentication(true); assertFalse("Should fail due to timeout on source connection", core.exitIfConnectionFails(core.client, 3)); assertFalse("Failure does not mean it should be closed", core.isClosed()); }