long_runnable_spec.rb 4.4 KB

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