mqtt_agent_spec.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. require 'spec_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(41234, '127.0.0.1')
  8. @server.just_one = true
  9. @server.logger = Logger.new(@error_log)
  10. @server.logger.level = Logger::DEBUG
  11. @server.start
  12. @valid_params = {
  13. 'uri' => "mqtt://#{@server.address}:#{@server.port}",
  14. 'topic' => '/#',
  15. 'max_read_time' => '1',
  16. 'expected_update_period_in_days' => "2"
  17. }
  18. @checker = Agents::MqttAgent.new(
  19. :name => "somename",
  20. :options => @valid_params,
  21. :schedule => "midnight",
  22. )
  23. @checker.user = users(:jane)
  24. @checker.save!
  25. end
  26. after :each do
  27. @server.stop
  28. end
  29. describe "#check" do
  30. it "should check that initial run creates an event" do
  31. expect { @checker.check }.to change { Event.count }.by(2)
  32. end
  33. end
  34. describe "#working?" do
  35. it "checks if its generating events as scheduled" do
  36. expect(@checker).not_to be_working
  37. @checker.check
  38. expect(@checker.reload).to be_working
  39. three_days_from_now = 3.days.from_now
  40. stub(Time).now { three_days_from_now }
  41. expect(@checker).not_to be_working
  42. end
  43. end
  44. end