delay_agent_spec.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. require 'rails_helper'
  2. describe Agents::DelayAgent do
  3. let(:agent) do
  4. _agent = Agents::DelayAgent.new(name: 'My DelayAgent')
  5. _agent.options = _agent.default_options.merge('max_events' => 2)
  6. _agent.user = users(:bob)
  7. _agent.sources << agents(:bob_website_agent)
  8. _agent.save!
  9. _agent
  10. end
  11. def create_event
  12. _event = Event.new(payload: { random: rand })
  13. _event.agent = agents(:bob_website_agent)
  14. _event.save!
  15. _event
  16. end
  17. let(:first_event) { create_event }
  18. let(:second_event) { create_event }
  19. let(:third_event) { create_event }
  20. describe "#working?" do
  21. it "checks if events have been received within expected receive period" do
  22. expect(agent).not_to be_working
  23. Agents::DelayAgent.async_receive agent.id, [events(:bob_website_agent_event).id]
  24. expect(agent.reload).to be_working
  25. the_future = (agent.options[:expected_receive_period_in_days].to_i + 1).days.from_now
  26. stub(Time).now { the_future }
  27. expect(agent.reload).not_to be_working
  28. end
  29. end
  30. describe "validation" do
  31. before do
  32. expect(agent).to be_valid
  33. end
  34. it "should validate max_events" do
  35. agent.options.delete('max_events')
  36. expect(agent).not_to be_valid
  37. agent.options['max_events'] = ""
  38. expect(agent).not_to be_valid
  39. agent.options['max_events'] = "0"
  40. expect(agent).not_to be_valid
  41. agent.options['max_events'] = "10"
  42. expect(agent).to be_valid
  43. end
  44. it "should validate presence of expected_receive_period_in_days" do
  45. agent.options['expected_receive_period_in_days'] = ""
  46. expect(agent).not_to be_valid
  47. agent.options['expected_receive_period_in_days'] = 0
  48. expect(agent).not_to be_valid
  49. agent.options['expected_receive_period_in_days'] = -1
  50. expect(agent).not_to be_valid
  51. end
  52. it "should validate keep" do
  53. agent.options.delete('keep')
  54. expect(agent).not_to be_valid
  55. agent.options['keep'] = ""
  56. expect(agent).not_to be_valid
  57. agent.options['keep'] = 'wrong'
  58. expect(agent).not_to be_valid
  59. agent.options['keep'] = 'newest'
  60. expect(agent).to be_valid
  61. agent.options['keep'] = 'oldest'
  62. expect(agent).to be_valid
  63. end
  64. end
  65. describe "#receive" do
  66. it "records Events" do
  67. expect(agent.memory).to be_empty
  68. agent.receive([first_event])
  69. expect(agent.memory).not_to be_empty
  70. agent.receive([second_event])
  71. expect(agent.memory['event_ids']).to eq [first_event.id, second_event.id]
  72. end
  73. it "keeps the newest when 'keep' is set to 'newest'" do
  74. expect(agent.options['keep']).to eq 'newest'
  75. agent.receive([first_event, second_event, third_event])
  76. expect(agent.memory['event_ids']).to eq [second_event.id, third_event.id]
  77. end
  78. it "keeps the oldest when 'keep' is set to 'oldest'" do
  79. agent.options['keep'] = 'oldest'
  80. agent.receive([first_event, second_event, third_event])
  81. expect(agent.memory['event_ids']).to eq [first_event.id, second_event.id]
  82. end
  83. end
  84. describe "#check" do
  85. it "re-emits Events and clears the memory" do
  86. agent.receive([first_event, second_event, third_event])
  87. expect(agent.memory['event_ids']).to eq [second_event.id, third_event.id]
  88. expect {
  89. agent.check
  90. }.to change { agent.events.count }.by(2)
  91. events = agent.events.reorder('events.id desc')
  92. expect(events.first.payload).to eq third_event.payload
  93. expect(events.second.payload).to eq second_event.payload
  94. expect(agent.memory['event_ids']).to eq []
  95. end
  96. it "re-emits max_emitted_events and clears just them from the memory" do
  97. agent.options['max_emitted_events'] = 1
  98. agent.receive([first_event, second_event, third_event])
  99. expect(agent.memory['event_ids']).to eq [second_event.id, third_event.id]
  100. expect {
  101. agent.check
  102. }.to change { agent.events.count }.by(1)
  103. events = agent.events.reorder('events.id desc')
  104. expect(agent.memory['event_ids']).to eq [third_event.id]
  105. expect(events.first.payload).to eq second_event.payload
  106. end
  107. end
  108. end