rocketjob 3.4.3 → 3.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +27 -0
  3. data/bin/rocketjob +1 -1
  4. data/lib/rocket_job/active_worker.rb +4 -3
  5. data/lib/rocket_job/cli.rb +13 -12
  6. data/lib/rocket_job/config.rb +17 -13
  7. data/lib/rocket_job/dirmon_entry.rb +88 -91
  8. data/lib/rocket_job/extensions/mongo/logging.rb +8 -4
  9. data/lib/rocket_job/extensions/mongoid/factory.rb +8 -6
  10. data/lib/rocket_job/extensions/rocket_job_adapter.rb +2 -4
  11. data/lib/rocket_job/heartbeat.rb +7 -8
  12. data/lib/rocket_job/job_exception.rb +6 -5
  13. data/lib/rocket_job/jobs/dirmon_job.rb +5 -7
  14. data/lib/rocket_job/jobs/housekeeping_job.rb +3 -2
  15. data/lib/rocket_job/jobs/on_demand_job.rb +104 -0
  16. data/lib/rocket_job/jobs/simple_job.rb +0 -2
  17. data/lib/rocket_job/jobs/upload_file_job.rb +100 -0
  18. data/lib/rocket_job/performance.rb +15 -10
  19. data/lib/rocket_job/plugins/cron.rb +7 -124
  20. data/lib/rocket_job/plugins/document.rb +8 -10
  21. data/lib/rocket_job/plugins/job/callbacks.rb +0 -1
  22. data/lib/rocket_job/plugins/job/logger.rb +0 -1
  23. data/lib/rocket_job/plugins/job/model.rb +15 -20
  24. data/lib/rocket_job/plugins/job/persistence.rb +3 -13
  25. data/lib/rocket_job/plugins/job/state_machine.rb +1 -2
  26. data/lib/rocket_job/plugins/job/throttle.rb +16 -12
  27. data/lib/rocket_job/plugins/job/worker.rb +15 -19
  28. data/lib/rocket_job/plugins/processing_window.rb +2 -2
  29. data/lib/rocket_job/plugins/restart.rb +3 -4
  30. data/lib/rocket_job/plugins/retry.rb +2 -3
  31. data/lib/rocket_job/plugins/singleton.rb +2 -3
  32. data/lib/rocket_job/plugins/state_machine.rb +19 -23
  33. data/lib/rocket_job/rocket_job.rb +4 -5
  34. data/lib/rocket_job/server.rb +35 -41
  35. data/lib/rocket_job/version.rb +2 -2
  36. data/lib/rocket_job/worker.rb +22 -21
  37. data/lib/rocketjob.rb +2 -0
  38. data/test/config/mongoid.yml +2 -2
  39. data/test/config_test.rb +0 -2
  40. data/test/dirmon_entry_test.rb +161 -134
  41. data/test/dirmon_job_test.rb +80 -78
  42. data/test/job_test.rb +0 -2
  43. data/test/jobs/housekeeping_job_test.rb +0 -1
  44. data/test/jobs/on_demand_job_test.rb +59 -0
  45. data/test/jobs/upload_file_job_test.rb +99 -0
  46. data/test/plugins/cron_test.rb +1 -3
  47. data/test/plugins/job/callbacks_test.rb +8 -13
  48. data/test/plugins/job/defaults_test.rb +0 -1
  49. data/test/plugins/job/logger_test.rb +2 -4
  50. data/test/plugins/job/model_test.rb +1 -2
  51. data/test/plugins/job/persistence_test.rb +0 -2
  52. data/test/plugins/job/state_machine_test.rb +0 -2
  53. data/test/plugins/job/throttle_test.rb +6 -8
  54. data/test/plugins/job/worker_test.rb +1 -2
  55. data/test/plugins/processing_window_test.rb +0 -2
  56. data/test/plugins/restart_test.rb +0 -1
  57. data/test/plugins/retry_test.rb +1 -2
  58. data/test/plugins/singleton_test.rb +0 -2
  59. data/test/plugins/state_machine_event_callbacks_test.rb +1 -2
  60. data/test/plugins/state_machine_test.rb +0 -2
  61. data/test/plugins/transaction_test.rb +5 -7
  62. data/test/test_db.sqlite3 +0 -0
  63. data/test/test_helper.rb +2 -1
  64. metadata +42 -36
data/test/config_test.rb CHANGED
@@ -1,12 +1,10 @@
1
1
  require_relative 'test_helper'
