navvy 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ describe 'Navvy::Job' do
4
4
  before do
5
5
  require File.expand_path(File.dirname(__FILE__) + '/../setup/mongo_mapper')
6
6
  end
7
-
7
+
8
8
  describe '.enqueue' do
9
9
  before(:each) do
10
10
  Navvy::Job.delete_all
@@ -15,17 +15,17 @@ describe 'Navvy::Job' do
15
15
  Navvy::Job.count.should == 1
16
16
  end
17
17
 
18
- it 'should set the object and the method' do
18
+ it 'should set the object and the method_name' do
19
19
  Navvy::Job.enqueue(Cow, :speak)
20
20
  job = Navvy::Job.first
21
21
  job.object.should == 'Cow'
22
- job.method.should == :speak
22
+ job.method_name.should == :speak
23
23
  end
24
24
 
25
- it 'should turn the method into a symbol' do
25
+ it 'should turn the method_name into a symbol' do
26
26
  Navvy::Job.enqueue(Cow, 'speak')
27
27
  job = Navvy::Job.first
28
- job.method.should == :speak
28
+ job.method_name.should == :speak
29
29
  end
30
30
 
31
31
  it 'should set the arguments' do
@@ -34,56 +34,129 @@ describe 'Navvy::Job' do
34
34
  job.arguments.should == [true, false]
35
35
  end
36
36
 
37
+ it 'should set the created_at date' do
38
+ Navvy::Job.enqueue(Cow, :speak, true, false)
39
+ job = Navvy::Job.first
40
+ job.created_at.should be_instance_of Time
41
+ job.created_at.should <= Time.now
42
+ job.created_at.should > 10.seconds.ago
43
+ end
44
+
37
45
  it 'should set the run_at date' do
38
46
  Navvy::Job.enqueue(Cow, :speak, true, false)
39
47
  job = Navvy::Job.first
40
48
  job.run_at.should be_instance_of Time
41
49
  job.run_at.should <= Time.now
50
+ job.created_at.should > 10.seconds.ago
51
+ end
52
+
53
+ it 'should return the enqueued job' do
54
+ Navvy::Job.enqueue(Cow, :speak, true, false).
55
+ should be_instance_of Navvy::Job
42
56
  end
43
57
  end
44
58
 
45
59
  describe '.next' do
46
60
  before(:each) do
47
- Navvy::Job.delete_all
61
+ Navvy::Job.delete_all
48
62
  Navvy::Job.create(
49
- :object => 'Cow',
50
- :method => :break,
51
- :failed_at => Time.now
63
+ :object => 'Cow',
64
+ :method_name => :last,
65
+ :created_at => Time.now + 1.day,
66
+ :run_at => Time.now
52
67
  )
53
68
  Navvy::Job.create(
54
- :object => 'Cow',
55
- :method => :tomorrow,
56
- :run_at => Time.now + 1.day
69
+ :object => 'Cow',
70
+ :method_name => :break,
71
+ :completed_at => Time.now,
72
+ :run_at => Time.now
57
73
  )
58
- 12.times do
74
+ Navvy::Job.create(
75
+ :object => 'Cow',
76
+ :method_name => :break,
77
+ :failed_at => Time.now,
78
+ :run_at => Time.now
79
+ )
80
+ Navvy::Job.create(
81
+ :object => 'Cow',
82
+ :method_name => :tomorrow,
83
+ :run_at => Time.now + 1.day
84
+ )
85
+ 120.times do
59
86
  Navvy::Job.enqueue(Cow, :speak)
60
87
  end
61
88
  end
62
89
 
63
90
  it 'should find the next 10 available jobs' do
64
91
  jobs = Navvy::Job.next
65
- jobs.count.should == 10
92
+ jobs.count.should == 100
66
93
  jobs.each do |job|
67
94
  job.should be_instance_of Navvy::Job
68
- job.method.should == :speak
95
+ job.method_name.should == :speak
69
96
  end
70
97
  end
71
-
98
+
72
99
  it 'should find the next 2 available jobs' do
73
100
  Navvy::Job.next(2).count.should == 2
74
101
  end
75
-
102
+
76
103
  it 'should find the next 4 available jobs' do
77
104
  Navvy::Job.limit = 4
