Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ runForever()
proc mqttPub() {.async.} =
await ctx.start()
await ctx.publish("nmqtt", "hallo", 2)
await sleepAsync 500
await sleepAsync(500)
await ctx.disconnect()

waitFor mqttPub()
Expand All @@ -214,11 +214,11 @@ proc mqttSubPub() {.async.} =

# Subscribe to topic the topic `nmqtt`
await ctx.subscribe("nmqtt", 2, onData)
await sleepAsync 500
await sleepAsync(500)

# Publish a message to the topic `nmqtt`
await ctx.publish("nmqtt", "hallo", 2)
await sleepAsync 500
await sleepAsync(500)

# Disconnect
await ctx.disconnect()
Expand Down
4 changes: 2 additions & 2 deletions nmqtt.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ proc runRx(ctx: MqttCtx) {.async.} =

proc runPing(ctx: MqttCtx) {.async.} =
while true:
await sleepAsync ctx.keepAlive.int * 1000
await sleepAsync(ctx.keepAlive.int * 1000)
let ok = await ctx.sendPingReq()
if not ok:
break
Expand Down Expand Up @@ -1135,7 +1135,7 @@ proc runConnect(ctx: MqttCtx) {.async.} =
for topic, cb in ctx.pubCallbacks:
let msgId = ctx.nextMsgId()
ctx.workQueue[msgId] = Work(wk: SubWork, msgId: msgId, topic: topic, qos: cb.qos, typ: Subscribe)
await sleepAsync 1000
await sleepAsync(1000)

#
# Public API
Expand Down
2 changes: 1 addition & 1 deletion nmqtt/nmqtt_pub.nim
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ proc nmqttPub(host="127.0.0.1", port=1883, ssl=false, clientid="", username="",
else:
for i in 0..repeat-1:
waitFor ctx.publish(topic, msg, qos, retain)
if repeatdelay > 0: waitFor sleepAsync (repeatdelay * 1000)
if repeatdelay > 0: waitFor sleepAsync(repeatdelay * 1000)

# Check that the message has been succesfully send
while ctx.workQueue.len() > 0:
Expand Down
66 changes: 26 additions & 40 deletions tests/connection.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,104 +5,90 @@ suite "test suite for connections":
let (tpc, msg) = tdata("connection public broker")

proc conn() {.async.} =
let ctx = newMqttCtx("nmqttTestConn")
ctx.set_host("test.mosquitto.org", 1883)
let ctx = newMqttCtx("nmqttTestConn" & tpc) # unique clientid for public broker
ctx.setHost("broker-cn.emqx.io", 1883)
await ctx.connect()
await sleepAsync(1500)
check(ctx.state == Connected)
await ctx.publish(tpc, msg, 0)
await sleepAsync(1500)
await ctx.disconnect()
check(ctx.state == Disabled)
waitFor conn()

waitFor conn()

test "connection public broker SSL":
let (tpc, msg) = tdata("connection public broker SSL")

proc conn() {.async.} =
let ctx = newMqttCtx("nmqttTestConn")
ctx.set_host("test.mosquitto.org", 8883, true)
let ctx = newMqttCtx("nmqttTestConn" & tpc) # unique clientid for public broker
ctx.setHost("broker-cn.emqx.io", 8883, true)
await ctx.connect()
await sleepAsync(1500)
check(ctx.state == Connected)
await ctx.publish(tpc, msg, 0)
await sleepAsync(1500)
await ctx.disconnect()
check(ctx.state == Disabled)
waitFor conn()


#[test "connection wrong port - timeout":
proc conn() {.async.} =
let ctx = newMqttCtx("nmqttTestConn")
ctx.set_host("test.mosquitto.org", 2222)
await ctx.start()
check(ctx.state == Error)
waitFor conn()]#

waitFor conn()

test "connect() to broker":
## FAILS. Due to `runConnect` it will reconnect forever
let ctxMain = newCtx()

proc conn() {.async.} =
await ctxSlave.connect()
await ctxMain.connect()
await sleepAsync(500)
check(ctxSlave.state == Connected)
check(ctxMain.state == Connected)

# Do important stuff
await sleepAsync(500)

# Disconnect
await ctxSlave.disconnect()
check(ctxSlave.state == Disabled)
await ctxMain.disconnect()
check(ctxMain.state == Disabled)

waitFor conn()


test "start() and reconnect":
## FAILS. Due to `runConnect` it will reconnect forever
let ctxMain = newCtx()

proc conn() {.async.} =
await sleepAsync(500)
await ctxSlave.start()
await sleepAsync(500)
check(ctxSlave.state == Connected)
check(ctxMain.state == Connected)

# Do important stuff
await sleepAsync(500)

# Close connection
ctxSlave.state = Disconnecting
ctxSlave.s.close()
echo(ctxSlave.state) # = Disconnected
ctxMain.state = Disconnecting
ctxMain.s.close()
await sleepAsync(500)

# Auto-reconnect goes on `Disconnected"
ctxSlave.state = Disconnected
ctxMain.state = Disconnected
# Auto-reconnect loop is 1000ms, wait 2000ms to ensure loop
await sleepAsync(2000)

# Check reconnect
check(ctxSlave.state == Connected)
check(ctxMain.state == Connected)

# Disconnect
await ctxSlave.disconnect()
check(ctxSlave.state == Disabled)
await ctxMain.disconnect()
check(ctxMain.state == Disabled)

waitFor conn()


test "isConnected()":
## FAILS. Due to `runConnect` it will reconnect forever
let ctxMain = newCtx()

proc conn() {.async.} =
check(ctxSlave.isConnected() == false)
await ctxSlave.connect()
check(ctxMain.isConnected() == false)
await ctxMain.connect()
await sleepAsync(500)
check(ctxSlave.isConnected() == true)
check(ctxMain.isConnected() == true)

await ctxSlave.disconnect()
check(ctxSlave.state == Disabled)
await ctxMain.disconnect()
check(ctxMain.state == Disabled)

waitFor conn()
waitFor conn()
19 changes: 9 additions & 10 deletions tests/ping.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
suite "test suite for ping":

test "set ping interval":
let (tpc, msg) = tdata("set ping interval")
let
ctxMain = newCtx()
(tpc, msg) = tdata("set ping interval")

proc conn() {.async.} =

ctxSlave.set_ping_interval(1)
await ctxSlave.connect()
ctxMain.setPingInterval(1)
await ctxMain.connect()
await sleepAsync(6000)

var
Expand All @@ -21,12 +23,12 @@ suite "test suite for ping":
check(pingCount > 3)
check(pingResp > 3)

await ctxSlave.disconnect()
await sleepAsync(1500)
await ctxMain.disconnect()
await sleepAsync(500)

testDmp = @[]
ctxSlave.set_ping_interval(60)
await ctxSlave.connect()
ctxMain.setPingInterval(60)
await ctxMain.connect()
await sleepAsync(6000)

pingCount = 0
Expand All @@ -39,7 +41,4 @@ suite "test suite for ping":
check(pingCount == 0)
check(pingResp == 0)

await ctxSlave.disconnect()
await sleepAsync(500)

waitFor conn()
Loading
Loading