2
2
  class ConfigTest < Minitest::Test
3
3
  describe RocketJob::Config do
4
-
5
4
  describe '.config' do
6
5
  it 'support multiple databases' do
7
6
  assert_equal 'rocketjob_test', RocketJob::Job.collection.database.name
8
7
  end
9
8
  end
10
-
11
9
  end
12
10
  end
@@ -2,30 +2,47 @@ require_relative 'test_helper'
2
2
 
3
3
  # Unit Test for RocketJob::Job
4
4
  class DirmonEntryTest < Minitest::Test
5
-
6
- class WithFullFileNameJob < RocketJob::Job
5
+ class TestJob < RocketJob::Job
7
6
  # Dirmon will store the filename in this property when starting the job
8
7
  field :upload_file_name, type: String
9
8
 
9
+ field :user_id, type: Integer
10
+
10
11
  def perform
11
12
  # Do something with the file name stored in :upload_file_name
12
13
  end
13
14
  end
14
15
 
15
- class SumJob < RocketJob::Job
16
- @result = nil
16
+ describe RocketJob::DirmonEntry do
17
+ let :archive_directory do
18
+ '/tmp/archive_directory'
19
+ end
17
20
 
18
- # For temp test data
19
- def self.result
20
- @result
21
+ let :archive_path do
22
+ archive_path = Pathname.new(archive_directory)
23
+ archive_path.mkpath
24
+ archive_path.realdirpath
21
25
  end
22
26
 
23
- def perform(a, b)
24
- @result = a + b
27
+ let :dirmon_entry do
28
+ dirmon_entry = RocketJob::DirmonEntry.new(
29
+ name: 'Test',
30
+ job_class_name: 'DirmonEntryTest::TestJob',
31
+ pattern: 'test/files/**',
32
+ properties: {user_id: 341},
33
+ archive_directory: archive_directory
34
+ )
35
+ dirmon_entry.enable!
36
+ assert dirmon_entry.valid?, dirmon_entry.errors.messages.ai
37
+ dirmon_entry
38
+ end
39
+
40
+ before do
41
+ RocketJob::Jobs::DirmonJob.delete_all
42
+ RocketJob::Jobs::UploadFileJob.delete_all
43
+ RocketJob::DirmonEntry.delete_all
25
44
  end
26
- end
27
45
 
28
- describe RocketJob::DirmonEntry do
29
46
  describe '.config' do
30
47
  it 'support multiple databases' do
31
48
  assert_equal 'rocketjob_test', RocketJob::DirmonEntry.collection.database.name
@@ -83,21 +100,11 @@ class DirmonEntryTest < Minitest::Test
83
100
  end
84
101
 
85
102
  describe '#fail!' do
86
- before do
87
- @dirmon_entry = RocketJob::DirmonEntry.new(job_class_name: 'DirmonEntryTest::WithFullFileNameJob', pattern: 'test/files/**')
88
- @dirmon_entry.enable!
89
- assert @dirmon_entry.valid?, @dirmon_entry.errors.messages.ai
90
- end
91
-
92
- after do
93
- @dirmon_entry.destroy if @dirmon_entry && @dirmon_entry.new_record?
94
- end
95
-
96
103
  it 'fail with message' do
97
- @dirmon_entry.fail!('myworker:2323', 'oh no')
98
- assert_equal true, @dirmon_entry.failed?
99
- assert_equal 'RocketJob::DirmonEntryException', @dirmon_entry.exception.class_name
100
- assert_equal 'oh no', @dirmon_entry.exception.message
104
+ dirmon_entry.fail!('myworker:2323', 'oh no')
105
+ assert dirmon_entry.failed?
106
+ assert_equal 'RocketJob::DirmonEntryException', dirmon_entry.exception.class_name
107
+ assert_equal 'oh no', dirmon_entry.exception.message
101
108
  end
102
109
 
103
110
  it 'fail with exception' do
@@ -107,138 +114,100 @@ class DirmonEntryTest < Minitest::Test
107
114
  rescue Exception => exc
108
115
  exception = exc
109
116
  end
110
- @dirmon_entry.fail!('myworker:2323', exception)
117
+ dirmon_entry.fail!('myworker:2323', exception)
111
118
 