78
- Navvy::Job.next
105
+ Navvy::Job.next.count.should == 4
106
+ end
107
+ end
108
+
109
+ describe '.cleanup' do
110
+ before(:each) do
111
+ Navvy::Job.delete_all
112
+ Navvy::Job.create(
113
+ :object => 'Cow',
114
+ :method_name => :speak,
115
+ :completed_at => Time.now - 2.days
116
+ )
117
+ Navvy::Job.create(
118
+ :object => 'Cow',
119
+ :method_name => :speak,
120
+ :completed_at => Time.now
121
+ )
122
+ Navvy::Job.create(
123
+ :object => 'Cow',
124
+ :method_name => :speak
125
+ )
126
+ end
127
+
128
+ it 'should delete all complete jobs when "keep" is false' do
129
+ Navvy::Job.cleanup
130
+ Navvy::Job.count.should == 1
131
+ end
132
+
133
+ it 'should not delete any complete jobs when "keep" is true' do
134
+ Navvy::Job.keep = true
135
+ Navvy::Job.cleanup
136
+ Navvy::Job.count.should == 3
137
+ end
138
+
139
+ it 'should delete all complete jobs where "keep" has passed' do
140
+ Navvy::Job.keep = 1.day
141
+ Navvy::Job.cleanup
142
+ Navvy::Job.count.should == 2
79
143
  end
80
144
  end
81
145
 
82
146
  describe '#run' do
147
+
148
+ it 'should pass the arguments' do
149
+ Navvy::Job.delete_all
150
+ job = Navvy::Job.enqueue(Cow, :name, 'Betsy')
151
+ Cow.should_receive(:name).with('Betsy')
152
+ job.run
153
+ end
154
+
83
155
  describe 'when everything goes well' do
84
156
  before(:each) do
85
157
  Navvy::Job.delete_all
86
158
  Navvy::Job.enqueue(Cow, :speak)
159
+ Navvy::Job.keep = false
87
160
  end
88
161
 
89
162
  it 'should run the job and delete it' do
@@ -91,6 +164,33 @@ describe 'Navvy::Job' do
91
164
  jobs.first.run.should == 'moo'
92
165
  Navvy::Job.count.should == 0
93
166
  end
167
+
168
+ describe 'when Navvy::Job.keep is set' do
169
+ it 'should mark the job as complete when keep is true' do
170
+ Navvy::Job.keep = true
171
+ jobs = Navvy::Job.next
172
+ jobs.first.run
173
+ Navvy::Job.count.should == 1
174
+ jobs.first.started_at.should be_instance_of Time
175
+ jobs.first.completed_at.should be_instance_of Time
176
+ end
177
+
178
+ it 'should mark the job as complete when keep has not passed yer' do
179
+ Navvy::Job.keep = 1.day
180
+ jobs = Navvy::Job.next
181
+ jobs.first.run
182
+ Navvy::Job.count.should == 1
183
+ jobs.first.started_at.should be_instance_of Time
184
+ jobs.first.completed_at.should be_instance_of Time
185
+ end
186
+
187
+ it 'should delete the job when the "keep" flag has passed' do
188
+ Navvy::Job.keep = -1.day
189
+ jobs = Navvy::Job.next
190
+ jobs.first.run
191
+ Navvy::Job.count.should == 0
192
+ end
193
+ end
94
194
  end
95
195
 
96
196
  describe 'when a job fails' do
@@ -104,8 +204,122 @@ describe 'Navvy::Job' do
104
204
  jobs.first.run
105
205
  Navvy::Job.count.should == 1
106
206
  jobs.first.exception.should == 'this method is broken'
207
+ jobs.first.started_at.should be_instance_of Time
107
208
  jobs.first.failed_at.should be_instance_of Time
108
209
  end
109
210
  end
110
211
  end
