Skip to content

Commit 9041f34

Browse files
authored
Use free port for each run instead of fixed port. (#419)
1 parent edea56d commit 9041f34

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

quickfixj-core/src/test/java/quickfix/test/acceptance/resynch/ResynchTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package quickfix.test.acceptance.resynch;
2121

22+
import org.apache.mina.util.AvailablePortFinder;
2223
import org.junit.After;
2324
import org.junit.Before;
2425
import org.junit.Test;
@@ -33,11 +34,13 @@
3334
public class ResynchTest {
3435

3536
ResynchTestServer server;
37+
int port;
3638

3739
@Before
3840
public void setUp() throws Exception {
3941
SystemTime.setTimeSource(null);
40-
server = new ResynchTestServer();
42+
port = AvailablePortFinder.getNextAvailable();
43+
server = new ResynchTestServer(port);
4144
}
4245

4346
@After
@@ -49,7 +52,7 @@ public void tearDown() throws Exception {
4952
public void testAcceptorTimerSync() throws ConfigError, SessionNotFound, InterruptedException {
5053
server.start();
5154
server.waitForInitialization();
52-
new ResynchTestClient().run();
55+
new ResynchTestClient(port).run();
5356
}
5457

5558
@Test(timeout=30000)
@@ -58,7 +61,7 @@ public void testAcceptorTimerUnsyncWithValidatingSequenceNumbers() throws Config
5861
server.setValidateSequenceNumbers(true);
5962
server.start();
6063
server.waitForInitialization();
61-
ResynchTestClient client = new ResynchTestClient();
64+
ResynchTestClient client = new ResynchTestClient(port);
6265
client.setUnsynchMode(true);
6366
client.run();
6467
}
@@ -69,7 +72,7 @@ public void testAcceptorTimerUnsyncWithoutValidatingSequenceNumbers() throws Con
6972
server.setValidateSequenceNumbers(false);
7073
server.start();
7174
server.waitForInitialization();
72-
ResynchTestClient client = new ResynchTestClient();
75+
ResynchTestClient client = new ResynchTestClient(port);
7376
client.setUnsynchMode(false);
7477
client.setForceResynch(true);
7578
client.run();

quickfixj-core/src/test/java/quickfix/test/acceptance/resynch/ResynchTestClient.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,17 @@ public class ResynchTestClient extends MessageCracker implements Application {
5555
private final SessionSettings settings = new SessionSettings();
5656
private final CountDownLatch shutdownLatch = new CountDownLatch(1);
5757
private boolean failed;
58+
private final int port;
5859

5960
private boolean unsynchMode = false;
6061
private boolean forceResynch = false;
6162

63+
public ResynchTestClient(int port) {
64+
this.port = port;
65+
}
66+
67+
68+
6269
public void fromAdmin(Message message, SessionID sessionId) throws FieldNotFound,
6370
IncorrectDataFormat, IncorrectTagValue, RejectLogon {
6471
try {
@@ -102,7 +109,7 @@ public void run() throws ConfigError, SessionNotFound, InterruptedException {
102109
defaults.put("ConnectionType", "initiator");
103110
defaults.put("HeartBtInt", "2");
104111
defaults.put("SocketConnectHost", "localhost");
105-
defaults.put("SocketConnectPort", "19889");
112+
defaults.put("SocketConnectPort", String.valueOf(port));
106113
defaults.put("SocketTcpNoDelay", "Y");
107114
defaults.put("ReconnectInterval", "3");
108115
defaults.put("StartTime", "00:00:00");
@@ -154,12 +161,6 @@ public void toAdmin(Message message, SessionID sessionId) {
154161
public void toApp(Message message, SessionID sessionId) throws DoNotSend {
155162
}
156163

157-
public static void main(String[] args) throws ConfigError, SessionNotFound,
158-
InterruptedException {
159-
ResynchTestClient ttc = new ResynchTestClient();
160-
ttc.run();
161-
}
162-
163164
public void setUnsynchMode(boolean unsynchMode) {
164165
this.unsynchMode = unsynchMode;
165166
}

quickfixj-core/src/test/java/quickfix/test/acceptance/resynch/ResynchTestServer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,15 @@ public class ResynchTestServer extends MessageCracker implements Application, Ru
5555
private Thread serverThread;
5656
private final CountDownLatch initializationLatch = new CountDownLatch(1);
5757
private final CountDownLatch shutdownLatch = new CountDownLatch(1);
58+
private final int port;
5859

5960
private boolean unsynchMode = false;
6061
private boolean validateSequenceNumbers = true;
6162

63+
public ResynchTestServer(int port) {
64+
this.port = port;
65+
}
66+
6267
@Override
6368
public void fromAdmin(Message message, SessionID sessionId) throws FieldNotFound,
6469
IncorrectDataFormat, IncorrectTagValue, RejectLogon {
@@ -107,7 +112,7 @@ public void run() {
107112
try {
108113
HashMap<Object, Object> defaults = new HashMap<>();
109114
defaults.put("ConnectionType", "acceptor");
110-
defaults.put("SocketAcceptPort", "19889");
115+
defaults.put("SocketAcceptPort", String.valueOf(port));
111116
defaults.put("StartTime", "00:00:00");
112117
defaults.put("EndTime", "00:00:00");
113118
defaults.put("SenderCompID", "ISLD");
@@ -161,11 +166,6 @@ public void waitForInitialization() throws InterruptedException {
161166
initializationLatch.await();
162167
}
163168

164-
public static void main(String[] args) {
165-
ResynchTestServer server = new ResynchTestServer();
166-
server.run();
167-
}
168-
169169
public void setUnsynchMode(boolean unsynchMode) {
170170
this.unsynchMode = unsynchMode;
171171
}

0 commit comments

Comments
 (0)