112
- assert_equal true, @dirmon_entry.failed?
113
- assert_equal exception.class.name.to_s, @dirmon_entry.exception.class_name
114
- assert @dirmon_entry.exception.message.include?('undefined local variable or method'), @dirmon_entry.attributes.inspect
119
+ assert_equal true, dirmon_entry.failed?
120
+ assert_equal exception.class.name.to_s, dirmon_entry.exception.class_name
121
+ assert dirmon_entry.exception.message.include?('undefined local variable or method'), dirmon_entry.attributes.inspect
115
122
  end
116
123
  end
117
124
 
118
125
  describe '#validate' do
119
- it 'existance' do
120
- assert entry = RocketJob::DirmonEntry.new(job_class_name: 'DirmonEntryTest::WithFullFileNameJob')
121
- assert_equal false, entry.valid?
122
- assert_equal ["can't be blank"], entry.errors[:pattern], entry.errors.messages.ai
126
+ it 'strip_whitespace' do
127
+ dirmon_entry.pattern = ' test/files/*'
128
+ dirmon_entry.archive_directory = ' test/archive/ '
129
+ assert dirmon_entry.valid?
130
+ assert_equal 'test/files/*', dirmon_entry.pattern
131
+ assert_equal 'test/archive/', dirmon_entry.archive_directory
132
+ end
133
+
134
+ describe 'pattern' do
135
+ it 'present' do
136
+ dirmon_entry.pattern = nil
137
+ refute dirmon_entry.valid?
138
+ assert_equal ["can't be blank"], dirmon_entry.errors[:pattern], dirmon_entry.errors.messages.ai
139
+ end
123
140
  end
124
141
 
125
142
  describe 'job_class_name' do
126
143
  it 'ensure presence' do
127
- assert entry = RocketJob::DirmonEntry.new(pattern: 'test/files/**')
128
- assert_equal false, entry.valid?
129
- assert_equal ["can't be blank", 'job_class_name must be defined and must be derived from RocketJob::Job'], entry.errors[:job_class_name], entry.errors.messages.ai
144
+ dirmon_entry.job_class_name = nil
145
+ refute dirmon_entry.valid?
146
+ assert_equal ["can't be blank"], dirmon_entry.errors[:job_class_name], dirmon_entry.errors.messages.ai
130
147
  end
131
- end
132
- end
133
148
 
134
- describe 'with valid entry' do
135
- before do
136
- RocketJob::Jobs::DirmonJob.delete_all
137
- @archive_directory = '/tmp/archive_directory'
138
- @archive_path = Pathname.new(@archive_directory)
139
- @archive_path.mkpath
140
- @archive_path = @archive_path.realdirpath
141
- @entry = RocketJob::DirmonEntry.new(
142
- pattern: 'test/files/**/*',
143
- job_class_name: 'RocketJob::Jobs::DirmonJob',
144
- properties: {priority: 23},
145
- archive_directory: @archive_directory
146
- )
147
- @job = DirmonEntryTest::WithFullFileNameJob.new(
148
- @entry.properties
149
- )
150
- @file = Tempfile.new('archive')
151
- @file_name = @file.path
152
- @pathname = Pathname.new(@file_name)
153
- File.open(@file_name, 'w') { |file| file.write('Hello World') }
154
- assert File.exist?(@file_name)
155
- @archive_real_name = @archive_path.join("#{@job.id}_#{File.basename(@file_name)}").to_s
156
- end
149
+ it 'is a RocketJob::Job' do
150
+ dirmon_entry.job_class_name = 'String'
151
+ refute dirmon_entry.valid?
152
+ assert_equal ['Job String must be defined and inherit from RocketJob::Job'], dirmon_entry.errors[:job_class_name], dirmon_entry.errors.messages.ai
153
+ end
157
154
 
158
- after do
159
- @file.delete if @file
160
- RocketJob::Jobs::DirmonJob.delete_all
155
+ it 'is invalid' do
156
+ dirmon_entry.job_class_name = 'Blah'
157
+ refute dirmon_entry.valid?
158
+ assert_equal ['Job Blah must be defined and inherit from RocketJob::Job'], dirmon_entry.errors[:job_class_name], dirmon_entry.errors.messages.ai
159
+ end
161
160
  end
162
161
 
163
- describe '#archive_pathname' do
164
- it 'with archive directory' do
165
- assert_equal File.dirname(@archive_real_name), @entry.archive_pathname(@pathname).to_s
162
+ describe 'properties' do
163
+ it 'are valid' do
164
+ dirmon_entry.properties = {user_id: 123}
165
+ assert dirmon_entry.valid?, dirmon_entry.errors.messages.ai
166
166
  end
