Skip to content

Commit

Permalink
add test for lwt/birth
Browse files Browse the repository at this point in the history
  • Loading branch information
sidoh committed Mar 24, 2019
1 parent 06aefc8 commit 0956246
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
4 changes: 2 additions & 2 deletions dist/index.html.gz.h

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions test/remote/espmh.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ ESPMH_HOSTNAME=milight-hub-test

# Used to test states, etc.
ESPMH_TEST_DEVICE_ID_BASE=0x2200

# MQTT server/auth. Used for MQTT tests
ESPMH_MQTT_SERVER=my-mqtt-server
ESPMH_MQTT_USERNAME=username
ESPMH_MQTT_PASSWORD=password
ESPMH_MQTT_TOPIC_PREFIX=milight_test/
3 changes: 0 additions & 3 deletions test/remote/settings.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
"packet_repeats": 50,
"http_repeat_factor": 1,
"auto_restart_period": 0,
"mqtt_server": "",
"mqtt_username": "",
"mqtt_password": "",
"mqtt_topic_pattern": "milight_test/commands/:device_id/:device_type/:group_id",
"mqtt_update_topic_pattern": "milight_test/update/:device_id/:device_type/:group_id",
"mqtt_state_topic_pattern": "milight/states/:device_id/:device_type/:group_id",
Expand Down
64 changes: 64 additions & 0 deletions test/remote/spec/mqtt_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'mqtt'
require 'api_client'

RSpec.describe 'State' do
before(:all) do
@client = ApiClient.new(ENV.fetch('ESPMH_HOSTNAME'), ENV.fetch('ESPMH_TEST_DEVICE_ID_BASE'))
@client.upload_json('/settings', 'settings.json')

@client.put(
'/settings',
mqtt_server: ENV.fetch('ESPMH_MQTT_SERVER'),
mqtt_username: ENV.fetch('ESPMH_MQTT_USERNAME'),
mqtt_password: ENV.fetch('ESPMH_MQTT_PASSWORD'),
)

@topic_prefix = ENV.fetch('ESPMH_MQTT_TOPIC_PREFIX')
end

context 'birth and LWT' do
it 'should send birth and LWT messages when configured' do
lwt_topic = "#{@topic_prefix}lwt"
birth_topic = "#{@topic_prefix}birth"

seen_birth = false
seen_lwt = false

MQTT::Client.connect("mqtt://#{ENV.fetch('ESPMH_MQTT_USERNAME')}:#{ENV.fetch('ESPMH_MQTT_PASSWORD')}@#{ENV.fetch('ESPMH_MQTT_SERVER')}") do |c|
birth_listen_thread = Thread.new do
begin
Timeout.timeout(10) do
c.get(birth_topic)
seen_birth = true
end
rescue Timeout::Error
end
end

lwt_listen_thread = Thread.new do
begin
Timeout.timeout(10) do
c.get(lwt_topic)
seen_lwt = true
end
rescue Timeout::Error
end
end

@client.put(
'/settings',
mqtt_lwt_topic: lwt_topic,
mqtt_lwt_message: 'disconnected',
mqtt_birth_topic: birth_topic
)
@client.post('/system', command: 'restart')

lwt_listen_thread.join
birth_listen_thread.join
end

expect(seen_birth).to be(true)
expect(seen_lwt).to be(true)
end
end
end

0 comments on commit 0956246

Please sign in to comment.