Skip to content

Commit 9e92a38

Browse files
committed
add index reset when no more connections to try; adapt test case
Signed-off-by: Tim Nielens <tim.nielens@gmail.com>
1 parent d7613b2 commit 9e92a38

2 files changed

Lines changed: 7 additions & 25 deletions

File tree

src/MQTTAsyncUtils.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,8 @@ static void nextOrClose(MQTTAsyncs* m, int rc, char* message)
16841684

16851685
if (!more_to_try)
16861686
{
1687+
// resetting index so that if further connection attempts are made, they will start with the first URI in the list
1688+
m->connect.details.conn.currentURI = 0;
16871689
MQTTAsync_closeSession(m->c, MQTTREASONCODE_SUCCESS, NULL);
16881690
if (m->connect.onFailure)
16891691
{

test/test_serveruris_reset.c

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -210,38 +210,18 @@ static int test_serveruris_reset(void) {
210210
WAIT_TRUE(conn.connected || conn.connect_failed, 3000);
211211
assert("reconnected via 1886", MQTTAsync_isConnected(client), "\n");
212212

213-
/* take 1886 down, bring 1885 up */
213+
/* take 1886 down */
214214
toxi("POST", "/proxies/mqtt2", "{\"enabled\":false}");
215-
toxi("POST", "/proxies/mqtt1", "{\"enabled\":true}");
216215
WAIT_TRUE(!MQTTAsync_isConnected(client), 3000);
217216
assert("disconnected after 1886 went down", !MQTTAsync_isConnected(client), "\n");
218217

219-
/* reconnect — expected to restart cycle from 1885, but URI index is not reset:
220-
paho tries 1886 again (stuck at last index), fails, stays disconnected */
221-
conn.connected = false; conn.connect_failed = false;
222-
MQTTAsync_reconnect(client);
223-
WAIT_TRUE(conn.connected || conn.connect_failed, 3000);
224-
assert("BUG: reconnect did not restart cycle from 1885 — stayed stuck on 1886", !MQTTAsync_isConnected(client), "\n");
225-
226-
/* bring 1886 back, reconnect — succeeds on 1886 */
227-
toxi("POST", "/proxies/mqtt2", "{\"enabled\":true}");
218+
/* reconnect — restart cycle from 1885 */
228219
conn.connected = false; conn.connect_failed = false;
229-
MQTTAsync_reconnect(client);
230-
WAIT_TRUE(conn.connected || conn.connect_failed, 3000);
231-
assert("reconnected via 1886 after it came back", MQTTAsync_isConnected(client), "\n");
232-
233-
/* disconnect + connect (not reconnect) — resets URI index to 0, connects to 1885 */
234-
int disc = 0;
235-
MQTTAsync_disconnectOptions dco = MQTTAsync_disconnectOptions_initializer;
236-
dco.onSuccess = on_disconnect; dco.context = &disc;
237-
MQTTAsync_disconnect(client, &dco);
238-
WAIT_TRUE(disc, 3000);
239-
240-
toxi("POST", "/proxies/mqtt2", "{\"enabled\":false}"); /* only 1885 available */
220+
toxi("POST", "/proxies/mqtt1", "{\"enabled\":true}");
241221
conn.connected = false; conn.connect_failed = false;
242-
MQTTAsync_connect(client, &co);
222+
MQTTAsync_reconnect(client);
243223
WAIT_TRUE(conn.connected || conn.connect_failed, 3000);
244-
assert("WORKAROUND: disconnect+connect resets URI cycle, reconnects to 1885", MQTTAsync_isConnected(client), "\n");
224+
assert("reconnect, restart cycle from 1885", MQTTAsync_isConnected(client), "\n");
245225

246226
MQTTAsync_destroy(&client);
247227
MyLog(LOGA_INFO, "TEST: test_serveruris_reset %s. %d assertions run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures);

0 commit comments

Comments
 (0)