spool_pool 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,245 @@
1
+ require 'spec_helper'
2
+
3
+ describe SpoolPool::Spool do
4
+ before( :each ) do
5
+ @testspoolpath = File.join( TEST_SPOOL_ROOT, "spools" )
6
+ @pathname = Pathname.new( @testspoolpath )
7
+ @pathname.mkpath
8
+ @pathname.chmod 0755
9
+ @instance = SpoolPool::Spool.new( @pathname )
10
+ end
11
+
12
+ after( :each ) do
13
+ @pathname.rmtree if @pathname.exist?
14
+ end
15
+
16
+ it "should have a pathname attribute" do
17
+ @instance.should respond_to( :pathname )
18
+ end
19
+
20
+ describe "#initialize" do
21
+ it "should set the pathname attribute" do
22
+ @instance.pathname.should == @pathname
23
+ end
24
+
25
+ context "if the directory exists" do
26
+ before( :each ) do
27
+ @pathname.mkpath
28
+ end
29
+
30
+ it "should raise an exception if it can't create a file" do
31
+ with_fs_mode( @pathname, 0555 ) do
32
+ lambda { SpoolPool::Spool.new( @pathname ) }.should raise_error( Errno::EACCES )
33
+ end
34
+ end
35
+
36
+ it "should raise an exception if it can't read a file" do
37
+ with_fs_mode( @pathname, 0333 ) do
38
+ lambda { SpoolPool::Spool.new( @pathname ) }.should raise_error( Errno::EACCES )
39
+ end
40
+ end
41
+ end
42
+
43
+ context "if the directory doesn't exist" do
44
+ before( :each ) do
45
+ @pathname.unlink if @pathname.exist?
46
+ end
47
+
48
+ it "should not check accessablity" do
49
+ @pathname.should_not_receive( :readable? )
50
+ @pathname.should_not_receive( :writable? )
51
+ SpoolPool::Spool.new( @pathname )
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "#put" do
57
+ before( :each ) do
58
+ @data = 'some data'
59
+ end
60
+
61
+ context "the queue directory doesn't exist" do
62
+ before( :each ) do
63
+ @pathname.rmtree if @pathname.exist?
64
+ end
65
+
66
+ it "should try to create the queue directory" do
67
+ path = @instance.put( @data )
68
+
69
+ @pathname.should exist
70
+ end
71
+
72
+ it "should raise an exception if it can't create the spool_dir" do
73
+ with_fs_mode( @pathname.parent, 0000 ) do
74
+ lambda { @instance.put( @data ) }.should raise_error
75
+ end
76
+ end
77
+ end
78
+
79
+ it "should return the path of the stored file" do
80
+ path = @instance.put( @data )
81
+ Pathname.new( path ).read.should == @instance.serialize( @data )
82
+ end
83
+ end
84
+
85
+ describe "the #get/#put pair" do
86
+ it "should serialize and deserialize the data written" do
87
+ data = { :foo => 2, :bar => [ "some string", 3.45, ] }
88
+
89
+ @instance.put data
90
+ read_data = @instance.get
91
+
92
+ read_data.should == data
93
+ end
94
+ end
95
+
96
+ describe "#get" do
97
+ before( :each ) do
98
+ @pathname.mkpath
99
+ @pathname.chmod 0755
100
+ end
101
+
102
+ it "should return the contents of one of the files with the oldest ctime in spool directory" do
103
+ oldest_data = 'foo'
104
+ youngest_data = 'blubb'
105
+ @instance.put oldest_data
106
+ @instance.put youngest_data
107
+
108
+ @instance.get.should == oldest_data
109
+ end
110
+
111
+ context "no file is available in the requested spool" do
112
+ before( :each ) do
113
+ @pathname.children.each { |child| child.unlink }
114
+ end
115
+
116
+ it "should return nil" do
117
+ @instance.get.should be_nil
118
+ end
119
+ end
120
+
121
+ it "should raise an exception if the queue directory is not readable" do
122
+ with_fs_mode( @pathname, 0000 ) do
123
+ lambda { @instance.get }.should raise_error
124
+ end
125
+ end
126
+
127
+ context "if no block is passed" do
128
+ it "should delete the read file" do
129
+ path = Pathname.new( @instance.put( @data ) )
130
+ @instance.get
131
+ path.should_not be_exist
132
+ end
133
+ end
134
+
135
+ context "if a block is passed" do
136
+ it "should yield the contents of one of the files with the oldest ctime in spool directory" do
137
+ oldest_data = 'foo'
138
+ youngest_data = 'blubb'
139
+ @instance.put oldest_data
140
+ @instance.put youngest_data
141
+
142
+ @instance.get { |spool_data| spool_data.should == oldest_data }
143
+ end
144
+
145
+ context "no exception was raised within the passed block" do
146
+ it "should delete the read file" do
147
+ path = Pathname.new( @instance.put( @data ) )
148
+ @instance.get{}
149
+ path.should_not be_exist
150
+ end
151
+ end
152
+
153
+ context "an exception was raised within the passed block" do
154
+ it "should not delete the read file" do
155
+ path = Pathname.new( @instance.put( @data ) )
156
+ lambda{ @instance.get{ raise RuntimeError } }
157
+ path.should be_exist
158
+ end
159
+
160
+ it "should let the raised exception bubble up" do
161
+ path = Pathname.new( @instance.put( @data ) )
162
+ lambda{ @instance.get{ raise RuntimeError } }.should raise_error( RuntimeError )
163
+ end
164
+ end
165
+ end
166
+
167
+ it "should raise an exception if the oldest file in the queue directory is not readable" do
168
+ path = Pathname.new( @instance.put( @data ) )
169
+ with_fs_mode( path, 0333 ) do
170
+ lambda { @instance.get{} }.should raise_error
171
+ end
172
+ end
173
+
174
+ it "should raise an exception if the oldest file in the queue directory is not deleteable" do
175
+ path = Pathname.new( @instance.put( @data ) )
176
+ with_fs_mode( @pathname, 0555 ) do
177
+ lambda { @instance.get{} }.should raise_error
178
+ end
179
+ end
180
+
181
+ end
182
+
183
+ describe "#flush" do
184
+ context "each file in the spool directory" do
185
+ before( :each ) do
186
+ @oldest_data = "oldest data"
187
+ @middle_data = "middle data"
188
+ @youngest_data = "youngest data"
189
+ @oldest_file = @instance.put @oldest_data
190
+ @middle_file = @instance.put @middle_data
191
+ @youngest_file = @instance.put @youngest_data
192
+ end
193
+
194
+ it "should be yielded to the passed block" do
195
+ times_yielded = 0
196
+ @instance.flush { times_yielded += 1 }
197
+ times_yielded.should == 3
198
+ end
199
+
200
+ it "should be yielded ordered by date, oldest first" do
201
+ times_yielded = 0
202
+ @instance.flush do |data|
203
+ times_yielded += 1
204
+ case times_yielded
205
+ when 1 then data.should == @oldest_data
206
+ when 2 then data.should == @middle_data
207
+ when 3 then data.should == @youngest_data
208
+ end
209
+ end
210
+ end
211
+
212
+ it "should be deleted after it was processed" do
213
+ times_yielded = 0
214
+ @instance.flush do |data|
215
+ times_yielded += 1
216
+ case times_yielded
217
+ when 1 then File.exist?(@oldest_file).should_not be_true
218
+ when 2 then File.exist?(@middle_file).should_not be_true
219
+ when 3 then File.exist?(@youngest_file).should_not be_true
220
+ end
221
+ end
222
+ end
223
+ end
224
+
225
+ it "should raise an exception if the queue directory is not readable" do
226
+ with_fs_mode( @pathname, 0000 ) do
227
+ lambda { @instance.fetch }.should raise_error
228
+ end
229
+ end
230
+
231
+ it "should raise an exception if the oldest file in the queue directory is not readable" do
232
+ path = Pathname.new( @instance.put( @data ) )
233
+ with_fs_mode( path, 0333 ) do
234
+ lambda { @instance.fetch }.should raise_error
235
+ end
236
+ end
237
+
238
+ it "should raise an exception if the oldest file in the queue directory is not deleteable" do
239
+ path = Pathname.new( @instance.put( @data ) )
240
+ with_fs_mode( @pathname, 0555 ) do
241
+ lambda { @instance.fetch }.should raise_error
242
+ end
243
+ end
244
+ end
245
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spool_pool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Sven Riedel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-11 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A simple library for spooler pools.
17
+ email: sr@gimp.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE.txt
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE.txt
27
+ - README.rdoc
28
+ - History.rdoc
29
+ - TODOs
30
+ - lib/spool_pool.rb
31
+ - lib/spool_pool/pool.rb
32
+ - lib/spool_pool/spool.rb
33
+ - lib/spool_pool/file.rb
34
+ - spec/spec_helper.rb
35
+ - spec/spool_pool/pool_spec.rb
36
+ - spec/spool_pool/spool_spec.rb
37
+ - spec/spool_pool/file_spec.rb
38
+ - scripts/perf_test.rb
39
+ has_rdoc: true
40
+ homepage:
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.9.1
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.5
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: A simple library for spooler pools.
67
+ test_files: []
68
+