Skip to content

Commit

Permalink
Merge pull request #324 from fablabbcn/bugfix/only-retry-devices-with…
Browse files Browse the repository at this point in the history
…-orphan

Bugfix/only retry devices with orphan
  • Loading branch information
timcowlishaw authored May 16, 2024
2 parents 772fc39 + 7896218 commit e9cfe1e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/lib/mqtt_messages_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def handle_topic(topic, message, retry_on_nil_device=true)
end

def handle_nil_device(topic, message, retry_on_nil_device)
if !topic.to_s.include?("inventory") && !topic.to_s.include?("bridge")
orphan_device = OrphanDevice.find_by_device_token(device_token(topic))
if !topic.to_s.include?("inventory") && !topic.to_s.include?("bridge") && orphan_device
retry_later(topic, message) if retry_on_nil_device
end
end
Expand Down
19 changes: 17 additions & 2 deletions spec/lib/mqtt_messages_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,14 @@
message_handler.handle_topic(@packet.topic, @hardware_info_packet.payload)
end

it 'defers messages with unknown device tokens if retry flag is true' do
it 'defers messages with unknown device tokens and an orphan device if retry flag is true' do
expect(RetryMQTTMessageJob).to receive(:perform_later).with(@invalid_packet.topic, @invalid_packet.payload)
OrphanDevice.create(device_token: "invalid_device_token")
message_handler.handle_topic(@invalid_packet.topic, @invalid_packet.payload)
end

it 'does not defer messages with unknown device tokens and no orphan device even if retry flag is true' do
expect(RetryMQTTMessageJob).not_to receive(:perform_later).with(@invalid_packet.topic, @invalid_packet.payload)
message_handler.handle_topic(@invalid_packet.topic, @invalid_packet.payload)
end

Expand Down Expand Up @@ -270,15 +276,24 @@
expect(orphan_device.reload.device_handshake).to be true
end

it 'defers messages with unknown device tokens if retry flag is true' do
it 'defers messages with unknown device tokens if retry flag is true and an orphan device exists' do
expect(device.hardware_info["id"]).to eq(47)
expect(RetryMQTTMessageJob).to receive(:perform_later).with(@hardware_info_packet_bad.topic, @hardware_info_packet_bad.payload)
OrphanDevice.create(device_token: "BAD_TOPIC")
message_handler.handle_topic(@hardware_info_packet_bad.topic, @hardware_info_packet_bad.payload)
device.reload
expect(device.hardware_info["id"]).to eq(47)
expect(@hardware_info_packet_bad.payload).to_not eq((device.hardware_info.to_json))
end

it 'does not defer messages with unknown device tokens if retry flag is true and an orphan device does not exist' do
expect(device.hardware_info["id"]).to eq(47)
expect(RetryMQTTMessageJob).not_to receive(:perform_later).with(@hardware_info_packet_bad.topic, @hardware_info_packet_bad.payload)
message_handler.handle_topic(@hardware_info_packet_bad.topic, @hardware_info_packet_bad.payload)
device.reload
expect(device.hardware_info["id"]).to eq(47)
expect(@hardware_info_packet_bad.payload).to_not eq((device.hardware_info.to_json))
end

it 'does not defers messages with unknown device tokens from the bridge even if retry flag is true' do
expect(device.hardware_info["id"]).to eq(47)
Expand Down

0 comments on commit e9cfe1e

Please sign in to comment.