212
+
213
+ describe '#completed' do
214
+ before(:each) do
215
+ Navvy::Job.delete_all
216
+ Navvy::Job.enqueue(Cow, :speak)
217
+ end
218
+
219
+ it 'should update the jobs completed_at date' do
220
+ jobs = Navvy::Job.next
221
+ jobs.first.completed
222
+ jobs.first.completed_at.should_not be_nil
223
+ end
224
+
225
+ it 'should set the return if provided' do
226
+ jobs = Navvy::Job.next
227
+ jobs.first.completed('woo!')
228
+ jobs.first.return.should == 'woo!'
229
+ end
230
+ end
231
+
232
+ describe '#failed' do
233
+ before(:each) do
234
+ Navvy::Job.delete_all
235
+ Navvy::Job.enqueue(Cow, :speak)
236
+ end
237
+
238
+ it 'should update the jobs failed_at date' do
239
+ jobs = Navvy::Job.next
240
+ jobs.first.failed
241
+ jobs.first.failed_at.should_not be_nil
242
+ end
243
+
244
+ it 'should set the exception message if provided' do
245
+ jobs = Navvy::Job.next
246
+ jobs.first.failed('broken')
247
+ jobs.first.exception.should == 'broken'
248
+ end
249
+ end
250
+
251
+ describe '#ran?' do
252
+ it 'should return false when failed_at? and completed_at? are false' do
253
+ job = Navvy::Job.create
254
+ job.ran?.should be_false
255
+ end
256
+
257
+ it 'should return true when failed_at? or completed_at? is true' do
258
+ [
259
+ Navvy::Job.create(:failed_at => Time.now),
260
+ Navvy::Job.create(:completed_at => Time.now)
261
+ ].each do |job|
262
+ job.ran?.should be_true
263
+ end
264
+ end
265
+ end
266
+
267
+ describe '#duration' do
268
+ it 'should return a duration if started_at and completed_at are set' do
269
+ job = Navvy::Job.create(
270
+ :started_at => 2.seconds.ago,
271
+ :completed_at => Time.now
272
+ )
273
+
274
+ job.duration.should >= 2.seconds
275
+ end
276
+
277
+ it 'should return a duration if started_at and failed_at are set' do
278
+ job = Navvy::Job.create(
279
+ :started_at => 3.seconds.ago,
280
+ :failed_at => Time.now
281
+ )
282
+
283
+ job.duration.should >= 3.seconds
284
+ end
285
+
286
+ it 'should return 0 if only started_at is set' do
287
+ job = Navvy::Job.create(
288
+ :started_at => 4.seconds.ago
289
+ )
290
+
291
+ job.duration.should == 0
292
+ end
293
+ end
294
+
295
+ describe '#args' do
296
+ it 'should return an array of arguments' do
297
+ job = Navvy::Job.enqueue(Cow, :speak, true, false)
298
+ job.args.should be_instance_of Array
299
+ job.args.count.should == 2
300
+ end
301
+ end
302
+
303
+ describe '#status' do
304
+ before(:each) do
305
+ Navvy::Job.delete_all
306
+ end
307
+
308
+ it 'should return :pending' do
309
+ job = Navvy::Job.enqueue(Cow, :speak)
310
+ job.status.should == :pending
311
+ end
312
+
313
+ it 'should return :completed' do
314
+ job = Navvy::Job.enqueue(Cow, :speak)
315
+ job.completed
316
+ job.status.should == :completed
317
+ end
318
+
319
+ it 'should return :failed' do
320
+ job = Navvy::Job.enqueue(Cow, :speak)
321
+ job.failed
322
+ job.status.should == :failed
323
+ end
324
+ end
111
325
  end
