agent_runner_spec.rb 3.1 KB

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