agent_runner_spec.rb 3.4 KB

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