66
77import org .eclipse .paho .client .mqttv3 .MqttClient ;
88import org .eclipse .paho .client .mqttv3 .MqttConnectOptions ;
9+ import org .eclipse .paho .client .mqttv3 .MqttException ;
910import org .eclipse .paho .client .mqttv3 .persist .MemoryPersistence ;
1011import org .eclipse .paho .client .mqttv3 .test .logging .LoggingUtilities ;
1112import org .eclipse .paho .client .mqttv3 .test .properties .TestProperties ;
@@ -51,6 +52,7 @@ public static void tearDownAfterClass() throws Exception {
5152 proxy .stopProxy ();
5253
5354 }
55+
5456
5557 /**
5658 * Tests that if a connection is opened and then is lost that the client automatically reconnects
@@ -73,17 +75,14 @@ public void testAutomaticReconnectAfterDisconnect() throws Exception{
7375 Assert .assertTrue (isConnected );
7476
7577 proxy .disableProxy ();
76-
77- // Give it a second to close everything down
78- Thread .sleep (100 );
7978 isConnected = client .isConnected ();
8079 log .info ("Proxy Disconnect isConnected: " + isConnected );
8180 Assert .assertFalse (isConnected );
8281
8382 proxy .enableProxy ();
8483 // give it some time to reconnect
8584 long currentTime = System .currentTimeMillis ();
86- int timeout = 16000 ;
85+ int timeout = 4000 ;
8786 while (client .isConnected () == false ){
8887 long now = System .currentTimeMillis ();
8988 if ((currentTime + timeout ) < now ){
@@ -121,17 +120,14 @@ public void testManualReconnectAfterDisconnect() throws Exception {
121120 Assert .assertTrue (isConnected );
122121
123122 proxy .disableProxy ();
124-
125- // Give it a second to close everything down
126- Thread .sleep (100 );
127123 isConnected = client .isConnected ();
128124 log .info ("Proxy Disconnect isConnected: " + isConnected );
129125 Assert .assertFalse (isConnected );
130126
131127 proxy .enableProxy ();
132128 client .reconnect ();
133129 // give it some time to reconnect
134- Thread .sleep (5000 );
130+ Thread .sleep (4000 );
135131 isConnected = client .isConnected ();
136132 log .info ("Proxy Re-Enabled isConnected: " + isConnected );
137133 Assert .assertTrue (isConnected );
@@ -140,4 +136,48 @@ public void testManualReconnectAfterDisconnect() throws Exception {
140136 }
141137
142138
139+ /**
140+ * Tests that if the initial connection attempt fails, that the automatic reconnect code does NOT
141+ * engage.
142+ */
143+ @ Test
144+ public void testNoAutomaticReconnectWithNoInitialConnect () throws Exception {
145+ String methodName = Utility .getMethodName ();
146+ LoggingUtilities .banner (log , cclass , methodName );
147+ MqttConnectOptions options = new MqttConnectOptions ();
148+ options .setCleanSession (true );
149+ options .setAutomaticReconnect (true );
150+ options .setConnectionTimeout (15 );
151+ final MqttClient client = new MqttClient ("tcp://localhost:4242" , clientId , DATA_STORE );
152+
153+ // Make sure the proxy is disabled and give it a second to close everything down
154+ proxy .disableProxy ();
155+ try {
156+ client .connect (options );
157+ } catch (MqttException ex ) {
158+ // Exceptions are good in this case!
159+ }
160+ boolean isConnected = client .isConnected ();
161+ log .info ("First Connection isConnected: " + isConnected );
162+ Assert .assertFalse (isConnected );
163+
164+ // Enable The Proxy
165+ proxy .enableProxy ();
166+
167+ // Give it some time to make sure we are still not connected
168+ long currentTime = System .currentTimeMillis ();
169+ int timeout = 4000 ;
170+ while (client .isConnected () == false ){
171+ long now = System .currentTimeMillis ();
172+ if ((currentTime + timeout ) < now ){
173+ Assert .assertFalse (isConnected );
174+ break ;
175+ }
176+ Thread .sleep (500 );
177+ }
178+ isConnected = client .isConnected ();
179+ log .info ("Proxy Re-Enabled isConnected: " + isConnected );
180+ Assert .assertFalse (isConnected );
181+
182+ }
143183}
0 commit comments