diff --git a/quickfixj-core/src/test/java/quickfix/SessionStateTest.java b/quickfixj-core/src/test/java/quickfix/SessionStateTest.java index dc0a5e8440..a19e48f949 100644 --- a/quickfixj-core/src/test/java/quickfix/SessionStateTest.java +++ b/quickfixj-core/src/test/java/quickfix/SessionStateTest.java @@ -70,6 +70,23 @@ public void testTestRequestTiming() { assertFalse("testRequest shouldn't be needed yet", state.isTestRequestNeeded()); } + @Test + public void testHeartbeatTiming() { + // we set a HB interval of 2 seconds = 2000ms + SessionState state = new SessionState(new Object(), null, 2 /* HB interval */, false, null, + Session.DEFAULT_TEST_REQUEST_DELAY_MULTIPLIER, Session.DEFAULT_HEARTBEAT_TIMEOUT_MULTIPLIER); + + long now = System.currentTimeMillis(); + timeSource.setSystemTimes(now); + state.setLastSentTime(now); + assertFalse("heartbeat shouldn't be needed yet", state.isHeartBeatNeeded()); + timeSource.increment(1000); + assertFalse("heartbeat shouldn't be needed yet", state.isHeartBeatNeeded()); + timeSource.increment(1000); + // current time is now 2000ms further since the start, i.e. the HB interval has elapsed + assertTrue("heartbeat should be needed", state.isHeartBeatNeeded()); + } + @Test public void testSessionTimeout() { SessionState state = new SessionState(new Object(), null, 30, false, null, diff --git a/quickfixj-core/src/test/java/quickfix/test/acceptance/timer/TimerTest.java b/quickfixj-core/src/test/java/quickfix/test/acceptance/timer/TimerTest.java deleted file mode 100644 index 5d6600d192..0000000000 --- a/quickfixj-core/src/test/java/quickfix/test/acceptance/timer/TimerTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/******************************************************************************* - * Copyright (c) quickfixengine.org All rights reserved. - * - * This file is part of the QuickFIX FIX Engine - * - * This file may be distributed under the terms of the quickfixengine.org - * license as defined by quickfixengine.org and appearing in the file - * LICENSE included in the packaging of this file. - * - * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING - * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE. - * - * See http://www.quickfixengine.org/LICENSE for licensing information. - * - * Contact ask@quickfixengine.org if any conditions of this licensing - * are not clear to you. - ******************************************************************************/ - -package quickfix.test.acceptance.timer; - -import junit.framework.TestCase; -import quickfix.ConfigError; -import quickfix.SessionNotFound; - -public class TimerTest extends TestCase { - - TimerTestServer server; - - public void testAcceptorTimer() throws ConfigError, SessionNotFound, InterruptedException { - new TimerTestClient().run(); - } - - protected void setUp() throws Exception { - server = new TimerTestServer(); - server.start(); - server.waitForInitialization(); - } - - protected void tearDown() throws Exception { - server.stop(); - } -} diff --git a/quickfixj-core/src/test/java/quickfix/test/acceptance/timer/TimerTestClient.java b/quickfixj-core/src/test/java/quickfix/test/acceptance/timer/TimerTestClient.java deleted file mode 100644 index 55765bcd83..0000000000 --- a/quickfixj-core/src/test/java/quickfix/test/acceptance/timer/TimerTestClient.java +++ /dev/null @@ -1,150 +0,0 @@ -/******************************************************************************* - * Copyright (c) quickfixengine.org All rights reserved. - * - * This file is part of the QuickFIX FIX Engine - * - * This file may be distributed under the terms of the quickfixengine.org - * license as defined by quickfixengine.org and appearing in the file - * LICENSE included in the packaging of this file. - * - * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING - * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE. - * - * See http://www.quickfixengine.org/LICENSE for licensing information. - * - * Contact ask@quickfixengine.org if any conditions of this licensing - * are not clear to you. - ******************************************************************************/ - -package quickfix.test.acceptance.timer; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import quickfix.Application; -import quickfix.ConfigError; -import quickfix.DefaultMessageFactory; -import quickfix.DoNotSend; -import quickfix.FieldNotFound; -import quickfix.FixVersions; -import quickfix.IncorrectDataFormat; -import quickfix.IncorrectTagValue; -import quickfix.Initiator; -import quickfix.MemoryStoreFactory; -import quickfix.Message; -import quickfix.MessageCracker; -import quickfix.MessageStoreFactory; -import quickfix.RejectLogon; -import quickfix.RuntimeError; -import quickfix.SLF4JLogFactory; -import quickfix.SessionID; -import quickfix.SessionNotFound; -import quickfix.SessionSettings; -import quickfix.SocketInitiator; -import quickfix.UnsupportedMessageType; -import quickfix.fix44.ListStatusRequest; -import quickfix.fix44.TestRequest; - -import java.util.HashMap; -import java.util.Timer; -import java.util.TimerTask; -import java.util.concurrent.CountDownLatch; - -/** - * @author John Hensley - */ -public class TimerTestClient extends MessageCracker implements Application { - private final Logger log = LoggerFactory.getLogger(TimerTestServer.class); - private final SessionSettings settings = new SessionSettings(); - private final CountDownLatch shutdownLatch = new CountDownLatch(1); - private boolean failed; - - public void fromAdmin(Message message, SessionID sessionId) throws FieldNotFound, - IncorrectDataFormat, IncorrectTagValue, RejectLogon { - } - - public void fromApp(Message message, SessionID sessionID) throws FieldNotFound, - IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType { - crack(message, sessionID); - } - - public void onCreate(SessionID sessionId) { - } - - public void onLogon(SessionID sessionId) { - } - - public void onLogout(SessionID sessionId) { - } - - public void onMessage(ListStatusRequest message, SessionID sessionID) { - log.info("got ListStatusRequest"); - } - - private void stop(boolean failed) { - this.failed = failed; - shutdownLatch.countDown(); - } - - public void run() throws ConfigError, SessionNotFound, InterruptedException { - HashMap defaults = new HashMap<>(); - defaults.put("ConnectionType", "initiator"); - defaults.put("HeartBtInt", "2"); - defaults.put("SocketConnectHost", "localhost"); - defaults.put("SocketConnectPort", "19888"); - defaults.put("SocketTcpNoDelay", "Y"); - defaults.put("StartTime", "00:00:00"); - defaults.put("EndTime", "00:00:00"); - defaults.put("SenderCompID", "TW"); - defaults.put("TargetCompID", "ISLD"); - defaults.put("FileStorePath", "target/data/timer_test"); - defaults.put("ValidateUserDefinedFields", "Y"); - settings.set(defaults); - - SessionID sessionID = new SessionID(FixVersions.BEGINSTRING_FIX44, "TW", "ISLD"); - settings.setString(sessionID, "BeginString", FixVersions.BEGINSTRING_FIX44); - settings.setString(sessionID, "DataDictionary", "FIX44.xml"); - - MessageStoreFactory storeFactory = new MemoryStoreFactory(); - Initiator initiator = new SocketInitiator(this, storeFactory, settings, - new SLF4JLogFactory(settings), new DefaultMessageFactory()); - initiator.start(); - - try { - Timer timer = new Timer(); - timer.schedule(new TimerTask() { - public void run() { - stop(false); - } - }, 5000); - - try { - shutdownLatch.await(); - } catch (InterruptedException e) { - } - - if (failed) { - String message = "TimerTestClient had to send a test request, indicating that the test server was not reliably sending heartbeats."; - log.error(message); - throw new RuntimeError(message); - } - } finally { - initiator.stop(); - } - } - - public void toAdmin(Message message, SessionID sessionId) { - if (message instanceof TestRequest) { - stop(true); - } - } - - public void toApp(Message message, SessionID sessionId) throws DoNotSend { - } - - public static void main(String[] args) throws ConfigError, SessionNotFound, - InterruptedException { - TimerTestClient ttc = new TimerTestClient(); - ttc.run(); - } -} diff --git a/quickfixj-core/src/test/java/quickfix/test/acceptance/timer/TimerTestServer.java b/quickfixj-core/src/test/java/quickfix/test/acceptance/timer/TimerTestServer.java deleted file mode 100644 index f6ca060b9b..0000000000 --- a/quickfixj-core/src/test/java/quickfix/test/acceptance/timer/TimerTestServer.java +++ /dev/null @@ -1,169 +0,0 @@ -/******************************************************************************* - * Copyright (c) quickfixengine.org All rights reserved. - * - * This file is part of the QuickFIX FIX Engine - * - * This file may be distributed under the terms of the quickfixengine.org - * license as defined by quickfixengine.org and appearing in the file - * LICENSE included in the packaging of this file. - * - * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING - * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE. - * - * See http://www.quickfixengine.org/LICENSE for licensing information. - * - * Contact ask@quickfixengine.org if any conditions of this licensing - * are not clear to you. - ******************************************************************************/ - -package quickfix.test.acceptance.timer; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import quickfix.Application; -import quickfix.DefaultMessageFactory; -import quickfix.DoNotSend; -import quickfix.FieldNotFound; -import quickfix.FixVersions; -import quickfix.IncorrectDataFormat; -import quickfix.IncorrectTagValue; -import quickfix.MemoryStoreFactory; -import quickfix.Message; -import quickfix.MessageCracker; -import quickfix.MessageStoreFactory; -import quickfix.RejectLogon; -import quickfix.SLF4JLogFactory; -import quickfix.Session; -import quickfix.SessionID; -import quickfix.SessionNotFound; -import quickfix.SessionSettings; -import quickfix.SocketAcceptor; -import quickfix.UnsupportedMessageType; -import quickfix.field.ListID; -import quickfix.fix44.ListStatusRequest; -import quickfix.fix44.Logon; - -import java.util.HashMap; -import java.util.Timer; -import java.util.TimerTask; -import java.util.concurrent.CountDownLatch; - -/** - * @author John Hensley - */ -public class TimerTestServer extends MessageCracker implements Application, Runnable { - SocketAcceptor acceptor; - private final Logger log = LoggerFactory.getLogger(TimerTestServer.class); - private final SessionSettings settings = new SessionSettings(); - private Thread serverThread; - private final CountDownLatch initializationLatch = new CountDownLatch(1); - private final CountDownLatch shutdownLatch = new CountDownLatch(1); - - private class DelayedTestRequest extends TimerTask { - final SessionID session; - - DelayedTestRequest(SessionID sessionID) { - this.session = sessionID; - } - - public void run() { - try { - log.info("Sending offset message"); - ListStatusRequest lsr = new ListStatusRequest(new ListID("somelist")); - Session.sendToTarget(lsr, this.session); - } catch (SessionNotFound sessionNotFound) { - // not going to happen - } - } - } - - public void fromAdmin(Message message, SessionID sessionId) throws FieldNotFound, - IncorrectDataFormat, IncorrectTagValue, RejectLogon { - // sleep to move our timer off from the client's - if (message instanceof Logon) { - new Timer().schedule(new DelayedTestRequest(sessionId), 3000); - } - } - - public void fromApp(Message message, SessionID sessionId) throws FieldNotFound, - IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType { - } - - public void onCreate(SessionID sessionId) { - } - - public void onLogon(SessionID sessionId) { - } - - public void onLogout(SessionID sessionId) { - log.info("logout"); - shutdownLatch.countDown(); - } - - public void start() { - serverThread = new Thread(this, "TimerTestServer"); - serverThread.setDaemon(true); - serverThread.start(); - } - - public void stop() { - shutdownLatch.countDown(); - } - - public void run() { - try { - HashMap defaults = new HashMap<>(); - defaults.put("ConnectionType", "acceptor"); - defaults.put("SocketAcceptPort", "19888" ); - defaults.put("StartTime", "00:00:00"); - defaults.put("EndTime", "00:00:00"); - defaults.put("SenderCompID", "ISLD"); - defaults.put("TargetCompID", "TW"); - defaults.put("FileStorePath", "target/data/server"); - defaults.put("ValidateUserDefinedFields", "Y"); - defaults.put("ResetOnDisconnect", "Y"); - settings.set(defaults); - - SessionID sessionID = new SessionID(FixVersions.BEGINSTRING_FIX44, "ISLD", "TW"); - settings.setString(sessionID, "BeginString", FixVersions.BEGINSTRING_FIX44); - settings.setString(sessionID, "DataDictionary", FixVersions.BEGINSTRING_FIX44.replaceAll("\\.", "") + ".xml"); - - MessageStoreFactory factory = new MemoryStoreFactory(); - acceptor = new SocketAcceptor(this, factory, settings, new SLF4JLogFactory(settings), - new DefaultMessageFactory()); - acceptor.start(); - try { - //acceptor.waitForInitialization(); - initializationLatch.countDown(); - - try { - shutdownLatch.await(); - } catch (InterruptedException e) { - } - - log.info("TimerTestServer shutting down."); - } finally { - acceptor.stop(); - } - } catch (Throwable e) { - log.error("Error in TimerTestServer server: ", e); - initializationLatch.countDown(); - } - } - - public void toAdmin(Message message, SessionID sessionId) { - } - - public void toApp(Message message, SessionID sessionId) throws DoNotSend { - } - - public void waitForInitialization() throws InterruptedException { - initializationLatch.await(); - } - - public static void main(String[] args) { - TimerTestServer server = new TimerTestServer(); - server.run(); - } -}