/
juggler_spec.rb
152 lines (121 loc) · 4.26 KB
/
juggler_spec.rb
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
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe Juggler do
before :all do
Juggler.configure(
'access_key_id' => '0XZN91V3MBE44A05R2R2',
'secret_access_key' => 'WKVJffwxdw+JNzuRTYfj0SmCMgemiFGnIc+KEYRB',
'queue_bucket_name' => "queue.juggler.development",
'processed_bucket_name' => "processed.juggler.development"
)
end
def mock_s3_object(overrides={})
mock(AWS::S3::S3Object, {
:path => '/path/to/file', :rename => nil, :delete => nil, :value => 'val'
}.merge(overrides))
end
before :each do
@bucket = mock(AWS::S3::Bucket, :objects => [ ], :[] => nil)
AWS::S3::Bucket.stub!(:find).
and_return(@bucket)
AWS::S3::S3Object.stub!(:store)
@s3_object = mock_s3_object
Juggler.queue_bucket.stub!(:objects).and_return([ @s3_object ])
Juggler.queue_bucket.stub!(:[]).and_return(@s3_object)
end
describe Juggler::Worker do
it "should poll for whether there is a file in the queue" do
Juggler.queue_bucket.should_receive(:objects)
Juggler::Worker.run
end
context "when there is a file in the queue" do
it "should process the file" do
Juggler::Job.should_receive(:run).with(@s3_object)
Juggler::Worker.run
end
end
context "when all the files in the queue are locked" do
before :each do
@locked_s3_object = mock_s3_object(:path => '/path/to/---LOCKED---file')
Juggler.queue_bucket.stub!(:objects).and_return([ @locked_s3_object ])
end
it "shouldn't process the files" do
Juggler::Job.should_not_receive(:run).with(@locked_s3_object)
Juggler::Worker.run
end
end
end
describe Juggler::Job do
before :each do
@s3_object = mock_s3_object(:path => "/the_bucket/filename")
Juggler.queue_bucket.stub!(:[]).and_return(@s3_object)
class TestProcessor < Juggler::Processor
def run(io)
progress 0.5
set 'test123', { 'key' => 'value' }
return StringIO.new("FOOBAR")
end
end
Juggler.processor = TestProcessor
end
it "should have set the data object according to the #put call in the processor" do
job = Juggler::Job.run(@s3_object)
job.data['test123'].should == { 'key' => 'value' }
end
it "should reflect the appropriate progress status" do
job = Juggler::Job.run(@s3_object)
job.progress.should == 0.5
end
context "and the file already has the lock prefix" do
before :each do
@s3_object.stub!(:path).and_return("---LOCKED---foobar")
end
it "should skip processing" do
Juggler::Job.run(@s3_object)
end
end
it "should prepend a lock prefix to the object path" do
@s3_object.should_receive(:rename).with("---LOCKED---filename")
Juggler::Job.run(@s3_object)
end
it "should process the file through the processor" do
AWS::S3::S3Object.should_receive(:store).
with(Digest::SHA1.hexdigest("FOOBAR"), anything, Juggler.config['processed_bucket_name'])
Juggler::Job.run(@s3_object)
end
it "should have an initial status of 'queued'" do
job = Juggler::Job.new(@s3_object)
job.status.should == 'queued'
end
it "should mark the job as 'completed' when completed successfully" do
job = Juggler::Job.run(@s3_object)
job.status.should == 'completed'
end
context "when the job fails" do
before :each do
@job = Juggler::Job.new(@s3_object)
@job.stub!(:perform).and_return(false)
end
it "should mark the job as 'failed'" do
@job.run
@job.status.should == 'failed'
end
end
context "when using a processor with multiple output streams" do
before :each do
class MultipleOutputProcessor
def run(io)
return [ StringIO.new("FOO"), StringIO.new("BAR") ]
end
end
Juggler.processor = MultipleOutputProcessor
end
it "should store each as a separate object" do
%w(FOO BAR).each do |data|
AWS::S3::S3Object.should_receive(:store).
with(Digest::SHA1.hexdigest(data), anything, Juggler.config['processed_bucket_name'])
end
Juggler::Job.run(@s3_object)
end
end
end
end