mqtt_agent_spec.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. require 'rails_helper'
  2. require 'mqtt'
  3. require './spec/support/fake_mqtt_server'
  4. describe Agents::MqttAgent do
  5. before :each do
  6. @error_log = StringIO.new
  7. @server = MQTT::FakeServer.new('127.0.0.1')
  8. @server.logger = Logger.new(@error_log)
  9. @server.logger.level = Logger::DEBUG
  10. @server.start
  11. @valid_params = {
  12. 'uri' => "mqtt://#{@server.address}:#{@server.port}",
  13. 'topic' => '/#',
  14. 'max_read_time' => '0.1',
  15. 'expected_update_period_in_days' => "2"
  16. }
  17. @checker = Agents::MqttAgent.new(
  18. :name => "somename",
  19. :options => @valid_params,
  20. :schedule => "midnight",
  21. )
  22. @checker.user = users(:jane)
  23. @checker.save!
  24. end
  25. after :each do
  26. @server.stop
  27. end
  28. describe "#check" do
  29. it "should create events in the initial run" do
  30. expect { @checker.check }.to change { Event.count }.by(2)
  31. end
  32. it "should ignore retained messages that are previously received" do
  33. expect { @checker.check }.to change { Event.count }.by(2)
  34. expect { @checker.check }.to change { Event.count }.by(1)
  35. expect { @checker.check }.to change { Event.count }.by(1)
  36. expect { @checker.check }.to change { Event.count }.by(2)
  37. end
  38. end
  39. describe "#working?" do
  40. it "checks if its generating events as scheduled" do
  41. expect(@checker).not_to be_working
  42. @checker.check
  43. expect(@checker.reload).to be_working
  44. three_days_from_now = 3.days.from_now
  45. stub(Time).now { three_days_from_now }
  46. expect(@checker).not_to be_working
  47. end
  48. end
  49. end