@@ -0,0 +1,324 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe 'Navvy::Job' do
4
+ before do
5
+ require File.expand_path(File.dirname(__FILE__) + '/../setup/sequel')
6
+ end
7
+
8
+ describe '.enqueue' do
9
+ before(:each) do
10
+ Navvy::Job.delete
11
+ end
12
+
13
+ it 'should enqueue a job' do
14
+ Navvy::Job.enqueue(Cow, :speak)
15
+ Navvy::Job.count.should == 1
16
+ end
17
+
18
+ it 'should set the object and the method' do
19
+ Navvy::Job.enqueue(Cow, :speak)
20
+ job = Navvy::Job.first
21
+ job.object.should == 'Cow'
22
+ job.method_name.should == 'speak'
23
+ end
24
+
25
+ it 'should turn the method into a symbol' do
26
+ Navvy::Job.enqueue(Cow, 'speak')
27
+ job = Navvy::Job.first
28
+ job.method_name.should == 'speak'
29
+ end
30
+
31
+ it 'should set the arguments' do
32
+ Navvy::Job.enqueue(Cow, :speak, true, false)
33
+ job = Navvy::Job.first
34
+ YAML.load(job.arguments).should == [true, false]
35
+ end
36
+
37
+ it 'should set the created_at date' do
38
+ Navvy::Job.enqueue(Cow, :speak, true, false)
39
+ job = Navvy::Job.first
40
+ job.created_at.should be_instance_of Time
41
+ job.created_at.should <= Time.now
42
+ job.created_at.should > Time.now - 10
43
+ end
44
+
45
+ it 'should set the run_at date' do
46
+ Navvy::Job.enqueue(Cow, :speak, true, false)
47
+ job = Navvy::Job.first
48
+ job.run_at.should be_instance_of Time
49
+ job.run_at.should <= Time.now
50
+ job.created_at.should > Time.now - 10
51
+ end
52
+
53
+ it 'should return the enqueued job' do
54
+ Navvy::Job.enqueue(Cow, :speak, true, false).
55
+ should be_instance_of Navvy::Job
56
+ end
57
+ end
58
+
59
+ describe '.next' do
60
+ before(:each) do
61
+ Navvy::Job.delete
62
+ Navvy::Job.insert(
63
+ :object => 'Cow',
64
+ :method_name => :last.to_s,
65
+ :created_at => Time.now + (60*60*24),
66
+ :run_at => Time.now
67
+ )
68
+ Navvy::Job.insert(
69
+ :object => 'Cow',
70
+ :method_name => :break.to_s,
71
+ :completed_at => Time.now,
72
+ :run_at => Time.now
73
+ )
74
+ Navvy::Job.insert(
75
+ :object => 'Cow',
76
+ :method_name => :break.to_s,
77
+ :failed_at => Time.now,
78
+ :run_at => Time.now
79
+ )
80
+ Navvy::Job.insert(
81
+ :object => 'Cow',
82
+ :method_name => :tomorrow.to_s,
83
+ :run_at => Time.now + (60*60*24)
84
+ )
85
+ 120.times do
86
+ Navvy::Job.enqueue(Cow, :speak)
87
+ end
88
+ end
89
+
90
+ it 'should find the next 10 available jobs' do
91
+ jobs = Navvy::Job.next
92
+ jobs.count.should == 100
93
+ jobs.each do |job|
94
+ job.should be_instance_of Navvy::Job
95
+ job.method_name.should == 'speak'
96
+ end
97
+ end
98
+
99
+ it 'should find the next 2 available jobs' do
100
+ Navvy::Job.next(2).count.should == 2
101
+ end
102
+
103
+ it 'should find the next 4 available jobs' do
104
+ Navvy::Job.limit = 4
105
+ Navvy::Job.next.count.should == 4
106
+ end
107
+ end
108
+
109
+ describe '.cleanup' do
110
+ before(:each) do
111
+ Navvy::Job.delete
112
+ Navvy::Job.create(
113
+ :object => 'Cow',
114
+ :method_name => :speak.to_s,
115
+ :completed_at => Time.now - (60*60*24*2)
116
+ )
117
+ Navvy::Job.create(
118
+ :object => 'Cow',
119
+ :method_name => :speak.to_s,
120
+ :completed_at => Time.now
121
+ )
122
+ Navvy::Job.create(
123
+ :object => 'Cow',
124
+ :method_name => :speak.to_s
125
+ )
126
+ end
127
+
128
+ it 'should delete all complete jobs when "keep" is false' do
129
+ Navvy::Job.cleanup
130
+ Navvy::Job.count.should == 1
131
+ end
132
+
133
+ it 'should not delete any complete jobs when "keep" is true' do
134
+ Navvy::Job.keep = true
135
+ Navvy::Job.cleanup
136
+ Navvy::Job.count.should == 3
137
+ end
138
+
139
+ it 'should delete all complete jobs where "keep" has passed' do
140
+ Navvy::Job.keep = (60*60*24)
141
+ Navvy::Job.cleanup
142
+ Navvy::Job.count.should == 2
143
+ end
144
+ end
145
+
146
+ describe '#run' do
147
+
148
+ it 'should pass the arguments' do
149
+ Navvy::Job.delete
150
+ job = Navvy::Job.enqueue(Cow, :name, 'Betsy')
151
+ Cow.should_receive(:name).with('Betsy')
152
+ job.run
153
+ end
154
+
155
+ describe 'when everything goes well' do
156
+ before(:each) do
157
+ Navvy::Job.delete
158
+ Navvy::Job.enqueue(Cow, :speak)
159
+ Navvy::Job.keep = false
160
+ end
161
+
162
+ it 'should run the job and delete it' do
163
+ jobs = Navvy::Job.next
164
+ jobs.first.run.should == 'moo'
165
+ Navvy::Job.count.should == 0
166
+ end
167
+
168
+ describe 'when Navvy::Job.keep is set' do
169
+ it 'should mark the job as complete when keep is true' do
170
+ Navvy::Job.keep = true
171
+ jobs = Navvy::Job.next
172
+ jobs.first.run
173
+ Navvy::Job.count.should == 1
174
+ jobs.first.started_at.should be_instance_of Time
175
+ jobs.first.completed_at.should be_instance_of Time
176
+ end
177
+
178
+ it 'should mark the job as complete when keep has not passed yer' do
179
+ Navvy::Job.keep = (60*60*24)
180
+ jobs = Navvy::Job.next
181
+ jobs.first.run
182
+ Navvy::Job.count.should == 1
183
+ jobs.first.started_at.should be_instance_of Time
184
+ jobs.first.completed_at.should be_instance_of Time
185
+ end
186
+
187
+ it 'should delete the job when the "keep" flag has passed' do
188
+ Navvy::Job.keep = -(60*60*24)
189
+ jobs = Navvy::Job.next
190
+ jobs.first.run
191
+ Navvy::Job.count.should == 0
192
+ end
193
+ end
194
+ end
195
+
196
+ describe 'when a job fails' do
197
+ before(:each) do
198
+ Navvy::Job.delete
199
+ Navvy::Job.enqueue(Cow, :broken)
200
+ end
201
+
202
+ it 'should store the exception and current time' do
203
+ jobs = Navvy::Job.next
204
+ jobs.first.run
205
+ Navvy::Job.count.should == 1
206
+ jobs.first.exception.should == 'this method is broken'
207
+ jobs.first.started_at.should be_instance_of Time
208
+ jobs.first.failed_at.should be_instance_of Time
209
+ end
210
+ end
211
+ end
212
+
213
+ describe '#completed' do
214
+ before(:each) do
215
+ Navvy::Job.delete
216
+ Navvy::Job.enqueue(Cow, :speak)
217
+ end
218
+
219
+ it 'should update the jobs completed_at date' do
220
+ jobs = Navvy::Job.next
221
+ jobs.first.completed
222
+ jobs.first.completed_at.should_not be_nil
223
+ end
224
+
225
+ it 'should set the return if provided' do
226
+ jobs = Navvy::Job.next
227
+ jobs.first.completed('woo!')
228
+ jobs.first.return.should == 'woo!'
229
+ end
230
+ end
231
+
232
+ describe '#failed' do
233
+ before(:each) do
234
+ Navvy::Job.delete
235
+ Navvy::Job.enqueue(Cow, :speak)
236
+ end
237
+
238
+ it 'should update the jobs failed_at date' do
239
+ jobs = Navvy::Job.next
240
+ jobs.first.failed
241
+ jobs.first.failed_at.should_not be_nil
242
+ end
243
+
244
+ it 'should set the exception message if provided' do
245
+ jobs = Navvy::Job.next
246
+ jobs.first.failed('broken')
247
+ jobs.first.exception.should == 'broken'
248
+ end
249
+ end
250
+
251
+ describe '#ran?' do
252
+ it 'should return false when failed_at? and completed_at? are false' do
253
+ job = Navvy::Job.create
254
+ job.ran?.should be_false
255
+ end
256
+
257
+ it 'should return true when failed_at? or completed_at? is true' do
258
+ [
259
+ Navvy::Job.create(:failed_at => Time.now),
260
+ Navvy::Job.create(:completed_at => Time.now)
261
+ ].each do |job|
262
+ job.ran?.should be_true
263
+ end
264
+ end
265
+ end
266
+
267
+ describe '#duration' do
268
+ it 'should return a duration if started_at and completed_at are set' do
269
+ job = Navvy::Job.create(
270
+ :started_at => (Time.now - 2),
271
+ :completed_at => Time.now
272
+ )
273
+ job.duration.should >= 2
274
+ end
275
+
276
+ it 'should return a duration if started_at and failed_at are set' do
277
+ job = Navvy::Job.create(
278
+ :started_at => (Time.now - 3),
279
+ :failed_at => Time.now
280
+ )
281
+
282
+ job.duration.should >= 2
283
+ end
284
+
285
+ it 'should return 0 if only started_at is set' do
286
+ job = Navvy::Job.create(
287
+ :started_at => (Time.now - 4)
288
+ )
289
+
290
+ job.duration.should == 0
291
+ end
292
+ end
293
+
294
+ describe '#args' do
295
+ it 'should return an array of arguments' do
296
+ job = Navvy::Job.enqueue(Cow, :speak, true, false)
297
+ job.args.should be_instance_of Array
298
+ job.args.count.should == 2
299
+ end
300
+ end
301
+
302
+ describe '#status' do
303
+ before(:each) do
304
+ Navvy::Job.delete
305
+ end
306
+
307
+ it 'should return :pending' do
308
+ job = Navvy::Job.enqueue(Cow, :speak)
309
+ job.status.should == :pending
310
+ end
311
+
312
+ it 'should return :completed' do
313
+ job = Navvy::Job.enqueue(Cow, :speak)
314
+ job.completed
315
+ job.status.should == :completed
316
+ end
317
+
318
+ it 'should return :failed' do
319
+ job = Navvy::Job.enqueue(Cow, :speak)
320
+ job.failed
321
+ job.status.should == :failed
322
+ end
323
+ end
324
+ end