File: error_spec.rb

package info (click to toggle)
ruby-rufus-scheduler 3.8.2-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 524 kB
  • sloc: ruby: 4,324; makefile: 26
file content (139 lines) | stat: -rw-r--r-- 2,520 bytes parent folder | download | duplicates (3)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

#
# Specifying rufus-scheduler
#
# Fri Aug  9 07:10:18 JST 2013
#

require 'spec_helper'


describe Rufus::Scheduler do

  before :each do

    @taoe = Thread.abort_on_exception
    Thread.abort_on_exception = false

    @ose = $stderr
    $stderr = StringIO.new

    @scheduler = Rufus::Scheduler.new
  end

  after :each do

    @scheduler.shutdown

    Thread.abort_on_exception = @taoe

    $stderr = @ose
  end

  context 'error in block' do

    it 'intercepts the error and describes it on $stderr' do

      counter = 0

      @scheduler.every('0.5s') do
        counter += 1
        fail 'argh'
      end

      sleep 2

      expect(counter).to be > 2
      expect($stderr.string).to match(/argh/)
    end
  end

  context 'error in callable' do

    class MyFailingHandler
      attr_reader :counter
      def initialize
        @counter = 0
      end
      def call(job, time)
        @counter = @counter + 1
        fail 'ouch'
      end
    end

    it 'intercepts the error and describes it on $stderr' do

      mfh = MyFailingHandler.new

      @scheduler.every('0.5s', mfh)

      sleep 2

      expect(mfh.counter).to be > 2
      expect($stderr.string).to match(/ouch/)
    end
  end

  context 'Rufus::Scheduler#stderr=' do

    it 'lets divert error information to custom files' do

      @scheduler.stderr = StringIO.new

      @scheduler.in('0s') do
        fail 'miserably'
      end

      sleep 0.5

      expect(@scheduler.stderr.string).to match(/intercepted an error/)
      expect(@scheduler.stderr.string).to match(/miserably/)
    end
  end

  context 'error information' do

    it 'contains information about the error, the job and the scheduler' do

      @scheduler.stderr = StringIO.new

      @scheduler.in('0s') do
        fail 'miserably'
      end

      sleep 0.5

      s = @scheduler.stderr.string
      #puts s

      expect(s).to match(/ENV\['TZ'\]:/)
      expect(s).to match(/down\?: false/)
      expect(s).to match(/work_threads: 1/)
      expect(s).to match(/running_jobs: 1/)
      expect(s).to match(/uptime: \d/)
    end
  end

  context 'Rufus::Scheduler#on_error(&block)' do

    it 'intercepts all StandardError instances' do

      $message = nil

      def @scheduler.on_error(job, err)
        $message = "#{job.class} #{job.original} #{err.message}"
      rescue
        p $!
      end

      @scheduler.in('0s') do
        fail 'miserably'
      end

      sleep 0.5

      expect($message).to eq('Rufus::Scheduler::InJob 0s miserably')
    end
  end
end