167
167
 
168
- it 'without archive directory' do
169
- @entry.archive_directory = nil
170
- assert @entry.archive_pathname(@pathname).to_s.end_with?('_archive')
168
+ it 'not valid' do
169
+ dirmon_entry.properties = {blah: 123}
170
+ refute dirmon_entry.valid?
171
+ assert_equal ['Unknown Property: Attempted to set a value for :blah which is not allowed on the job DirmonEntryTest::TestJob'], dirmon_entry.errors[:properties], dirmon_entry.errors.messages.ai
171
172
  end
172
173
  end
174
+ end
173
175
 
174
- describe '#archive_file' do
175
- it 'archive file' do
176
- assert_equal @archive_real_name, @entry.send(:archive_file, @job, Pathname.new(@file_name))
177
- assert File.exist?(@archive_real_name)
178
- end
176
+ describe 'with valid entry' do
177
+ let :file do
178
+ file = Tempfile.new('archive')
179
+ File.open(file.path, 'w') { |file| file.write('Hello World') }
180
+ file
179
181
  end
180
182
 
181
- describe '#upload_default' do
182
- it 'sets upload_file_name property' do
183
- @entry = RocketJob::DirmonEntry.new(
184
- pattern: 'test/files/**/*',
185
- job_class_name: 'DirmonEntryTest::WithFullFileNameJob',
186
- archive_directory: @archive_directory
187
- )
188
- assert @entry.valid?, @entry.errors.messages
189
- job = @entry.job_class.new
190
- @entry.send(:upload_default, job, @pathname)
191
- archive_real_name = @archive_path.join("#{job.id}_#{File.basename(@file_name)}").to_s
192
- assert_equal archive_real_name, job.upload_file_name
193
- end
183
+ let :file_name do
184
+ file.path
194
185
  end
195
186
 
196
- describe '#upload_file' do
197
- it 'upload using #upload' do
198
- @job.define_singleton_method(:upload) do |file_name|
199
- self.description = "FILE:#{file_name}"
200
- end
201
- @entry.send(:upload_file, @job, @pathname)
202
- assert_equal "FILE:#{@file_name}", @job.description
203
- end
187
+ let :pathname do
188
+ Pathname.new(file_name)
204
189
  end
205
190
 
206
- describe '#later' do
207
- it 'enqueues job' do
208
- @entry = RocketJob::DirmonEntry.new(
209
- pattern: 'test/files/**/*',
210
- job_class_name: 'DirmonEntryTest::WithFullFileNameJob',
211
- archive_directory: @archive_directory
212
- )
213
- job = @entry.later(@pathname)
214
- assert_equal Pathname.new(@archive_directory).join("#{job.id}_#{File.basename(@file_name)}").realdirpath.to_s, job.upload_file_name
215
- assert job.queued?
216
- assert_equal 50, job.priority
217
- end
218
-
219
- it 'fails with bad job class name' do
220
- @entry.job_class_name = 'Blah'
221
- assert_raises ArgumentError do
222
- @entry.later(@pathname)
223
- end
224
- end
191
+ after do
192
+ file.delete
193
+ RocketJob::Jobs::DirmonJob.delete_all
225
194
  end
226
195
 
227
196
  describe '#each' do
228
197
  it 'without archive path' do
229
- @entry.archive_directory = nil
230
- files = []
231
- @entry.each do |file_name|
198
+ dirmon_entry.archive_directory = nil
199
+ files = []
200
+ dirmon_entry.each do |file_name|
232
201
  files << file_name
233
202
  end
234
- assert_nil @entry.archive_directory
203
+ assert_nil dirmon_entry.archive_directory
235
204
  assert_equal 1, files.count
236
205
  assert_equal Pathname.new('test/files/text.txt').realpath, files.first
237
206
  end
238
207
 
239
208
  it 'with archive path' do
240
209
  files = []
241
- @entry.each do |file_name|
210
+ dirmon_entry.each do |file_name|
242
211
  files << file_name
243
212
  end
244
213
  assert_equal 1, files.count
@@ -246,9 +215,9 @@ class DirmonEntryTest < Minitest::Test
246
215
  end
247
216
 
248
217
  it 'with case-insensitive pattern' do
