long_runnable_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. require 'rails_helper'
  2. describe LongRunnable do
  3. class LongRunnableAgent < Agent
  4. include LongRunnable
  5. def default_options
  6. {test: 'test'}
  7. end
  8. end
  9. before(:each) do
  10. @agent = LongRunnableAgent.new
  11. end
  12. it "start_worker? defaults to true" do
  13. expect(@agent.start_worker?).to be_truthy
  14. end
  15. it "should build the worker_id" do
  16. expect(@agent.worker_id).to eq('LongRunnableAgent--bf21a9e8fbc5a3846fb05b4fa0859e0917b2202f')
  17. end
  18. context "#setup_worker" do
  19. it "returns active agent workers" do
  20. mock(LongRunnableAgent).active { [@agent] }
  21. workers = LongRunnableAgent.setup_worker
  22. expect(workers.length).to eq(1)
  23. expect(workers.first).to be_a(LongRunnableAgent::Worker)
  24. expect(workers.first.agent).to eq(@agent)
  25. end
  26. it "returns an empty array when no agent is active" do
  27. mock(LongRunnableAgent).active { [] }
  28. workers = LongRunnableAgent.setup_worker
  29. expect(workers.length).to eq(0)
  30. end
  31. end
  32. describe LongRunnable::Worker do
  33. before(:each) do
  34. @agent = Object.new
  35. @worker = LongRunnable::Worker.new(agent: @agent, id: 'test1234')
  36. @scheduler = Rufus::Scheduler.new
  37. @worker.setup!(@scheduler, Mutex.new)
  38. end
  39. after(:each) do
  40. @worker.thread.terminate if @worker.thread && !@skip_thread_terminate
  41. @scheduler.shutdown(:wait)
  42. end
  43. it "calls boolify of the agent" do
  44. mock(@agent).boolify('true') { true }
  45. expect(@worker.boolify('true')).to be_truthy
  46. end
  47. it "expects run to be overriden" do
  48. expect { @worker.run }.to raise_error(StandardError)
  49. end
  50. context "#run!" do
  51. it "runs the agent worker" do
  52. mock(@worker).run
  53. @worker.run!.join
  54. end
  55. it "stops when rescueing a SystemExit" do
  56. mock(@worker).run { raise SystemExit }
  57. mock(@worker).stop!
  58. @worker.run!.join
  59. end
  60. it "creates an agent log entry for a generic exception" do
  61. stub(STDERR).puts
  62. mock(@worker).run { raise "woups" }
  63. mock(@agent).error(/woups/)
  64. @worker.run!.join
  65. end
  66. end
  67. context "#stop!" do
  68. it "terminates the thread" do
  69. mock.proxy(@worker).terminate_thread!
  70. @worker.stop!
  71. end
  72. it "gracefully stops the worker" do
  73. mock(@worker).stop
  74. @worker.stop!
  75. end
  76. end
  77. context "#terminate_thread!" do
  78. before do
  79. @skip_thread_terminate = true
  80. mock_thread = Object.new
  81. stub(@worker).thread { mock_thread }
  82. end
  83. it "terminates the thread" do
  84. mock(@worker.thread).terminate
  85. do_not_allow(@worker.thread).wakeup
  86. mock(@worker.thread).status { 'run' }
  87. @worker.terminate_thread!
  88. end
  89. it "wakes up sleeping threads after termination" do
  90. mock(@worker.thread).terminate
  91. mock(@worker.thread).wakeup
  92. mock(@worker.thread).status { 'sleep' }
  93. @worker.terminate_thread!
  94. end
  95. end
  96. context "#restart!" do
  97. it "stops, setups and starts the worker" do
  98. mock(@worker).stop!
  99. mock(@worker).setup!(@worker.scheduler, @worker.mutex)
  100. mock(@worker).run!
  101. mock(@worker).puts(anything) { |text| expect(text).to match(/Restarting/) }
  102. @worker.restart!
  103. end
  104. end
  105. context "scheduling" do
  106. it "schedules tasks once" do
  107. mock(@worker.scheduler).send(:schedule_in, 1.hour, tag: 'test1234')
  108. @worker.schedule_in 1.hour do noop; end
  109. end
  110. it "schedules repeating tasks" do
  111. mock(@worker.scheduler).send(:every, 1.hour, tag: 'test1234')
  112. @worker.every 1.hour do noop; end
  113. end
  114. it "allows the cron syntax" do
  115. mock(@worker.scheduler).send(:cron, '0 * * * *', tag: 'test1234')
  116. @worker.cron '0 * * * *' do noop; end
  117. end
  118. end
  119. end
  120. end