test/test_integration.rb in mosquitto-0.2 vs test/test_integration.rb in mosquitto-0.3
- old
+ new
@@ -8,10 +8,11 @@
"a/+/#", "a/#", "+/+/+", "will/topic"]
CLIENT_IDS = %w(test_integration test_lwt test_clean_session test_duplicate)
def setup
+ super
@result = nil
@client = nil
connected = false
@client = Mosquitto::Client.new(nil, true)
@client.loop_start
@@ -20,20 +21,25 @@
connected = true
end
@client.on_message do |msg|
@result = msg.to_s
end
- assert @client.connect(TEST_HOST, TEST_PORT, 10)
+ assert @client.connect(TEST_HOST, TEST_PORT, TIMEOUT)
wait{ connected }
end
def teardown
+ super
disconnected, connected = false, false
@client.on_disconnect do |rc|
disconnected = true
end
- @client.disconnect
+ begin
+ @client.disconnect
+ rescue Mosquitto::Error
+ disconnected = true
+ end
wait{ disconnected }
@client.loop_stop(true)
CLIENT_IDS.each do |client_id|
disconnected = false
@@ -41,13 +47,17 @@
client.loop_start
client.on_disconnect do |rc|
disconnected = true
end
client.on_connect do |rc|
- assert client.disconnect
+ begin
+ assert client.disconnect
+ rescue Mosquitto::Error
+ disconnected = true
+ end
end
- assert client.connect(TEST_HOST, TEST_PORT, 10)
+ assert client.connect(TEST_HOST, TEST_PORT, TIMEOUT)
wait{ disconnected }
client.loop_stop(true)
end
client = Mosquitto::Client.new("purge")
@@ -58,20 +68,23 @@
client.on_message do |msg|
if msg.retain?
assert client.publish(nil, msg.topic, "", Mosquitto::AT_LEAST_ONCE, true)
end
end
- assert client.connect(TEST_HOST, TEST_PORT, 10)
+ assert client.connect(TEST_HOST, TEST_PORT, TIMEOUT)
wait{ connected }
TOPICS.each do |topic|
assert client.subscribe(nil, topic, Mosquitto::AT_MOST_ONCE)
end
sleep 5
- client.disconnect
- client.loop_stop(true)
+ begin
+ client.disconnect
+ client.loop_stop(true)
+ rescue Mosquitto::Error
+ end
end
def test_basic
# check basic pub/sub on QOS 0
expected = "hello mqtt broker on QOS 0"
@@ -422,11 +435,11 @@
def test_subs
assert @client.subscribe(nil, "1/2/3", Mosquitto::AT_MOST_ONCE)
assert @client.subscribe(nil, "a/+/#", Mosquitto::AT_MOST_ONCE)
assert @client.subscribe(nil, "#", Mosquitto::AT_MOST_ONCE)
- sleep 1
+ sleep 3
@result = nil
expected = "should get everything"
assert @client.publish(nil, "1/2/3/4", expected, Mosquitto::AT_MOST_ONCE, false)
wait{ @result }
@@ -474,18 +487,18 @@
client1.on_disconnect do |rc|
client1_disconnected = true
client1.loop_stop(true)
client2.loop_stop(true)
end
- client1.connect(TEST_HOST, TEST_PORT, 10)
+ client1.connect(TEST_HOST, TEST_PORT, TIMEOUT)
client1.wait_readable
client2 = Mosquitto::Client.new("test_duplicate")
client2.loop_start
client2.logger = Logger.new(STDOUT)
- client2.connect(TEST_HOST, TEST_PORT, 10)
+ client2.connect(TEST_HOST, TEST_PORT, TIMEOUT)
client2.wait_readable
assert client1_connected
assert client1_disconnected
@@ -494,29 +507,30 @@
def test_clean_session
client1 = Mosquitto::Client.new("test_clean_session")
client1.logger = Logger.new(STDOUT)
client1.loop_start
client1.will_set("l/w/t", "This is an LWT", Mosquitto::AT_LEAST_ONCE, false)
- client1.connect(TEST_HOST, TEST_PORT, 10)
+ client1.connect(TEST_HOST, TEST_PORT, TIMEOUT)
assert client1.subscribe(nil, "a/b/c", Mosquitto::AT_LEAST_ONCE)
sleep 1
@result = nil
expected = "should not get anything on publish only after the subscribe"
client1.on_disconnect do |rc|
assert @client.publish(nil, "a/b/c", expected, Mosquitto::AT_LEAST_ONCE, false)
- client1.connect(TEST_HOST, TEST_PORT, 10)
+ client1.connect(TEST_HOST, TEST_PORT, TIMEOUT)
end
client1.disconnect
sleep 1
assert_nil @result
+ client1.loop_stop(true)
end
def test_retain
# publish message with retain
@result = nil
@@ -530,11 +544,11 @@
client1.logger = Logger.new(STDOUT)
client1.loop_start
client1.on_message do |msg|
result = msg.to_s
end
- client1.connect(TEST_HOST, TEST_PORT, 10)
+ client1.connect(TEST_HOST, TEST_PORT, TIMEOUT)
client1.wait_readable
assert client1.subscribe(nil, "a/b/c", Mosquitto::AT_LEAST_ONCE)
wait{ result }
@@ -545,28 +559,40 @@
sleep 1
result = nil
# clear retained message
assert @client.publish(nil, "a/b/c", "", Mosquitto::AT_LEAST_ONCE, true)
+
+ client1.loop_stop(true)
end
def test_lwt
- assert @client.subscribe(nil, "will/topic", Mosquitto::AT_MOST_ONCE)
+ connected = false
+ assert @client.subscribe(nil, "will/topic", Mosquitto::AT_LEAST_ONCE)
+ sleep 1
+
will = "This is an LWT"
client1 = Mosquitto::Client.new("test_lwt")
client1.logger = Logger.new(STDOUT)
client1.loop_start
+ sleep 1
client1.will_set("will/topic", will, Mosquitto::AT_LEAST_ONCE, false)
+ sleep 1
client1.on_connect do |rc|
- client1.disconnect
- client1.loop_stop(true)
+ connected = true
end
- client1.connect(TEST_HOST, TEST_PORT, 10)
+ client1.connect(TEST_HOST, TEST_PORT, TIMEOUT)
- client1.wait_readable
+ wait{ connected }
- @result = nil
+ sleep 5
+
+ IO.for_fd(client1.socket).close
+ client1.destroy
+
+ sleep 5
+
wait{ @result }
assert_equal will, @result
end
end
\ No newline at end of file