Skip to content

Commit 0cd4fb7

Browse files
committed
review tests
1 parent 78e63f4 commit 0cd4fb7

13 files changed

Lines changed: 538 additions & 746 deletions

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ runForever()
197197
proc mqttPub() {.async.} =
198198
await ctx.start()
199199
await ctx.publish("nmqtt", "hallo", 2)
200-
await sleepAsync 500
200+
await sleepAsync(500)
201201
await ctx.disconnect()
202202
203203
waitFor mqttPub()
@@ -214,11 +214,11 @@ proc mqttSubPub() {.async.} =
214214
215215
# Subscribe to topic the topic `nmqtt`
216216
await ctx.subscribe("nmqtt", 2, onData)
217-
await sleepAsync 500
217+
await sleepAsync(500)
218218
219219
# Publish a message to the topic `nmqtt`
220220
await ctx.publish("nmqtt", "hallo", 2)
221-
await sleepAsync 500
221+
await sleepAsync(500)
222222
223223
# Disconnect
224224
await ctx.disconnect()

‎nmqtt.nim‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ proc runRx(ctx: MqttCtx) {.async.} =
10771077

10781078
proc runPing(ctx: MqttCtx) {.async.} =
10791079
while true:
1080-
await sleepAsync ctx.keepAlive.int * 1000
1080+
await sleepAsync(ctx.keepAlive.int * 1000)
10811081
let ok = await ctx.sendPingReq()
10821082
if not ok:
10831083
break
@@ -1135,7 +1135,7 @@ proc runConnect(ctx: MqttCtx) {.async.} =
11351135
for topic, cb in ctx.pubCallbacks:
11361136
let msgId = ctx.nextMsgId()
11371137
ctx.workQueue[msgId] = Work(wk: SubWork, msgId: msgId, topic: topic, qos: cb.qos, typ: Subscribe)
1138-
await sleepAsync 1000
1138+
await sleepAsync(1000)
11391139

11401140
#
11411141
# Public API

‎nmqtt/nmqtt_pub.nim‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ proc nmqttPub(host="127.0.0.1", port=1883, ssl=false, clientid="", username="",
4747
else:
4848
for i in 0..repeat-1:
4949
waitFor ctx.publish(topic, msg, qos, retain)
50-
if repeatdelay > 0: waitFor sleepAsync (repeatdelay * 1000)
50+
if repeatdelay > 0: waitFor sleepAsync(repeatdelay * 1000)
5151

5252
# Check that the message has been succesfully send
5353
while ctx.workQueue.len() > 0:

‎tests/connection.nim‎

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,104 +5,90 @@ suite "test suite for connections":
55
let (tpc, msg) = tdata("connection public broker")
66

77
proc conn() {.async.} =
8-
let ctx = newMqttCtx("nmqttTestConn")
9-
ctx.set_host("test.mosquitto.org", 1883)
8+
let ctx = newMqttCtx("nmqttTestConn" & tpc) # unique clientid for public broker
9+
ctx.setHost("broker-cn.emqx.io", 1883)
1010
await ctx.connect()
1111
await sleepAsync(1500)
1212
check(ctx.state == Connected)
1313
await ctx.publish(tpc, msg, 0)
1414
await sleepAsync(1500)
1515
await ctx.disconnect()
1616
check(ctx.state == Disabled)
17-
waitFor conn()
1817

18+
waitFor conn()
1919

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

2323
proc conn() {.async.} =
24-
let ctx = newMqttCtx("nmqttTestConn")
25-
ctx.set_host("test.mosquitto.org", 8883, true)
24+
let ctx = newMqttCtx("nmqttTestConn" & tpc) # unique clientid for public broker
25+
ctx.setHost("broker-cn.emqx.io", 8883, true)
2626
await ctx.connect()
2727
await sleepAsync(1500)
2828
check(ctx.state == Connected)
2929
await ctx.publish(tpc, msg, 0)
3030
await sleepAsync(1500)
3131
await ctx.disconnect()
3232
check(ctx.state == Disabled)
33-
waitFor conn()
34-
35-
36-
#[test "connection wrong port - timeout":
37-
proc conn() {.async.} =
38-
let ctx = newMqttCtx("nmqttTestConn")
39-
ctx.set_host("test.mosquitto.org", 2222)
40-
await ctx.start()
41-
check(ctx.state == Error)
42-
waitFor conn()]#
4333