249
- @entry.pattern = 'test/files/**/*.TxT'
250
- files = []
251
- @entry.each do |file_name|
218
+ dirmon_entry.pattern = 'test/files/**/*.TxT'
219
+ files = []
220
+ dirmon_entry.each do |file_name|
252
221
  files << file_name
253
222
  end
254
223
  assert_equal 1, files.count
@@ -256,31 +225,89 @@ class DirmonEntryTest < Minitest::Test
256
225
  end
257
226
 
258
227
  it 'reads paths inside of the whitelist' do
259
- @entry.archive_directory = nil
260
- files = []
261
- @entry.stub(:whitelist_paths, [Pathname.new('test/files').realpath.to_s]) do
262
- @entry.each do |file_name|
228
+ dirmon_entry.archive_directory = nil
229
+ files = []
230
+ dirmon_entry.stub(:whitelist_paths, [Pathname.new('test/files').realpath.to_s]) do
231
+ dirmon_entry.each do |file_name|
263
232
  files << file_name
264
233
  end
265
234
  end
266
- assert_nil @entry.archive_directory
235
+ assert_nil dirmon_entry.archive_directory
267
236
  assert_equal 1, files.count
268
237
  assert_equal Pathname.new('test/files/text.txt').realpath, files.first
269
238
  end
270
239
 
271
240
  it 'skips paths outside of the whitelist' do
272
- @entry.archive_directory = nil
273
- files = []
274
- @entry.stub(:whitelist_paths, [Pathname.new('test/config').realpath.to_s]) do
275
- @entry.each do |file_name|
241
+ dirmon_entry.archive_directory = nil
242
+ files = []
243
+ dirmon_entry.stub(:whitelist_paths, [Pathname.new('test/config').realpath.to_s]) do
244
+ dirmon_entry.each do |file_name|
276
245
  files << file_name
277
246
  end
278
247
  end
279
- assert_nil @entry.archive_directory
248
+ assert_nil dirmon_entry.archive_directory
280
249
  assert_equal 0, files.count
281
250
  end
282
251
  end
283
- end
284
252
 
253
+ describe '#later' do
254
+ it 'enqueues job' do
255
+ job = dirmon_entry.later(pathname)
256
+ assert created_job = RocketJob::Jobs::UploadFileJob.last
257
+ assert_equal job.id, created_job.id
258
+ assert job.queued?
259
+ end
260
+
261
+ it 'sets attributes' do
262
+ job = dirmon_entry.later(pathname)
263
+
264
+ upload_file_name = Pathname.new(archive_directory).join("#{job.job_id}_#{File.basename(file_name)}").realdirpath.to_s
265
+
266
+ assert_equal dirmon_entry.job_class_name, job.job_class_name
267
+ assert_equal dirmon_entry.properties, job.properties
268
+ assert_equal upload_file_name, job.upload_file_name
269
+ assert_equal "#{dirmon_entry.name}: #{pathname.basename}", job.description
270
+ assert_equal pathname.to_s, job.original_file_name
271
+ assert job.job_id
272
+ end
273
+ end
274
+
275
+ describe '#archive_file' do
276
+ it 'moves file to archive dir' do
277
+ job_id = BSON::ObjectId.new
278
+ archive_real_name = archive_path.join("#{job_id}_#{File.basename(file_name)}").to_s
279
+
280
+ assert File.exist?(file_name)
281
+ assert_equal archive_real_name, dirmon_entry.send(:archive_file, job_id, pathname)
282
+ refute File.exist?(file_name)
283
+ assert File.exist?(archive_real_name)
284
+ end
285
+ end
286
+
287
+ describe '#archive_pathname' do
288
+ it 'with fully qualified archive directory' do
289
+ assert_equal archive_path.to_s, dirmon_entry.send(:archive_pathname, pathname).to_s
290
+ end
291
+
292
+ describe 'with relative' do
293
+ let :archive_directory do
294
+ 'my_archive/files'
295
+ end
296
+
297
+ it 'archive directory' do
298
+ archive_dir = File.join(pathname.realdirpath.dirname, archive_directory)
299
+ assert_equal archive_dir, dirmon_entry.send(:archive_pathname, pathname).to_s
300
+ end
301
+ end
302
+
303
+ it 'has a default archive directory' do
304
+ e = RocketJob::DirmonEntry.new(
305
+ pattern: 'test/files/**/*',
306
+ job_class_name: 'RocketJob::Jobs::DirmonJob'
307
+ )
308
+ assert_equal 'archive', e.archive_directory
309
+ end
310
+ end
311
+ end
285
312
  end
