@@ -18,61 +18,88 @@ public abstract class Connection {
1818
1919 final static int TIMEOUT = 7_000 ;
2020 final static int PORT = 3000 ;
21+ final static int NO_RECOVERY_PORT = 3001 ;
2122
2223 private Process serverProcess ;
24+ private Process noRecoveryServerProcess ;
2325 private ExecutorService serverService ;
24- private Future serverOutput ;
25- private Future serverError ;
26+ private Future <?> serverOutput ;
27+ private Future <?> serverError ;
28+ private Future <?> noRecoveryServerOutput ;
29+ private Future <?> noRecoveryServerError ;
2630
2731 @ Before
2832 public void startServer () throws IOException , InterruptedException {
29- logger .fine ("Starting server ..." );
33+ logger .fine ("Starting servers ..." );
3034
35+ // Start main server
3136 final CountDownLatch latch = new CountDownLatch (1 );
32- serverProcess = Runtime .getRuntime ().exec (
33- String .format ("node src/test/resources/server.js %s" , nsp ()), createEnv ());
37+ serverProcess = startServerProcess ("node src/test/resources/server.js %s" , PORT );
3438 serverService = Executors .newCachedThreadPool ();
35- serverOutput = serverService .submit (new Runnable () {
36- @ Override
37- public void run () {
38- BufferedReader reader = new BufferedReader (
39- new InputStreamReader (serverProcess .getInputStream ()));
40- String line ;
41- try {
42- line = reader .readLine ();
43- latch .countDown ();
44- do {
45- logger .fine ("SERVER OUT: " + line );
46- } while ((line = reader .readLine ()) != null );
47- } catch (IOException e ) {
48- logger .warning (e .getMessage ());
49- }
39+ serverOutput = startServerOutput (serverProcess , "MAIN" , latch );
40+ serverError = startServerError (serverProcess , "MAIN" );
41+
42+ // Start no-recovery server
43+ final CountDownLatch noRecoveryLatch = new CountDownLatch (1 );
44+ noRecoveryServerProcess = startServerProcess ("node src/test/resources/server_no_recovery.js %s" , NO_RECOVERY_PORT );
45+ noRecoveryServerOutput = startServerOutput (noRecoveryServerProcess , "NO_RECOVERY" , noRecoveryLatch );
46+ noRecoveryServerError = startServerError (noRecoveryServerProcess , "NO_RECOVERY" );
47+
48+ // Wait for both servers to start
49+ latch .await (3000 , TimeUnit .MILLISECONDS );
50+ noRecoveryLatch .await (3000 , TimeUnit .MILLISECONDS );
51+ }
52+
53+ private Process startServerProcess (String script , int port ) throws IOException {
54+ return Runtime .getRuntime ().exec (String .format (script , nsp ()), createEnv (port ));
55+ }
56+
57+ private Future <?> startServerOutput (Process process , String serverName , CountDownLatch latch ) {
58+ return serverService .submit (() -> {
59+ BufferedReader reader = new BufferedReader (
60+ new InputStreamReader (process .getInputStream ()));
61+ String line ;
62+ try {
63+ line = reader .readLine ();
64+ latch .countDown ();
65+ do {
66+ logger .fine (serverName + " SERVER OUT: " + line );
67+ } while ((line = reader .readLine ()) != null );
68+ } catch (IOException e ) {
69+ logger .warning (e .getMessage ());
5070 }
5171 });
52- serverError = serverService .submit (new Runnable () {
53- @ Override
54- public void run () {
55- BufferedReader reader = new BufferedReader (
56- new InputStreamReader (serverProcess .getErrorStream ()));
57- String line ;
58- try {
59- while ((line = reader .readLine ()) != null ) {
60- logger .fine ("SERVER ERR: " + line );
61- }
62- } catch (IOException e ) {
63- logger .warning (e .getMessage ());
72+ }
73+
74+ private Future <?> startServerError (Process process , String serverName ) {
75+ return serverService .submit (() -> {
76+ BufferedReader reader = new BufferedReader (
77+ new InputStreamReader (process .getErrorStream ()));
78+ String line ;
79+ try {
80+ while ((line = reader .readLine ()) != null ) {
81+ logger .fine (serverName + " SERVER ERR: " + line );
6482 }
83+ } catch (IOException e ) {
84+ logger .warning (e .getMessage ());
6585 }
6686 });
67- latch .await (3000 , TimeUnit .MILLISECONDS );
6887 }
6988
7089 @ After
7190 public void stopServer () throws InterruptedException {
72- logger .fine ("Stopping server ..." );
91+ logger .fine ("Stopping servers..." );
92+
93+ // Stop main server
7394 serverProcess .destroy ();
7495 serverOutput .cancel (false );
7596 serverError .cancel (false );
97+
98+ // Stop no-recovery server
99+ noRecoveryServerProcess .destroy ();
100+ noRecoveryServerOutput .cancel (false );
101+ noRecoveryServerError .cancel (false );
102+
76103 serverService .shutdown ();
77104 serverService .awaitTermination (3000 , TimeUnit .MILLISECONDS );
78105 }
@@ -90,11 +117,16 @@ Socket client(IO.Options opts) {
90117 }
91118
92119 Socket client (String path , IO .Options opts ) {
93- return IO .socket (URI .create (uri () + path ), opts );
120+ int port = opts .port != -1 ? opts .port : PORT ;
121+ return IO .socket (URI .create (uri (port ) + path ), opts );
94122 }
95123
96124 URI uri () {
97- return URI .create ("http://localhost:" + PORT );
125+ return uri (PORT );
126+ }
127+
128+ URI uri (int port ) {
129+ return URI .create ("http://localhost:" + port );
98130 }
99131
100132 String nsp () {
@@ -108,16 +140,19 @@ IO.Options createOptions() {
108140 }
109141
110142 String [] createEnv () {
143+ return createEnv (PORT );
144+ }
145+
146+ String [] createEnv (int port ) {
111147 Map <String , String > env = new HashMap <>(System .getenv ());
112148 env .put ("DEBUG" , "socket.io:*" );
113- env .put ("PORT" , String .valueOf (PORT ));
149+ env .put ("PORT" , String .valueOf (port ));
114150 String [] _env = new String [env .size ()];
115151 int i = 0 ;
116152 for (String key : env .keySet ()) {
117153 _env [i ] = key + "=" + env .get (key );
118154 i ++;
119155 }
120156 return _env ;
121-
122157 }
123158}
0 commit comments