|
17 | 17 | import org.junit.BeforeClass; |
18 | 18 | import org.junit.Test; |
19 | 19 |
|
20 | | -public class AutomaticReconnectTest{ |
21 | | - |
| 20 | +public class AutomaticReconnectTest { |
| 21 | + |
22 | 22 | static final Class<?> cclass = AutomaticReconnectTest.class; |
23 | 23 | private static final String className = cclass.getName(); |
24 | 24 | private static final Logger log = Logger.getLogger(className); |
25 | | - |
| 25 | + |
26 | 26 | private static final MemoryPersistence DATA_STORE = new MemoryPersistence(); |
27 | 27 |
|
28 | | - |
29 | 28 | private static URI serverURI; |
30 | | - private String clientId = "device-client-id"; |
| 29 | + private String clientId = "device-client-id"; |
31 | 30 | static ConnectionManipulationProxyServer proxy; |
32 | | - |
33 | | - |
| 31 | + |
34 | 32 | @BeforeClass |
35 | | - public static void setUpBeforeClass() throws Exception{ |
| 33 | + public static void setUpBeforeClass() throws Exception { |
36 | 34 | try { |
37 | 35 | String methodName = Utility.getMethodName(); |
38 | 36 | LoggingUtilities.banner(log, cclass, methodName); |
39 | 37 | serverURI = TestProperties.getServerURI(); |
40 | 38 | // Use 0 for the first time. |
41 | 39 | proxy = new ConnectionManipulationProxyServer(serverURI.getHost(), serverURI.getPort(), 0); |
42 | 40 | proxy.startProxy(); |
43 | | - while(!proxy.isPortSet()){ |
| 41 | + while (!proxy.isPortSet()) { |
44 | 42 | Thread.sleep(0); |
45 | 43 | } |
46 | 44 | log.log(Level.INFO, "Proxy Started, port set to: " + proxy.getLocalPort()); |
47 | 45 | } catch (Exception exception) { |
48 | | - log.log(Level.SEVERE, "caught exception:", exception); |
49 | | - throw exception; |
50 | | - } |
51 | | - |
| 46 | + log.log(Level.SEVERE, "caught exception:", exception); |
| 47 | + throw exception; |
| 48 | + } |
| 49 | + |
52 | 50 | } |
53 | | - |
| 51 | + |
54 | 52 | @AfterClass |
55 | 53 | public static void tearDownAfterClass() throws Exception { |
56 | 54 | log.info("Tests finished, stopping proxy"); |
57 | 55 | proxy.stopProxy(); |
58 | | - |
| 56 | + |
59 | 57 | } |
60 | | - |
61 | 58 |
|
62 | 59 | /** |
63 | | - * Tests that if a connection is opened and then is lost that the client automatically reconnects |
| 60 | + * Tests that if a connection is opened and then is lost that the client |
| 61 | + * automatically reconnects |
| 62 | + * |
64 | 63 | * @throws Exception |
65 | 64 | */ |
66 | 65 | @Test |
67 | | - public void testAutomaticReconnectAfterDisconnect() throws Exception{ |
68 | | - String methodName = Utility.getMethodName(); |
69 | | - LoggingUtilities.banner(log, cclass, methodName); |
70 | | - MqttConnectionOptions options = new MqttConnectionOptions(); |
71 | | - options.setCleanSession(true); |
72 | | - options.setAutomaticReconnect(true); |
73 | | - final MqttLegacyBlockingClient client = new MqttLegacyBlockingClient("tcp://localhost:" + proxy.getLocalPort(), clientId, DATA_STORE); |
74 | | - |
75 | | - proxy.enableProxy(); |
76 | | - client.connect(options); |
77 | | - |
78 | | - boolean isConnected = client.isConnected(); |
79 | | - log.info("First Connection isConnected: " + isConnected); |
80 | | - Assert.assertTrue(isConnected); |
81 | | - |
82 | | - proxy.disableProxy(); |
83 | | - isConnected = client.isConnected(); |
84 | | - log.info("Proxy Disconnect isConnected: " + isConnected); |
85 | | - Assert.assertFalse(isConnected); |
86 | | - |
87 | | - proxy.enableProxy(); |
88 | | - // give it some time to reconnect |
89 | | - long currentTime = System.currentTimeMillis(); |
90 | | - int timeout = 4000; |
91 | | - while(client.isConnected() == false){ |
92 | | - long now = System.currentTimeMillis(); |
93 | | - if((currentTime + timeout) < now){ |
94 | | - log.warning("Timeout Exceeded"); |
95 | | - break; |
96 | | - } |
97 | | - Thread.sleep(500); |
98 | | - } |
99 | | - isConnected = client.isConnected(); |
100 | | - log.info("Proxy Re-Enabled isConnected: " + isConnected); |
101 | | - Assert.assertTrue(isConnected); |
102 | | - client.disconnect(); |
103 | | - Assert.assertFalse(client.isConnected()); |
| 66 | + public void testAutomaticReconnectAfterDisconnect() throws Exception { |
| 67 | + String methodName = Utility.getMethodName(); |
| 68 | + LoggingUtilities.banner(log, cclass, methodName); |
| 69 | + MqttConnectionOptions options = new MqttConnectionOptions(); |
| 70 | + options.setCleanSession(true); |
| 71 | + options.setAutomaticReconnect(true); |
| 72 | + final MqttLegacyBlockingClient client = new MqttLegacyBlockingClient("tcp://localhost:" + proxy.getLocalPort(), |
| 73 | + clientId, DATA_STORE); |
| 74 | + |
| 75 | + proxy.enableProxy(); |
| 76 | + client.connect(options); |
| 77 | + |
| 78 | + boolean isConnected = client.isConnected(); |
| 79 | + log.info("First Connection isConnected: " + isConnected); |
| 80 | + Assert.assertTrue(isConnected); |
| 81 | + |
| 82 | + proxy.disableProxy(); |
| 83 | + isConnected = client.isConnected(); |
| 84 | + log.info("Proxy Disconnect isConnected: " + isConnected); |
| 85 | + Assert.assertFalse(isConnected); |
| 86 | + |
| 87 | + proxy.enableProxy(); |
| 88 | + // give it some time to reconnect |
| 89 | + long currentTime = System.currentTimeMillis(); |
| 90 | + int timeout = 4000; |
| 91 | + while (client.isConnected() == false) { |
| 92 | + long now = System.currentTimeMillis(); |
| 93 | + if ((currentTime + timeout) < now) { |
| 94 | + log.warning("Timeout Exceeded"); |
| 95 | + break; |
| 96 | + } |
| 97 | + Thread.sleep(500); |
| 98 | + } |
| 99 | + isConnected = client.isConnected(); |
| 100 | + log.info("Proxy Re-Enabled isConnected: " + isConnected); |
| 101 | + Assert.assertTrue(isConnected); |
| 102 | + client.disconnect(); |
| 103 | + Assert.assertFalse(client.isConnected()); |
104 | 104 | } |
105 | | - |
| 105 | + |
106 | 106 | /** |
107 | | - * Tests that if a connection is opened and lost, that when the user calls reconnect() that the |
108 | | - * client will attempt to reconnect straight away |
| 107 | + * Tests that if a connection is opened and lost, that when the user calls |
| 108 | + * reconnect() that the client will attempt to reconnect straight away |
109 | 109 | */ |
110 | 110 | @Test |
111 | 111 | public void testManualReconnectAfterDisconnect() throws Exception { |
112 | | - String methodName = Utility.getMethodName(); |
113 | | - LoggingUtilities.banner(log, cclass, methodName); |
| 112 | + String methodName = Utility.getMethodName(); |
| 113 | + LoggingUtilities.banner(log, cclass, methodName); |
114 | 114 | MqttConnectionOptions options = new MqttConnectionOptions(); |
115 | | - options.setCleanSession(true); |
116 | | - options.setAutomaticReconnect(true); |
117 | | - |
118 | | - final MqttLegacyBlockingClient client = new MqttLegacyBlockingClient("tcp://localhost:" + proxy.getLocalPort(), clientId, DATA_STORE); |
119 | | - |
120 | | - proxy.enableProxy(); |
121 | | - client.connect(options); |
122 | | - |
123 | | - boolean isConnected = client.isConnected(); |
124 | | - log.info("First Connection isConnected: " + isConnected); |
125 | | - Assert.assertTrue(isConnected); |
126 | | - |
127 | | - proxy.disableProxy(); |
128 | | - isConnected = client.isConnected(); |
129 | | - log.info("Proxy Disconnect isConnected: " + isConnected); |
130 | | - Assert.assertFalse(isConnected); |
131 | | - |
132 | | - proxy.enableProxy(); |
133 | | - client.reconnect(); |
134 | | - // give it some time to reconnect |
135 | | - Thread.sleep(4000); |
136 | | - isConnected = client.isConnected(); |
137 | | - log.info("Proxy Re-Enabled isConnected: " + isConnected); |
138 | | - Assert.assertTrue(isConnected); |
139 | | - client.disconnect(); |
140 | | - Assert.assertFalse(client.isConnected()); |
141 | | - } |
| 115 | + options.setCleanSession(true); |
| 116 | + options.setAutomaticReconnect(true); |
| 117 | + |
| 118 | + final MqttLegacyBlockingClient client = new MqttLegacyBlockingClient("tcp://localhost:" + proxy.getLocalPort(), |
| 119 | + clientId, DATA_STORE); |
| 120 | + |
| 121 | + proxy.enableProxy(); |
| 122 | + client.connect(options); |
142 | 123 |
|
| 124 | + boolean isConnected = client.isConnected(); |
| 125 | + log.info("First Connection isConnected: " + isConnected); |
| 126 | + Assert.assertTrue(isConnected); |
| 127 | + |
| 128 | + proxy.disableProxy(); |
| 129 | + isConnected = client.isConnected(); |
| 130 | + log.info("Proxy Disconnect isConnected: " + isConnected); |
| 131 | + Assert.assertFalse(isConnected); |
| 132 | + |
| 133 | + proxy.enableProxy(); |
| 134 | + client.reconnect(); |
| 135 | + // give it some time to reconnect |
| 136 | + Thread.sleep(4000); |
| 137 | + isConnected = client.isConnected(); |
| 138 | + log.info("Proxy Re-Enabled isConnected: " + isConnected); |
| 139 | + Assert.assertTrue(isConnected); |
| 140 | + client.disconnect(); |
| 141 | + Assert.assertFalse(client.isConnected()); |
| 142 | + } |
143 | 143 |
|
144 | 144 | /** |
145 | | - * Tests that if the initial connection attempt fails, that the automatic reconnect code does NOT |
146 | | - * engage. |
| 145 | + * Tests that if the initial connection attempt fails, that the automatic |
| 146 | + * reconnect code does NOT engage. |
147 | 147 | */ |
148 | 148 | @Test |
149 | 149 | public void testNoAutomaticReconnectWithNoInitialConnect() throws Exception { |
150 | | - String methodName = Utility.getMethodName(); |
151 | | - LoggingUtilities.banner(log, cclass, methodName); |
152 | | - MqttConnectionOptions options = new MqttConnectionOptions(); |
153 | | - options.setCleanSession(true); |
154 | | - options.setAutomaticReconnect(true); |
155 | | - options.setConnectionTimeout(15); |
156 | | - final MqttLegacyBlockingClient client = new MqttLegacyBlockingClient("tcp://localhost:" + proxy.getLocalPort(), clientId, DATA_STORE); |
157 | | - |
158 | | - // Make sure the proxy is disabled and give it a second to close everything down |
159 | | - proxy.disableProxy(); |
160 | | - try { |
161 | | - client.connect(options); |
162 | | - } catch (MqttException ex) { |
163 | | - // Exceptions are good in this case! |
164 | | - } |
165 | | - boolean isConnected = client.isConnected(); |
166 | | - log.info("First Connection isConnected: " + isConnected); |
167 | | - Assert.assertFalse(isConnected); |
168 | | - |
169 | | - // Enable The Proxy |
170 | | - proxy.enableProxy(); |
171 | | - |
172 | | - // Give it some time to make sure we are still not connected |
173 | | - long currentTime = System.currentTimeMillis(); |
174 | | - int timeout = 4000; |
175 | | - while(client.isConnected() == false){ |
176 | | - long now = System.currentTimeMillis(); |
177 | | - if((currentTime + timeout) < now){ |
178 | | - Assert.assertFalse(isConnected); |
179 | | - break; |
180 | | - } |
181 | | - Thread.sleep(500); |
182 | | - } |
183 | | - isConnected = client.isConnected(); |
184 | | - log.info("Proxy Re-Enabled isConnected: " + isConnected); |
185 | | - Assert.assertFalse(isConnected); |
| 150 | + String methodName = Utility.getMethodName(); |
| 151 | + LoggingUtilities.banner(log, cclass, methodName); |
| 152 | + MqttConnectionOptions options = new MqttConnectionOptions(); |
| 153 | + options.setCleanSession(true); |
| 154 | + options.setAutomaticReconnect(true); |
| 155 | + options.setConnectionTimeout(15); |
| 156 | + final MqttLegacyBlockingClient client = new MqttLegacyBlockingClient("tcp://localhost:" + proxy.getLocalPort(), |
| 157 | + clientId, DATA_STORE); |
| 158 | + |
| 159 | + // Make sure the proxy is disabled and give it a second to close everything down |
| 160 | + proxy.disableProxy(); |
| 161 | + try { |
| 162 | + client.connect(options); |
| 163 | + } catch (MqttException ex) { |
| 164 | + // Exceptions are good in this case! |
| 165 | + } |
| 166 | + boolean isConnected = client.isConnected(); |
| 167 | + log.info("First Connection isConnected: " + isConnected); |
| 168 | + Assert.assertFalse(isConnected); |
| 169 | + |
| 170 | + // Enable The Proxy |
| 171 | + proxy.enableProxy(); |
| 172 | + |
| 173 | + // Give it some time to make sure we are still not connected |
| 174 | + long currentTime = System.currentTimeMillis(); |
| 175 | + int timeout = 4000; |
| 176 | + while (client.isConnected() == false) { |
| 177 | + long now = System.currentTimeMillis(); |
| 178 | + if ((currentTime + timeout) < now) { |
| 179 | + Assert.assertFalse(isConnected); |
| 180 | + break; |
| 181 | + } |
| 182 | + Thread.sleep(500); |
| 183 | + } |
| 184 | + isConnected = client.isConnected(); |
| 185 | + log.info("Proxy Re-Enabled isConnected: " + isConnected); |
| 186 | + Assert.assertFalse(isConnected); |
186 | 187 |
|
187 | 188 | } |
188 | 189 | } |
0 commit comments