286
313
  end
@@ -2,44 +2,51 @@ require_relative 'test_helper'
2
2
 
3
3
  # Unit Test for RocketJob::Job
4
4
  class DirmonJobTest < Minitest::Test
5
- class DirmonTestJob < RocketJob::Job
5
+ class TestJob < RocketJob::Job
6
6
  def perform
7
7
  3645
8
8
  end
9
9
  end
10
10
 
11
11
  describe RocketJob::Jobs::DirmonJob do
12
- before do
13
- RocketJob::Jobs::DirmonJob.delete_all
14
- @dirmon_job = RocketJob::Jobs::DirmonJob.new
15
- @directory = '/tmp/directory'
16
- @archive_directory = '/tmp/archive_directory'
17
- @entry = RocketJob::DirmonEntry.new(
18
- pattern: "#{@directory}/abc/*",
19
- job_class_name: 'DirmonJobTest::DirmonTestJob',
12
+ let :dirmon_job do
13
+ RocketJob::Jobs::DirmonJob.new
14
+ end
15
+
16
+ let :directory do
17
+ '/tmp/directory'
18
+ end
19
+
20
+ let :archive_directory do
21
+ '/tmp/archive_directory'
22
+ end
23
+
24
+ let :dirmon_entry do
25
+ RocketJob::DirmonEntry.new(
26
+ pattern: "#{directory}/abc/*",
27
+ job_class_name: 'DirmonJobTest::TestJob',
20
28
  properties: {priority: 23},
21
- archive_directory: @archive_directory
29
+ archive_directory: archive_directory
22
30
  )
23
- FileUtils.makedirs("#{@directory}/abc")
24
- FileUtils.makedirs(@archive_directory)
31
+ end
32
+
33
+ before do
34
+ RocketJob::Jobs::DirmonJob.delete_all
35
+ FileUtils.makedirs("#{directory}/abc")
25
36
  end
26
37
 
27
38
  after do
28
- @dirmon_job.delete if @dirmon_job && !@dirmon_job.new_record?
29
- FileUtils.remove_dir(@archive_directory, true) if Dir.exist?(@archive_directory)
30
- FileUtils.remove_dir(@directory, true) if Dir.exist?(@directory)
39
+ FileUtils.remove_dir(archive_directory, true) if Dir.exist?(archive_directory)
40
+ FileUtils.remove_dir(directory, true) if Dir.exist?(directory)
31
41
  end
32
42
 
33
43
  describe '#check_file' do
34
44
  it 'check growing file' do
35
45
  previous_size = 5
36
46
  new_size = 10
37
- file = Tempfile.new('check_file')
38
- file_name = file.path
39
- File.open(file_name, 'w') { |file| file.write('*' * new_size) }
40
- assert_equal new_size, File.size(file_name)
41
- result = @entry.stub(:later, nil) do
42
- @dirmon_job.send(:check_file, @entry, file, previous_size)
47
+ file = create_temp_file(new_size)
48
+ result = dirmon_entry.stub(:later, nil) do
49
+ dirmon_job.send(:check_file, dirmon_entry, file, previous_size)
43
50
  end
44
51
  assert_equal new_size, result
45
52
  end
@@ -47,13 +54,10 @@ class DirmonJobTest < Minitest::Test
47
54
  it 'check completed file' do
48
55
  previous_size = 10
49
56
  new_size = 10
50
- file = Tempfile.new('check_file')
51
- file_name = file.path
52
- File.open(file_name, 'w') { |file| file.write('*' * new_size) }
53
- assert_equal new_size, File.size(file_name)
54
- started = false
55
- result = @entry.stub(:later, -> fn { started = true }) do
56
- @dirmon_job.send(:check_file, @entry, file, previous_size)
57
+ file = create_temp_file(new_size)
58
+ started = false
59
+ result = dirmon_entry.stub(:later, ->(_fn) { started = true }) do
60
+ dirmon_job.send(:check_file, dirmon_entry, file, previous_size)
57
61
  end
58
62
  assert_nil result
59
63
  assert started
@@ -63,7 +67,7 @@ class DirmonJobTest < Minitest::Test
63
67
  previous_size = 5
64
68
  file_name = Pathname.new('blah')
65
69
  assert_raises Errno::ENOENT do
66
- @dirmon_job.send(:check_file, @entry, file_name, previous_size)
70
+ dirmon_job.send(:check_file, dirmon_entry, file_name, previous_size)
67
71
  end
68
72
  end
69
73
  end
@@ -71,104 +75,94 @@ class DirmonJobTest < Minitest::Test
71
75
  describe '#check_directories' do
72
76
  before do
73
77
  RocketJob::DirmonEntry.destroy_all
74
- @entry.enable!
75
- end
76
-
77
- after do
78
- @entry.destroy if @entry
78
+ dirmon_entry.enable!
79
79
  end
80
80
 
81
81
  it 'no files' do
82
- result = @dirmon_job.send(:check_directories)
82
+ result = dirmon_job.send(:check_directories)
83
83
  assert_equal 0, result.count
84
84
  end
85
85
 
86
86
  it 'collect new files without enqueuing them' do
87
- create_file("#{@directory}/abc/file1", 5)
88
- create_file("#{@directory}/abc/file2", 10)
87
+ create_file("#{directory}/abc/file1", 5)
88
+ create_file("#{directory}/abc/file2", 10)
89
89
 
90
- result = @dirmon_job.send(:check_directories)
91
- assert_equal 2, result.count, result
92
- assert_equal 5, result.values.first, result
93
- assert_equal 10, result.values.second, result
90
+ result = dirmon_job.send(:check_directories)
91
+ assert_equal [5, 10], result.values.sort
94
92
  end
95
93
 
96
94
  it 'allow files to grow' do
97
- create_file("#{@directory}/abc/file1", 5)
98
- create_file("#{@directory}/abc/file2", 10)
99
- @dirmon_job.send(:check_directories)
100
- create_file("#{@directory}/abc/file1", 10)
101
- create_file("#{@directory}/abc/file2", 15)
102
- result = @dirmon_job.send(:check_directories)
103
- assert_equal 2, result.count, result
104
- assert_equal 10, result.values.first, result
105
- assert_equal 15, result.values.second, result
95
+ create_file("#{directory}/abc/file1", 5)
96
+ create_file("#{directory}/abc/file2", 10)
97
+ dirmon_job.send(:check_directories)
98
+ create_file("#{directory}/abc/file1", 10)
99
+ create_file("#{directory}/abc/file2", 15)
100
+ result = dirmon_job.send(:check_directories)
101
+ assert_equal [10, 15], result.values.sort
106
102
  end
107
103
 
108
104
  it 'start all files' do
109
- create_file("#{@directory}/abc/file1", 5)
110
- create_file("#{@directory}/abc/file2", 10)
111
- files = @dirmon_job.send(:check_directories)
105
+ create_file("#{directory}/abc/file1", 5)
106
+ create_file("#{directory}/abc/file2", 10)
107
+ files = dirmon_job.send(:check_directories)
112
108
  assert_equal 2, files.count, files
113
- assert_equal 2, @dirmon_job.previous_file_names.count, files
109
+ assert_equal 2, dirmon_job.previous_file_names.count, files
114
110
 
115
- # files = @dirmon_job.send(:check_directories)
111
+ # files = dirmon_job.send(:check_directories)
116
112
  # assert_equal 0, files.count, files
117
113
 
118
114
  count = 0
119
- result = RocketJob::DirmonEntry.stub_any_instance(:later, -> path { count += 1 }) do
120
- @dirmon_job.send(:check_directories)
115
+ result = RocketJob::DirmonEntry.stub_any_instance(:later, ->(_path) { count += 1 }) do
116
+ dirmon_job.send(:check_directories)
121
117
  end
122
118
  assert_equal 0, result.count, result
123
119
  assert 2, count
124
120
  end
125
121
 
126
122
  it 'skip files in archive directory' do
127
- @entry.archive_directory = nil
128
- @entry.pattern = "#{@directory}/abc/**/*"
129
- file_pathname = Pathname.new("#{@directory}/abc/file1")
123
+ dirmon_entry.archive_directory = 'archive'
124
+ dirmon_entry.pattern = "#{directory}/abc/**/*"
130
125
 
126
+ file_pathname = Pathname.new("#{directory}/abc/file1")
131
127
  create_file(file_pathname.to_s, 5)
132
- create_file("#{@directory}/abc/file2", 10)
133
- FileUtils.makedirs(@entry.archive_pathname(file_pathname))
134
- create_file("#{@entry.archive_pathname(file_pathname)}/file3", 10)
128
+ create_file("#{directory}/abc/file2", 10)
135
129
 
136
- result = @dirmon_job.send(:check_directories)
130
+ archive_pathname = dirmon_entry.send(:archive_pathname, file_pathname)
131
+ create_file("#{archive_pathname}/file3", 21)
137
132
 
138
- assert_equal 2, result.count, result
139
- assert_equal 5, result.values.first, result
140
- assert_equal 10, result.values.second, result
133
+ result = dirmon_job.send(:check_directories)
134
+ assert_equal [5, 10], result.values.sort
141
135
  end
142
136
  end
143
137
 
144
138
  describe '#perform' do
145
139
  it 'check directories and reschedule' do
146
140
  previous_file_names = {
147
- "#{@directory}/abc/file1" => 5,
148
- "#{@directory}/abc/file2" => 10,
141
+ "#{directory}/abc/file1" => 5,
142
+ "#{directory}/abc/file2" => 10
149
143
  }
150
144
  new_file_names = {
151
- "#{@directory}/abc/file1" => 10,
152
- "#{@directory}/abc/file2" => 10,
145
+ "#{directory}/abc/file1" => 10,
146
+ "#{directory}/abc/file2" => 10
153
147
  }
154
148
  assert_equal 0, RocketJob::Jobs::DirmonJob.count
155
149
  # perform_now does not save the job, just runs it
156
- @dirmon_job = RocketJob::Jobs::DirmonJob.create!(
150
+ dirmon_job = RocketJob::Jobs::DirmonJob.create!(
157
151
  previous_file_names: previous_file_names,
158
152
  priority: 11,
159
153
  check_seconds: 30
160
154
  )
161
155
  RocketJob::Jobs::DirmonJob.stub_any_instance(:check_directories, new_file_names) do
162
- @dirmon_job.perform_now
156
+ dirmon_job.perform_now
163
157
  end
164
- assert @dirmon_job.completed?, @dirmon_job.status.inspect
158
+ assert dirmon_job.completed?, dirmon_job.status.inspect
165
159
  # Job must destroy on complete
166
- assert_equal 0, RocketJob::Jobs::DirmonJob.where(id: @dirmon_job.id).count, -> { RocketJob::Jobs::DirmonJob.all.to_a.ai }
160
+ assert_equal 0, RocketJob::Jobs::DirmonJob.where(id: dirmon_job.id).count, -> { RocketJob::Jobs::DirmonJob.all.to_a.ai }
167
161
 
168
162
  # Must have enqueued another instance to run in the future
169
163
  assert_equal 1, RocketJob::Jobs::DirmonJob.count
170
164
  assert new_dirmon_job = RocketJob::Jobs::DirmonJob.last
171
- refute_equal @dirmon_job.id.to_s, new_dirmon_job.id.to_s
165
+ refute_equal dirmon_job.id.to_s, new_dirmon_job.id.to_s
172
166
  assert new_dirmon_job.run_at
173
167
  assert_equal 11, new_dirmon_job.priority
174
168
  assert_equal 30, new_dirmon_job.check_seconds
@@ -185,7 +179,7 @@ class DirmonJobTest < Minitest::Test
185
179
  check_seconds: 30,
186
180
  destroy_on_complete: false
187
181
  )
188
- RocketJob::Jobs::DirmonJob.stub_any_instance(:check_directories, -> { raise RuntimeError.new('Oh no') }) do
182
+ RocketJob::Jobs::DirmonJob.stub_any_instance(:check_directories, -> { raise 'Oh no' }) do
189
183
  assert_raises RuntimeError do
190
184
  dirmon_job.perform_now
191
185
  end
@@ -208,7 +202,15 @@ class DirmonJobTest < Minitest::Test
208
202
  end
209
203
 
210
204
  def create_file(file_name, size)
211
- File.open(file_name, 'w') { |file| file.write('*' * size) }
205
+ File.open(file_name, 'wb') { |file| file.write('*' * size) }
206
+ end
207
+
208
+ def create_temp_file(size)
209
+ file = Tempfile.new('check_file')
210
+ file_name = file.path
211
+ File.open(file_name, 'wb') { |f| f.write('*' * size) }
212
+ assert_equal size, File.size(file_name)
213
+ file
212
214
  end
213
215
  end
214
216
  end