Нет описания http://j1x-huginn.herokuapp.com

long_runnable_spec.rb 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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(:all) 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. @worker.setup!(Rufus::Scheduler.new, Mutex.new)
  37. end
  38. it "calls boolify of the agent" do
  39. mock(@agent).boolify('true') { true }
  40. expect(@worker.boolify('true')).to be_truthy
  41. end
  42. it "expects run to be overriden" do
  43. expect { @worker.run }.to raise_error(StandardError)
  44. end
  45. context "#run!" do
  46. it "runs the agent worker" do
  47. mock(@worker).run
  48. @worker.run!.join
  49. end
  50. it "stops when rescueing a SystemExit" do
  51. mock(@worker).run { raise SystemExit }
  52. mock(@worker).stop!
  53. @worker.run!.join
  54. end
  55. it "creates an agent log entry for a generic exception" do
  56. stub(STDERR).puts
  57. mock(@worker).run { raise "woups" }
  58. mock(@agent).error(/woups/)
  59. @worker.run!.join
  60. end
  61. end
  62. context "#stop!" do
  63. it "terminates the thread" do
  64. mock(@worker.thread).terminate
  65. @worker.stop!
  66. end
  67. it "gracefully stops the worker" do
  68. mock(@worker).stop
  69. @worker.stop!
  70. end
  71. end
  72. context "#restart!" do
  73. it "stops, setups and starts the worker" do
  74. mock(@worker).stop!
  75. mock(@worker).setup!(@worker.scheduler, @worker.mutex)
  76. mock(@worker).run!
  77. @worker.restart!
  78. end
  79. end
  80. context "scheduling" do
  81. it "schedules tasks once" do
  82. mock(@worker.scheduler).send(:schedule_in, 1.hour, tag: 'test1234')
  83. @worker.schedule_in 1.hour do noop; end
  84. end
  85. it "schedules repeating tasks" do
  86. mock(@worker.scheduler).send(:every, 1.hour, tag: 'test1234')
  87. @worker.every 1.hour do noop; end
  88. end
  89. it "allows the cron syntax" do
  90. mock(@worker.scheduler).send(:cron, '0 * * * *', tag: 'test1234')
  91. @worker.cron '0 * * * *' do noop; end
  92. end
  93. end
  94. end
  95. end