|
| 1 | +package uk.co.real_logic.artio.system_tests; |
| 2 | + |
| 3 | +import org.agrona.ErrorHandler; |
| 4 | +import org.agrona.concurrent.EpochNanoClock; |
| 5 | +import org.junit.Test; |
| 6 | +import uk.co.real_logic.artio.dictionary.generation.Exceptions; |
| 7 | +import uk.co.real_logic.artio.engine.EngineConfiguration; |
| 8 | +import uk.co.real_logic.artio.engine.FixEngine; |
| 9 | +import uk.co.real_logic.artio.fields.EpochFractionFormat; |
| 10 | +import uk.co.real_logic.artio.library.LibraryConfiguration; |
| 11 | +import uk.co.real_logic.artio.protocol.GatewayPublication; |
| 12 | +import uk.co.real_logic.artio.session.DirectSessionProxy; |
| 13 | +import uk.co.real_logic.artio.session.SessionCustomisationStrategy; |
| 14 | +import uk.co.real_logic.artio.session.SessionIdStrategy; |
| 15 | +import uk.co.real_logic.artio.session.SessionProxy; |
| 16 | +import uk.co.real_logic.artio.util.DebugFIXClient; |
| 17 | +import uk.co.real_logic.artio.util.DebugServer; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | +import static org.junit.Assert.assertTrue; |
| 23 | +import static uk.co.real_logic.artio.TestFixtures.launchMediaDriver; |
| 24 | +import static uk.co.real_logic.artio.messages.InitialAcceptedSessionOwner.SOLE_LIBRARY; |
| 25 | +import static uk.co.real_logic.artio.system_tests.SystemTestUtil.ACCEPTOR_ID; |
| 26 | +import static uk.co.real_logic.artio.system_tests.SystemTestUtil.INITIATOR_ID; |
| 27 | +import static uk.co.real_logic.artio.system_tests.SystemTestUtil.acceptingConfig; |
| 28 | +import static uk.co.real_logic.artio.system_tests.SystemTestUtil.acceptingLibraryConfig; |
| 29 | +import static uk.co.real_logic.artio.system_tests.SystemTestUtil.connect; |
| 30 | +import static uk.co.real_logic.artio.system_tests.SystemTestUtil.initiatingConfig; |
| 31 | +import static uk.co.real_logic.artio.system_tests.SystemTestUtil.initiatingLibraryConfig; |
| 32 | + |
| 33 | +/** |
| 34 | + * Try reproducing race between sent ResendRequest and ResetSequence message when both |
| 35 | + * parties request a resend. Also checks that SessionProxy is invoked when a ResetSequence |
| 36 | + * message must be sent. |
| 37 | + */ |
| 38 | +public class RaceResendResetTest extends AbstractGatewayToGatewaySystemTest { |
| 39 | + |
| 40 | + private boolean sendResendRequestCalled; |
| 41 | + private boolean sendSequenceResetCalled; |
| 42 | + private boolean useProxy; |
| 43 | + |
| 44 | + private void launch() { |
| 45 | + mediaDriver = launchMediaDriver(); |
| 46 | + launchAccepting(); |
| 47 | + launchInitiating(); |
| 48 | + testSystem = new TestSystem(acceptingLibrary, initiatingLibrary); |
| 49 | + } |
| 50 | + |
| 51 | + private void launchInitiating() { |
| 52 | + final EngineConfiguration initiatingConfig = |
| 53 | + initiatingConfig(libraryAeronPort, nanoClock) |
| 54 | + .deleteLogFileDirOnStart(true) |
| 55 | + .initialAcceptedSessionOwner(SOLE_LIBRARY); |
| 56 | + initiatingEngine = FixEngine.launch(initiatingConfig); |
| 57 | + LibraryConfiguration lib = initiatingLibraryConfig(libraryAeronPort, initiatingHandler, nanoClock); |
| 58 | + if (useProxy) |
| 59 | + lib.sessionProxyFactory(this::sessionProxyFactory); |
| 60 | + initiatingLibrary = connect(lib |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + private void launchAccepting() { |
| 65 | + final EngineConfiguration acceptingConfig = acceptingConfig(port, ACCEPTOR_ID, INITIATOR_ID, nanoClock) |
| 66 | + .deleteLogFileDirOnStart(true) |
| 67 | + .initialAcceptedSessionOwner(SOLE_LIBRARY); |
| 68 | + acceptingEngine = FixEngine.launch(acceptingConfig); |
| 69 | + |
| 70 | + final LibraryConfiguration acceptingLibraryConfig = acceptingLibraryConfig(acceptingHandler, nanoClock); |
| 71 | + acceptingLibrary = connect(acceptingLibraryConfig); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Sanity check that we can connect Artio to a debug server with canned messages. |
| 76 | + */ |
| 77 | + @Test |
| 78 | + public void testDebugServer() throws IOException { |
| 79 | + DebugServer srv = new DebugServer(port); |
| 80 | + srv.setWaitForData(true); |
| 81 | + srv.addFIXResponse("8=FIX.4.4|9=94|35=A|49=acceptor|56=initiator|34=1|52=20240315-10:52:24.098|98=0|108=10|141=N|35002=0|35003=0|10=024|"); |
| 82 | + srv.start(); |
| 83 | + |
| 84 | + mediaDriver = launchMediaDriver(); |
| 85 | + launchInitiating(); |
| 86 | + testSystem = new TestSystem(initiatingLibrary); |
| 87 | + connectAndAcquire(); |
| 88 | + } |
| 89 | + |
| 90 | + private SessionProxy sessionProxyFactory( |
| 91 | + final int sessionBufferSize, |
| 92 | + final GatewayPublication gatewayPublication, |
| 93 | + final SessionIdStrategy sessionIdStrategy, |
| 94 | + final SessionCustomisationStrategy customisationStrategy, |
| 95 | + final EpochNanoClock clock, |
| 96 | + final long connectionId, |
| 97 | + final int libraryId, |
| 98 | + final ErrorHandler errorHandler, |
| 99 | + final EpochFractionFormat epochFractionPrecision) { |
| 100 | + return new DirectSessionProxy(sessionBufferSize, gatewayPublication, sessionIdStrategy, customisationStrategy, |
| 101 | + clock, connectionId, libraryId, errorHandler, epochFractionPrecision) { |
| 102 | + @Override |
| 103 | + public long sendResendRequest(int msgSeqNo, int beginSeqNo, int endSeqNo, int sequenceIndex, int lastMsgSeqNumProcessed) { |
| 104 | + sendResendRequestCalled = true; |
| 105 | +// try { |
| 106 | +// Thread.sleep(10); |
| 107 | +// } catch (InterruptedException ignored) { |
| 108 | +// } |
| 109 | + return super.sendResendRequest(msgSeqNo, beginSeqNo, endSeqNo, sequenceIndex, lastMsgSeqNumProcessed); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public long sendSequenceReset(int msgSeqNo, int newSeqNo, int sequenceIndex, int lastMsgSeqNumProcessed) { |
| 114 | + sendSequenceResetCalled = true; |
| 115 | + return super.sendSequenceReset(msgSeqNo, newSeqNo, sequenceIndex, lastMsgSeqNumProcessed); |
| 116 | + } |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + @Test(timeout = TEST_TIMEOUT_IN_MS) |
| 121 | + public void shouldNotInvertResendAndReset() throws Exception { |
| 122 | + useProxy = false; |
| 123 | + reconnectTest(); |
| 124 | + } |
| 125 | + |
| 126 | + @Test(timeout = TEST_TIMEOUT_IN_MS) |
| 127 | + public void shouldCallProxySendSequenceReset() throws Exception { |
| 128 | + useProxy = true; |
| 129 | + reconnectTest(); |
| 130 | + } |
| 131 | + |
| 132 | + private void reconnectTest() throws Exception { |
| 133 | + launch(); |
| 134 | + |
| 135 | + connectAndAcquire(); |
| 136 | + |
| 137 | + messagesCanBeExchanged(); |
| 138 | + |
| 139 | + disconnectSessions(); |
| 140 | + Exceptions.closeAll(this::closeAcceptingEngine); |
| 141 | + |
| 142 | + assertEquals(3, acceptingSession.lastReceivedMsgSeqNum()); |
| 143 | + assertEquals(3, initiatingSession.lastReceivedMsgSeqNum()); |
| 144 | + |
| 145 | + DebugServer srv = new DebugServer(port); |
| 146 | + srv.setWaitForData(true); |
| 147 | + srv.addFIXResponse("8=FIX.4.4|9=94|35=A|49=acceptor|56=initiator|34=5|52=***|98=0|108=10|141=N|35002=0|35003=0|10=024|"); |
| 148 | + srv.addFIXResponse("8=FIX.4.4|9=94|35=2|49=acceptor|56=initiator|34=6|52=***|7=4|16=0|10=024|"); |
| 149 | + srv.start(); |
| 150 | + |
| 151 | + connectPersistentSessions(4, 4, false); |
| 152 | + |
| 153 | + DebugFIXClient exchange = new DebugFIXClient(srv.popClient(5000)); |
| 154 | + exchange.popAndAssert("35=A 34=4"); |
| 155 | + exchange.popAndAssert("35=2 34=5 7=4 16=0"); |
| 156 | + exchange.popAndAssert("35=4 34=4 36=6"); |
| 157 | + |
| 158 | + exchange.close(); |
| 159 | + srv.stop(); |
| 160 | + Exceptions.closeAll(this::closeInitiatingEngine, mediaDriver); |
| 161 | + |
| 162 | + if (useProxy) { |
| 163 | + assertTrue("SessionProxy.sendResendRequest() not called", sendResendRequestCalled); |
| 164 | + assertTrue("SessionProxy.sendSequenceReset() not called", sendSequenceResetCalled); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private void connectAndAcquire() { |
| 169 | + connectSessions(); |
| 170 | + acceptingSession = acceptingHandler.lastSession(); |
| 171 | + } |
| 172 | +} |
0 commit comments