34+
waitFor conn()
4435

4536
test "connect() to broker":
46-
## FAILS. Due to `runConnect` it will reconnect forever
37+
let ctxMain = newCtx()
4738

4839
proc conn() {.async.} =
49-
await ctxSlave.connect()
40+
await ctxMain.connect()
5041
await sleepAsync(500)
51-
check(ctxSlave.state == Connected)
42+
check(ctxMain.state == Connected)
5243

5344
# Do important stuff
5445
await sleepAsync(500)
5546

5647
# Disconnect
57-
await ctxSlave.disconnect()
58-
check(ctxSlave.state == Disabled)
48+
await ctxMain.disconnect()
49+
check(ctxMain.state == Disabled)
5950

6051
waitFor conn()
6152

62-
6353
test "start() and reconnect":
64-
## FAILS. Due to `runConnect` it will reconnect forever
54+
let ctxMain = newCtx()
6555

6656
proc conn() {.async.} =
6757
await sleepAsync(500)
68-
await ctxSlave.start()
69-
await sleepAsync(500)
70-
check(ctxSlave.state == Connected)
58+
check(ctxMain.state == Connected)
7159

7260
# Do important stuff
7361
await sleepAsync(500)
7462

7563
# Close connection
76-
ctxSlave.state = Disconnecting
77-
ctxSlave.s.close()
78-
echo(ctxSlave.state) # = Disconnected
64+
ctxMain.state = Disconnecting
65+
ctxMain.s.close()
7966
await sleepAsync(500)
8067

8168
# Auto-reconnect goes on `Disconnected"
82-
ctxSlave.state = Disconnected
69+
ctxMain.state = Disconnected
8370
# Auto-reconnect loop is 1000ms, wait 2000ms to ensure loop
8471
await sleepAsync(2000)
8572

8673
# Check reconnect
87-
check(ctxSlave.state == Connected)
74+
check(ctxMain.state == Connected)
8875

8976
# Disconnect
90-
await ctxSlave.disconnect()
91-
check(ctxSlave.state == Disabled)
77+
await ctxMain.disconnect()
78+
check(ctxMain.state == Disabled)
9279

9380
waitFor conn()
9481

95-
9682
test "isConnected()":
97-
## FAILS. Due to `runConnect` it will reconnect forever
83+
let ctxMain = newCtx()
9884

9985
proc conn() {.async.} =
100-
check(ctxSlave.isConnected() == false)
101-
await ctxSlave.connect()
86+
check(ctxMain.isConnected() == false)
87+
await ctxMain.connect()
10288
await sleepAsync(500)
103-
check(ctxSlave.isConnected() == true)
89+
check(ctxMain.isConnected() == true)
10490

105-
await ctxSlave.disconnect()
106-
check(ctxSlave.state == Disabled)
91+
await ctxMain.disconnect()
92+
check(ctxMain.state == Disabled)
10793

108-
waitFor conn()
94+
waitFor conn()

‎tests/ping.nim‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
suite "test suite for ping":
33

44
test "set ping interval":
5-
let (tpc, msg) = tdata("set ping interval")
5+
let
6+
ctxMain = newCtx()
7+
(tpc, msg) = tdata("set ping interval")
68

79
proc conn() {.async.} =
810

9-
ctxSlave.set_ping_interval(1)
10-
await ctxSlave.connect()
11+
ctxMain.setPingInterval(1)
12+
await ctxMain.connect()
1113
await sleepAsync(6000)
1214

1315
var
@@ -21,12 +23,12 @@ suite "test suite for ping":
2123
check(pingCount > 3)
2224
check(pingResp > 3)
2325

24-
await ctxSlave.disconnect()
25-
await sleepAsync(1500)
26+
await ctxMain.disconnect()
27+
await sleepAsync(500)
2628

2729
testDmp = @[]
28-
ctxSlave.set_ping_interval(60)
29-
await ctxSlave.connect()
30+
ctxMain.setPingInterval(60)
31+
await ctxMain.connect()
3032
await sleepAsync(6000)
3133

3234
pingCount = 0
@@ -39,7 +41,4 @@ suite "test suite for ping":
3941
check(pingCount == 0)
4042
check(pingResp == 0)
4143

42-
await ctxSlave.disconnect()
43-
await sleepAsync(500)
44-
4544
waitFor conn()

0 commit comments

Comments
 (0)