File: clockwork_test.rb

package info (click to toggle)
ruby-clockwork 2.0.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 332 kB
  • sloc: ruby: 1,583; makefile: 3
file content (85 lines) | stat: -rw-r--r-- 2,323 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require 'clockwork'
require 'minitest/autorun'
require 'mocha/setup'

describe Clockwork do
  before do
    @log_output = StringIO.new
    Clockwork.configure do |config|
      config[:sleep_timeout] = 0
      config[:logger] = Logger.new(@log_output)
    end
    IO.stubs(:select)
  end

  after do
    Clockwork.clear!
  end

  it 'should run events with configured logger' do
    run = false
    Clockwork.handler do |job|
      run = job == 'myjob'
    end
    Clockwork.every(1.minute, 'myjob')
    Clockwork.manager.stubs(:run_tick_loop).returns(Clockwork.manager.tick)
    Clockwork.run

    assert run
    assert @log_output.string.include?('Triggering')
  end

  it 'should log event correctly' do
    run = false
    Clockwork.handler do |job|
      run = job == 'an event'
    end
    Clockwork.every(1.minute, 'an event')
    Clockwork.manager.stubs(:run_tick_loop).returns(Clockwork.manager.tick)
    Clockwork.run
    assert run
    assert @log_output.string.include?("Triggering 'an event'")
  end

  it 'should pass event without modification to handler' do
    event_object = Object.new
    run = false
    Clockwork.handler do |job|
      run = job == event_object
    end
    Clockwork.every(1.minute, event_object)
    Clockwork.manager.stubs(:run_tick_loop).returns(Clockwork.manager.tick)
    Clockwork.run
    assert run
  end

  it 'should not run anything after reset' do
    Clockwork.every(1.minute, 'myjob') {  }
    Clockwork.clear!
    Clockwork.configure do |config|
      config[:sleep_timeout] = 0
      config[:logger] = Logger.new(@log_output)
    end
    Clockwork.manager.stubs(:run_tick_loop).returns(Clockwork.manager.tick)
    Clockwork.run
    assert @log_output.string.include?('0 events')
  end

  it 'should pass all arguments to every' do
    Clockwork.every(1.second, 'myjob', if: lambda { |_| false }) {  }
    Clockwork.manager.stubs(:run_tick_loop).returns(Clockwork.manager.tick)
    Clockwork.run
    assert @log_output.string.include?('1 events')
    assert !@log_output.string.include?('Triggering')
  end

  it 'support module re-open style' do
    $called = false
    module ::Clockwork
      every(1.second, 'myjob') { $called = true }
    end
    Clockwork.manager.stubs(:run_tick_loop).returns(Clockwork.manager.tick)
    Clockwork.run
    assert $called
  end
end