1414
1515package org .eclipse .paho .client .mqttv3 .test .connectionLoss ;
1616
17+ import java .net .URI ;
1718import java .util .Date ;
1819import java .util .Timer ;
1920import java .util .TimerTask ;
21+ import java .util .logging .Level ;
2022import java .util .logging .Logger ;
2123
2224import org .eclipse .paho .client .mqttv3 .IMqttDeliveryToken ;
2729import org .eclipse .paho .client .mqttv3 .MqttException ;
2830import org .eclipse .paho .client .mqttv3 .MqttMessage ;
2931import org .eclipse .paho .client .mqttv3 .persist .MqttDefaultFilePersistence ;
30- import org .eclipse .paho .client .mqttv3 .test .ManualTest ;
32+ import org .eclipse .paho .client .mqttv3 .test .logging .LoggingUtilities ;
33+ import org .eclipse .paho .client .mqttv3 .test .properties .TestProperties ;
34+ import org .eclipse .paho .client .mqttv3 .test .utilities .ConnectionManipulationProxyServer ;
35+ import org .eclipse .paho .client .mqttv3 .test .utilities .Utility ;
36+ import org .junit .AfterClass ;
3137import org .junit .Assert ;
38+ import org .junit .BeforeClass ;
3239import org .junit .Test ;
33- import org .junit .experimental .categories .Category ;
3440
3541/**
3642 * These tests verify whether paho can successfully detect a loss of connection with a broker.
4248 *
4349 * @author mcarrer
4450 */
45- @ Category ( ManualTest . class )
46- public class ConnectionLossManualTest implements MqttCallback
51+
52+ public class ConnectionLossTest implements MqttCallback
4753{
48- static final Class <?> cclass = ConnectionLossManualTest .class ;
54+ static final Class <?> cclass = ConnectionLossTest .class ;
4955 private static final String className = cclass .getName ();
5056 private static final Logger log = Logger .getLogger (className );
5157
@@ -56,6 +62,36 @@ public class ConnectionLossManualTest implements MqttCallback
5662 private String clientId = "device-client-id" ;
5763 private String message = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" ;
5864
65+ static ConnectionManipulationProxyServer proxy ;
66+ private static URI serverURI ;
67+
68+
69+ @ BeforeClass
70+ public static void setUpBeforeClass () throws Exception {
71+ try {
72+ String methodName = Utility .getMethodName ();
73+ LoggingUtilities .banner (log , cclass , methodName );
74+ serverURI = TestProperties .getServerURI ();
75+ // Use 0 for the first time.
76+ proxy = new ConnectionManipulationProxyServer (serverURI .getHost (), serverURI .getPort (), 0 );
77+ proxy .startProxy ();
78+ while (!proxy .isPortSet ()){
79+ Thread .sleep (0 );
80+ }
81+ log .log (Level .INFO , "Proxy Started, port set to: " + proxy .getLocalPort ());
82+ } catch (Exception exception ) {
83+ log .log (Level .SEVERE , "caught exception:" , exception );
84+ throw exception ;
85+ }
86+
87+ }
88+
89+ @ AfterClass
90+ public static void tearDownAfterClass () throws Exception {
91+ log .info ("Tests finished, stopping proxy" );
92+ proxy .stopProxy ();
93+
94+ }
5995 /**
6096 * Tests whether paho can detect a connection loss with the server even if it has outbound activity by publishing messages with QoS 0.
6197 * @throws Exception
@@ -72,8 +108,9 @@ public void testConnectionLossWhilePublishingQos0()
72108 options .setPassword (password );
73109 options .setKeepAliveInterval (keepAlive );
74110
75- MqttClient client = new MqttClient ("tcp://iot.eclipse.org:1883" , clientId , DATA_STORE );
111+ MqttClient client = new MqttClient ("tcp://localhost:" + proxy . getLocalPort () , clientId , DATA_STORE );
76112 client .setCallback (this );
113+ proxy .enableProxy ();
77114 client .connect (options );
78115
79116 log .info ((new Date ())+" - Connected." );
@@ -82,6 +119,7 @@ public void testConnectionLossWhilePublishingQos0()
82119 client .publish (username +"/" +clientId +"/abc" , message .getBytes (), 0 , false );
83120 Thread .sleep (1000 );
84121 }
122+ proxy .disableProxy ();
85123
86124 final int [] res = new int [1 ];
87125 new Timer ().schedule ( new TimerTask () {
@@ -126,8 +164,9 @@ public void testConnectionLossWhilePublishingQos1()
126164 options .setPassword (password );
127165 options .setKeepAliveInterval (keepAlive );
128166
129- MqttClient client = new MqttClient ("tcp://iot.eclipse.org:1883" , clientId , DATA_STORE );
167+ MqttClient client = new MqttClient ("tcp://localhost:" + proxy . getLocalPort () , clientId , DATA_STORE );
130168 client .setCallback (this );
169+ proxy .enableProxy ();
131170 client .connect (options );
132171
133172 log .info ((new Date ())+" - Connected." );
@@ -136,6 +175,7 @@ public void testConnectionLossWhilePublishingQos1()
136175 client .publish (username +"/" +clientId +"/abc" , message .getBytes (), 1 , false );
137176 Thread .sleep (1000 );
138177 }
178+ proxy .disableProxy ();
139179
140180 final int [] res = new int [1 ];
141181 new Timer ().schedule ( new TimerTask () {
@@ -181,8 +221,9 @@ public void testConnectionLossWhilePublishingQos2()
181221 options .setPassword (password );
182222 options .setKeepAliveInterval (keepAlive );
183223
184- MqttClient client = new MqttClient ("tcp://iot.eclipse.org:1883" , clientId , DATA_STORE );
224+ MqttClient client = new MqttClient ("tcp://localhost:" + proxy . getLocalPort () , clientId , DATA_STORE );
185225 client .setCallback (this );
226+ proxy .enableProxy ();
186227 client .connect (options );
187228
188229 log .info ((new Date ())+" - Connected." );
@@ -191,6 +232,7 @@ public void testConnectionLossWhilePublishingQos2()
191232 client .publish (username +"/" +clientId +"/abc" , message .getBytes (), 2 , false );
192233 Thread .sleep (1000 );
193234 }
235+ proxy .disableProxy ();
194236
195237 final int [] res = new int [1 ];
196238 new Timer ().schedule ( new TimerTask () {
@@ -235,9 +277,9 @@ public void testConnectionLossWhilePublishingQos1Async()
235277 options .setPassword (password );
236278 options .setKeepAliveInterval (keepAlive );
237279
238- MqttAsyncClient client = new MqttAsyncClient ("tcp://iot.eclipse.org:1883" , clientId , DATA_STORE );
280+ MqttAsyncClient client = new MqttAsyncClient ("tcp://localhost:" + proxy . getLocalPort () , clientId , DATA_STORE );
239281 client .setCallback (this );
240-
282+ proxy . enableProxy ();
241283 log .info ((new Date ())+" - Connecting..." );
242284 client .connect (options );
243285 while (!client .isConnected ()) {
@@ -250,7 +292,7 @@ public void testConnectionLossWhilePublishingQos1Async()
250292 client .publish (username +"/" +clientId +"/abc" , message .getBytes (), 1 , false );
251293 Thread .sleep (1000 );
252294 }
253-
295+ proxy . disableProxy ();
254296 final int [] res = new int [1 ];
255297 new Timer ().schedule ( new TimerTask () {
256298 @ Override
@@ -298,8 +340,9 @@ public void testKeepConnectionOpenWhilePublishingQos0()
298340 options .setPassword (password );
299341 options .setKeepAliveInterval (keepAlive );
300342
301- MqttClient client = new MqttClient ("tcp://iot.eclipse.org:1883" , clientId , DATA_STORE );
343+ MqttClient client = new MqttClient ("tcp://localhost:" + proxy . getLocalPort () , clientId , DATA_STORE );
302344 client .setCallback (this );
345+ proxy .enableProxy ();
303346 client .connect (options );
304347
305348 log .info ((new Date ())+" - Connected." );
@@ -342,8 +385,9 @@ public void testKeepConnectionOpenIdle()
342385 options .setPassword (password );
343386 options .setKeepAliveInterval (keepAlive );
344387
345- MqttClient client = new MqttClient ("tcp://iot.eclipse.org:1883" , clientId , DATA_STORE );
388+ MqttClient client = new MqttClient ("tcp://localhost:" + proxy . getLocalPort () , clientId , DATA_STORE );
346389 client .setCallback (this );
390+ proxy .enableProxy ();
347391 client .connect (options );
348392
349393 log .info ((new Date ())+" - Connected." );
0 commit comments