File: sync_performer_test.rb

package info (click to toggle)
ruby-clockwork 1.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 316 kB
  • ctags: 159
  • sloc: ruby: 1,365; makefile: 3
file content (291 lines) | stat: -rw-r--r-- 10,608 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
require "minitest/autorun"
require 'mocha/setup'
require 'time'
require 'active_support/time'

require_relative '../../lib/clockwork'
require_relative '../../lib/clockwork/database_events'
require_relative 'test_helpers'

describe Clockwork::DatabaseEvents::SyncPerformer do
  before do
    @now = Time.now
    DatabaseEventModel.delete_all
    DatabaseEventModel2.delete_all

    Clockwork.manager = @manager = Clockwork::DatabaseEvents::Manager.new
    class << @manager
      def log(msg); end # silence log output
    end
  end

  after do
    Clockwork.clear!
  end

  describe "setup" do
    before do
      @subject = Clockwork::DatabaseEvents::SyncPerformer
    end

    describe "arguments" do
      it 'raises argument error if model is not set' do
        error = assert_raises KeyError do
          @subject.setup(every: 1.minute) {}
        end
        assert_equal error.message, ":model must be set to the model class"
      end

      it 'raises argument error if every is not set' do
        error = assert_raises KeyError do
          @subject.setup(model: DatabaseEventModel) {}
        end
        assert_equal error.message, ":every must be set to the database sync frequency"
      end
    end

    describe "when database reload frequency is greater than model frequency period" do
      before do
        @events_run = []
        @sync_frequency = 1.minute
      end

      it 'fetches and registers event from database' do
        DatabaseEventModel.create(:frequency => 10)
        setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run)

        tick_at(@now, :and_every_second_for => 1.second)

        assert_equal ["DatabaseEventModel:1"], @events_run
      end

      it 'fetches and registers multiple events from database' do
        DatabaseEventModel.create(:frequency => 10)
        DatabaseEventModel.create(:frequency => 10)
        setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run)

        tick_at(@now, :and_every_second_for => 1.second)

        assert_equal ["DatabaseEventModel:1", "DatabaseEventModel:2"], @events_run
      end

      it 'does not run event again before frequency specified in database' do
        model = DatabaseEventModel.create(:frequency => 10)
        setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run)

        tick_at(@now, :and_every_second_for => model.frequency - 1.second)
        assert_equal 1, @events_run.length
      end

      it 'runs event repeatedly with frequency specified in database' do
        model = DatabaseEventModel.create(:frequency => 10)
        setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run)

        tick_at(@now, :and_every_second_for => (2 * model.frequency) + 1.second)

        assert_equal 3, @events_run.length
      end

      it 'runs reloaded events from database repeatedly' do
        model = DatabaseEventModel.create(:frequency => 10)
        setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run)

        tick_at(@now, :and_every_second_for => @sync_frequency - 1)
        model.update(:name => "DatabaseEventModel:1:Reloaded")
        tick_at(@now + @sync_frequency, :and_every_second_for => model.frequency * 2)

        assert_equal ["DatabaseEventModel:1:Reloaded", "DatabaseEventModel:1:Reloaded"], @events_run[-2..-1]
      end

      it 'updates modified event frequency with event reloading' do
        model = DatabaseEventModel.create(:frequency => 10)
        setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run)

        tick_at(@now, :and_every_second_for => @sync_frequency - 1.second)
        model.update(:frequency => 5)
        tick_at(@now + @sync_frequency, :and_every_second_for => 6.seconds)

        # model runs at: 1, 11, 21, 31, 41, 51 (6 runs)
        # database sync happens at: 60
        # modified model runs at: 61 (next tick after reload) and then 66 (2 runs)
        assert_equal 8, @events_run.length
      end

      it 'stoped running deleted events from database' do
        model = DatabaseEventModel.create(:frequency => 10)
        setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run)

        tick_at(@now, :and_every_second_for => (@sync_frequency - 1.second))
        before = @events_run.dup
        model.delete!
        tick_at(@now + @sync_frequency, :and_every_second_for => @sync_frequency)
        after = @events_run

        assert_equal before, after
      end

      it 'updates event name with new name' do
        model = DatabaseEventModel.create(:frequency => 10.seconds)
        setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run)

        tick_at @now, :and_every_second_for => @sync_frequency - 1.second
        @events_run.clear
        model.update(:name => "DatabaseEventModel:1_modified")
        tick_at @now + @sync_frequency, :and_every_second_for => (model.frequency * 2)

        assert_equal ["DatabaseEventModel:1_modified", "DatabaseEventModel:1_modified"], @events_run
      end

      it 'updates event frequency with new frequency' do
        model = DatabaseEventModel.create(:frequency => 10)
        setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run)

        tick_at @now, :and_every_second_for => @sync_frequency - 1.second
        @events_run.clear
        model.update(:frequency => 30)
        tick_at @now + @sync_frequency, :and_every_second_for => @sync_frequency - 1.seconds

        assert_equal 2, @events_run.length
      end

      it 'updates event at with new at' do
        model = DatabaseEventModel.create(:frequency => 1.day, :at => '10:30')
        setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run)

        assert_will_run 'jan 1 2010 10:30:00'
        assert_wont_run 'jan 1 2010 09:30:00'

        model.update(:at => '09:30')
        tick_at @now, :and_every_second_for => @sync_frequency + 1.second

        assert_will_run 'jan 1 2010 09:30:00'
        assert_wont_run 'jan 1 2010 10:30:00'
      end

      describe "when #name is defined" do
        it 'runs daily event with at from databse only once' do
          DatabaseEventModel.create(:frequency => 1.day, :at => next_minute(@now).strftime('%H:%M'))
          setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run)

          # tick from now, though specified :at time
          tick_at(@now, :and_every_second_for => (2 * @sync_frequency) + 1.second)

          assert_equal 1, @events_run.length
        end
      end

      describe "when #name is not defined" do
        it 'runs daily event with at from databse only once' do
          DatabaseEventModelWithoutName.create(:frequency => 1.day, :at => next_minute(next_minute(@now)).strftime('%H:%M'))
          setup_sync(model: DatabaseEventModelWithoutName, :every => @sync_frequency, :events_run => @events_run)

          # tick from now, though specified :at time
          tick_at(@now, :and_every_second_for => (2 * @sync_frequency) + 1.second)

          assert_equal 1, @events_run.length
        end
      end

      it 'creates multiple event ats with comma separated at string' do
        DatabaseEventModel.create(:frequency => 1.day, :at => '16:20, 18:10')
        setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run)

        tick_at @now, :and_every_second_for => 1.second

        assert_wont_run 'jan 1 2010 16:19:59'
        assert_will_run 'jan 1 2010 16:20:00'
        assert_wont_run 'jan 1 2010 16:20:01'

        assert_wont_run 'jan 1 2010 18:09:59'
        assert_will_run 'jan 1 2010 18:10:00'
        assert_wont_run 'jan 1 2010 18:10:01'
      end

      it 'allows syncing multiple database models' do
        DatabaseEventModel.create(:frequency => 10)
        setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run)

        DatabaseEventModel2.create(:frequency => 10)
        setup_sync(model: DatabaseEventModel2, :every => @sync_frequency, :events_run => @events_run)

        tick_at(@now, :and_every_second_for => 1.second)

        assert_equal ["DatabaseEventModel:1", "DatabaseEventModel2:1"], @events_run
      end
    end

    describe "when database reload frequency is less than model frequency period" do
      before do
        @events_run = []
      end

      it 'runs event only once within the model frequency period' do
        DatabaseEventModel.create(:frequency => 5.minutes)
        setup_sync(model: DatabaseEventModel, :every => 1.minute, :events_run => @events_run)

        tick_at(@now, :and_every_second_for => 5.minutes)

        assert_equal 1, @events_run.length
      end
    end

    describe "with database event :at set to empty string" do
      before do
        @events_run = []

        DatabaseEventModel.create(:frequency => 10)
        setup_sync(model: DatabaseEventModel, :every => 1.minute, :events_run => @events_run)
      end

      it 'does not raise an error' do
        begin
          tick_at(Time.now, :and_every_second_for => 10.seconds)
        rescue => e
          assert false, "Raised an error: #{e.message}"
        end
      end

      it 'runs the event' do
        begin
          tick_at(Time.now, :and_every_second_for => 10.seconds)
        rescue
        end
        assert_equal 1, @events_run.length
      end
    end

    describe "with task that responds to `tz`" do
      before do
        @events_run = []
        @utc_time_now = Time.now.utc

        DatabaseEventModel.create(:frequency => 1.days, :at => @utc_time_now.strftime('%H:%M'), :tz => 'America/Montreal')
        setup_sync(model: DatabaseEventModel, :every => 1.minute, :events_run => @events_run)
      end

      it 'does not raise an error' do
        begin
          tick_at(@utc_time_now, :and_every_second_for => 10.seconds)
        rescue => e
          assert false, "Raised an error: #{e.message}"
        end
      end

      it 'does not run the event based on UTC' do
        begin
          tick_at(@utc_time_now, :and_every_second_for => 3.hours)
        rescue
        end
        assert_equal 0, @events_run.length
      end

      it 'runs the event based on America/Montreal tz' do
        begin
          tick_at(@utc_time_now, :and_every_second_for => 5.hours)
        rescue
        end
        assert_equal 1, @events_run.length
      end
    end
  end
end