agent_runner_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. require 'rails_helper'
  2. describe AgentRunner do
  3. context "without traps" do
  4. before do
  5. stub.instance_of(Rufus::Scheduler).every
  6. stub.instance_of(AgentRunner).set_traps
  7. @agent_runner = AgentRunner.new
  8. end
  9. after(:each) do
  10. @agent_runner.stop
  11. AgentRunner.class_variable_set(:@@agents, [])
  12. end
  13. context "#run" do
  14. before do
  15. mock(@agent_runner).run_workers
  16. end
  17. it "runs until stop is called" do
  18. mock.instance_of(Rufus::Scheduler).join
  19. Thread.new { while @agent_runner.instance_variable_get(:@running) != false do sleep 0.1; @agent_runner.stop end }
  20. @agent_runner.run
  21. end
  22. it "handles signals" do
  23. @agent_runner.instance_variable_set(:@signal_queue, ['TERM'])
  24. @agent_runner.run
  25. end
  26. end
  27. context "#load_workers" do
  28. before do
  29. AgentRunner.class_variable_set(:@@agents, [HuginnScheduler, DelayedJobWorker])
  30. end
  31. it "loads all workers" do
  32. workers = @agent_runner.send(:load_workers)
  33. expect(workers).to be_a(Hash)
  34. expect(workers.keys).to eq(['HuginnScheduler', 'DelayedJobWorker'])
  35. end
  36. it "loads only the workers specified in the :only option" do
  37. agent_runner = AgentRunner.new(only: HuginnScheduler)
  38. workers = agent_runner.send(:load_workers)
  39. expect(workers.keys).to eq(['HuginnScheduler'])
  40. agent_runner.stop
  41. end
  42. it "does not load workers specified in the :except option" do
  43. agent_runner = AgentRunner.new(except: HuginnScheduler)
  44. workers = agent_runner.send(:load_workers)
  45. expect(workers.keys).to eq(['DelayedJobWorker'])
  46. agent_runner.stop
  47. end
  48. end
  49. context "running workers" do
  50. before do
  51. AgentRunner.class_variable_set(:@@agents, [HuginnScheduler, DelayedJobWorker])
  52. stub.instance_of(HuginnScheduler).setup
  53. stub.instance_of(DelayedJobWorker).setup
  54. end
  55. context "#run_workers" do
  56. it "runs all the workers" do
  57. mock.instance_of(HuginnScheduler).run!
  58. mock.instance_of(DelayedJobWorker).run!
  59. @agent_runner.send(:run_workers)
  60. end
  61. it "kills no long active workers" do
  62. mock.instance_of(HuginnScheduler).run!
  63. mock.instance_of(DelayedJobWorker).run!
  64. @agent_runner.send(:run_workers)
  65. AgentRunner.class_variable_set(:@@agents, [DelayedJobWorker])
  66. mock.instance_of(HuginnScheduler).stop!
  67. @agent_runner.send(:run_workers)
  68. end
  69. end
  70. context "#restart_dead_workers" do
  71. before do
  72. mock.instance_of(HuginnScheduler).run!
  73. mock.instance_of(DelayedJobWorker).run!
  74. @agent_runner.send(:run_workers)
  75. end
  76. it "restarts dead workers" do
  77. stub.instance_of(HuginnScheduler).thread { OpenStruct.new(alive?: false) }
  78. mock.instance_of(HuginnScheduler).run!
  79. @agent_runner.send(:restart_dead_workers)
  80. end
  81. end
  82. end
  83. end
  84. context "#set_traps" do
  85. it "sets traps for INT TERM and QUIT" do
  86. agent_runner = AgentRunner.new
  87. mock(Signal).trap('INT')
  88. mock(Signal).trap('TERM')
  89. mock(Signal).trap('QUIT')
  90. agent_runner.set_traps
  91. agent_runner.stop
  92. end
  93. end
  94. end