digest_agent_spec.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. require "rails_helper"
  2. describe Agents::DigestAgent do
  3. before do
  4. @checker = Agents::DigestAgent.new(:name => "something", :options => { :expected_receive_period_in_days => "2", :retained_events => "0", :message => "{{ events | map:'data' | join:';' }}" })
  5. @checker.user = users(:bob)
  6. @checker.save!
  7. end
  8. describe "#working?" do
  9. it "checks to see if the Agent has received any events in the last 'expected_receive_period_in_days' days" do
  10. event = Event.new
  11. event.agent = agents(:bob_rain_notifier_agent)
  12. event.payload = { :data => "event" }
  13. event.save!
  14. expect(@checker).not_to be_working # no events have ever been received
  15. @checker.options[:expected_receive_period_in_days] = 2
  16. @checker.save!
  17. Agents::DigestAgent.async_receive @checker.id, [event.id]
  18. expect(@checker.reload).to be_working # Events received
  19. three_days_from_now = 3.days.from_now
  20. stub(Time).now { three_days_from_now }
  21. expect(@checker).not_to be_working # too much time has passed
  22. end
  23. end
  24. describe "validation" do
  25. before do
  26. expect(@checker).to be_valid
  27. end
  28. it "should validate retained_events" do
  29. @checker.options[:retained_events] = ""
  30. expect(@checker).to be_valid
  31. @checker.options[:retained_events] = "0"
  32. expect(@checker).to be_valid
  33. @checker.options[:retained_events] = "10"
  34. expect(@checker).to be_valid
  35. @checker.options[:retained_events] = "10000"
  36. expect(@checker).not_to be_valid
  37. @checker.options[:retained_events] = "-1"
  38. expect(@checker).not_to be_valid
  39. end
  40. end
  41. describe "#receive" do
  42. describe "and retained_events is 0" do
  43. before { @checker.options['retained_events'] = 0 }
  44. it "retained_events any payloads it receives" do
  45. event1 = Event.new
  46. event1.agent = agents(:bob_rain_notifier_agent)
  47. event1.payload = { :data => "event1" }
  48. event1.save!
  49. event2 = Event.new
  50. event2.agent = agents(:bob_weather_agent)
  51. event2.payload = { :data => "event2" }
  52. event2.save!
  53. @checker.receive([event1])
  54. @checker.receive([event2])
  55. expect(@checker.memory["queue"]).to eq([event1.id, event2.id])
  56. end
  57. end
  58. describe "but retained_events is 1" do
  59. before { @checker.options['retained_events'] = 1 }
  60. it "retained_eventss only 1 event at a time" do
  61. event1 = Event.new
  62. event1.agent = agents(:bob_rain_notifier_agent)
  63. event1.payload = { :data => "event1" }
  64. event1.save!
  65. event2 = Event.new
  66. event2.agent = agents(:bob_weather_agent)
  67. event2.payload = { :data => "event2" }
  68. event2.save!
  69. @checker.receive([event1])
  70. @checker.receive([event2])
  71. expect(@checker.memory['queue']).to eq([event2.id])
  72. end
  73. end
  74. end
  75. describe "#check" do
  76. describe "and retained_events is 0" do
  77. before { @checker.options['retained_events'] = 0 }
  78. it "should emit a event" do
  79. expect { Agents::DigestAgent.async_check(@checker.id) }.not_to change { Event.count }
  80. event1 = Event.new
  81. event1.agent = agents(:bob_rain_notifier_agent)
  82. event1.payload = { :data => "event" }
  83. event1.save!
  84. event2 = Event.new
  85. event2.agent = agents(:bob_weather_agent)
  86. event2.payload = { :data => "event" }
  87. event2.save!
  88. @checker.receive([event1])
  89. @checker.receive([event2])
  90. @checker.sources << agents(:bob_rain_notifier_agent) << agents(:bob_weather_agent)
  91. @checker.save!
  92. expect { @checker.check }.to change { Event.count }.by(1)
  93. expect(@checker.most_recent_event.payload["events"]).to eq([event1.payload, event2.payload])
  94. expect(@checker.most_recent_event.payload["message"]).to eq("event;event")
  95. expect(@checker.memory['queue']).to be_empty
  96. end
  97. end
  98. describe "but retained_events is 1" do
  99. before { @checker.options['retained_events'] = 1 }
  100. it "should emit a event" do
  101. expect { Agents::DigestAgent.async_check(@checker.id) }.not_to change { Event.count }
  102. event1 = Event.new
  103. event1.agent = agents(:bob_rain_notifier_agent)
  104. event1.payload = { :data => "event" }
  105. event1.save!
  106. event2 = Event.new
  107. event2.agent = agents(:bob_weather_agent)
  108. event2.payload = { :data => "event" }
  109. event2.save!
  110. @checker.receive([event1])
  111. @checker.receive([event2])
  112. @checker.sources << agents(:bob_rain_notifier_agent) << agents(:bob_weather_agent)
  113. @checker.save!
  114. expect { @checker.check }.to change { Event.count }.by(1)
  115. expect(@checker.most_recent_event.payload["events"]).to eq([event2.payload])
  116. expect(@checker.most_recent_event.payload["message"]).to eq("event")
  117. expect(@checker.memory['queue'].length).to eq(1)
  118. end
  119. end
  120. end
  121. end