delay_agent_spec.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. require 'rails_helper'
  2. describe Agents::DelayAgent do
  3. let(:agent) {
  4. Agents::DelayAgent.create!(
  5. name: 'My DelayAgent',
  6. user: users(:bob),
  7. options: default_options.merge('max_events' => 2),
  8. sources: [agents(:bob_website_agent)]
  9. )
  10. }
  11. let(:default_options) { Agents::DelayAgent.new.default_options }
  12. def create_event(value)
  13. Event.create!(payload: { value: }, agent: agents(:bob_website_agent))
  14. end
  15. let(:first_event) { create_event("one") }
  16. let(:second_event) { create_event("two") }
  17. let(:third_event) { create_event("three") }
  18. describe "#working?" do
  19. it "checks if events have been received within expected receive period" do
  20. expect(agent).not_to be_working
  21. Agents::DelayAgent.async_receive agent.id, [events(:bob_website_agent_event).id]
  22. expect(agent.reload).to be_working
  23. the_future = (agent.options[:expected_receive_period_in_days].to_i + 1).days.from_now
  24. allow(Time).to receive(:now) { the_future }
  25. expect(agent.reload).not_to be_working
  26. end
  27. end
  28. describe "validation" do
  29. before do
  30. expect(agent).to be_valid
  31. end
  32. it "should validate max_events" do
  33. agent.options.delete('max_events')
  34. expect(agent).not_to be_valid
  35. agent.options['max_events'] = ""
  36. expect(agent).not_to be_valid
  37. agent.options['max_events'] = "0"
  38. expect(agent).not_to be_valid
  39. agent.options['max_events'] = "10"
  40. expect(agent).to be_valid
  41. end
  42. it "should validate emit_interval" do
  43. agent.options.delete('emit_interval')
  44. expect(agent).to be_valid
  45. agent.options['emit_interval'] = "0"
  46. expect(agent).to be_valid
  47. agent.options['emit_interval'] = "0.5"
  48. expect(agent).to be_valid
  49. agent.options['emit_interval'] = 0.5
  50. expect(agent).to be_valid
  51. agent.options['emit_interval'] = ''
  52. expect(agent).not_to be_valid
  53. agent.options['emit_interval'] = nil
  54. expect(agent).to be_valid
  55. end
  56. it "should validate presence of expected_receive_period_in_days" do
  57. agent.options['expected_receive_period_in_days'] = ""
  58. expect(agent).not_to be_valid
  59. agent.options['expected_receive_period_in_days'] = 0
  60. expect(agent).not_to be_valid
  61. agent.options['expected_receive_period_in_days'] = -1
  62. expect(agent).not_to be_valid
  63. end
  64. it "should validate keep" do
  65. agent.options.delete('keep')
  66. expect(agent).not_to be_valid
  67. agent.options['keep'] = ""
  68. expect(agent).not_to be_valid
  69. agent.options['keep'] = 'wrong'
  70. expect(agent).not_to be_valid
  71. agent.options['keep'] = 'newest'
  72. expect(agent).to be_valid
  73. agent.options['keep'] = 'oldest'
  74. expect(agent).to be_valid
  75. end
  76. end
  77. describe "#receive" do
  78. it "records Events" do
  79. expect(agent.memory).to be_empty
  80. agent.receive([first_event])
  81. expect(agent.memory).not_to be_empty
  82. agent.receive([second_event])
  83. expect(agent.memory['event_ids']).to eq [first_event.id, second_event.id]
  84. end
  85. it "keeps the newest when 'keep' is set to 'newest'" do
  86. expect(agent.options['keep']).to eq 'newest'
  87. agent.receive([first_event, second_event, third_event])
  88. expect(agent.memory['event_ids']).to eq [second_event.id, third_event.id]
  89. end
  90. it "keeps the oldest when 'keep' is set to 'oldest'" do
  91. agent.options['keep'] = 'oldest'
  92. agent.receive([first_event, second_event, third_event])
  93. expect(agent.memory['event_ids']).to eq [first_event.id, second_event.id]
  94. end
  95. end
  96. describe "#check" do
  97. it "re-emits Events and clears the memory" do
  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(agent).to receive(:sleep).with(0).once
  101. expect {
  102. agent.check
  103. }.to change { agent.events.count }.by(2)
  104. expect(agent.events.take(2).map(&:payload)).to eq [
  105. third_event,
  106. second_event,
  107. ].map(&:payload)
  108. expect(agent.memory['event_ids']).to eq []
  109. end
  110. context "with events_order and emit_interval" do
  111. before do
  112. agent.update!(options: agent.options.merge(
  113. 'events_order' => ['{{ value }}'],
  114. 'emit_interval' => 1,
  115. ))
  116. end
  117. it "re-emits Events in that order and clears the memory with that interval" do
  118. agent.receive([first_event, second_event, third_event])
  119. expect(agent.memory['event_ids']).to eq [second_event.id, third_event.id]
  120. expect(agent).to receive(:sleep).with(1).once
  121. expect {
  122. agent.check
  123. }.to change { agent.events.count }.by(2)
  124. expect(agent.events.take(2).map(&:payload)).to eq [
  125. second_event,
  126. third_event,
  127. ].map(&:payload)
  128. expect(agent.memory['event_ids']).to eq []
  129. end
  130. end
  131. context "with max_emitted_events" do
  132. before do
  133. agent.update!(options: agent.options.merge('max_emitted_events' => 1))
  134. end
  135. it "re-emits max_emitted_events per run" do
  136. agent.receive([first_event, second_event, third_event])
  137. expect(agent.memory['event_ids']).to eq [second_event.id, third_event.id]
  138. expect {
  139. agent.check
  140. }.to change { agent.events.count }.by(1)
  141. expect(agent.events.take.payload).to eq second_event.payload
  142. expect(agent.memory['event_ids']).to eq [third_event.id]
  143. expect {
  144. agent.check
  145. }.to change { agent.events.count }.by(1)
  146. expect(agent.events.take.payload).to eq third_event.payload
  147. expect(agent.memory['event_ids']).to eq []
  148. expect {
  149. agent.check
  150. }.not_to(change { agent.events.count })
  151. end
  152. end
  153. end
  154. end