forked from mongodb/mongo-ruby-driver
/
grid_io_test.rb
238 lines (202 loc) · 7.29 KB
/
grid_io_test.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
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
require File.expand_path("../test_helper", __FILE__)
class GridIOTest < Test::Unit::TestCase
context "GridIO" do
setup do
@db = standard_connection.db(MONGO_TEST_DB)
@files = @db.collection('fs.files')
@chunks = @db.collection('fs.chunks')
@chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]])
end
teardown do
@files.remove
@chunks.remove
end
context "Options" do
setup do
@filename = 'test'
@mode = 'w'
end
should "set default 256k chunk size" do
file = GridIO.new(@files, @chunks, @filename, @mode)
assert_equal 256 * 1024, file.chunk_size
end
should "set chunk size" do
file = GridIO.new(@files, @chunks, @filename, @mode, :chunk_size => 1000)
assert_equal 1000, file.chunk_size
end
end
context "StringIO methods" do
setup do
@filename = 'test'
@mode = 'w'
@data = "012345678\n" * 100000
@file = GridIO.new(@files, @chunks, @filename, @mode)
@file.write(@data)
@file.close
end
should "read data character by character using" do
bytes = 0
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
while file.getc
bytes += 1
end
assert_equal bytes, 1_000_000
end
should "read length is a length is given" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
string = file.gets(1000)
assert_equal string.length, 1000
bytes = 0
bytes += string.length
while string = file.gets(1000)
bytes += string.length
end
assert_equal bytes, 1_000_000
end
should "read to the end of the line by default and assign to $_" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
string = file.gets
assert_equal 10, string.length
end
should "read to the end of the file one line at a time" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
bytes = 0
while string = file.gets
bytes += string.length
end
assert_equal 1_000_000, bytes
end
should "read to the end of the file one multi-character separator at a time" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
bytes = 0
while string = file.gets("45")
bytes += string.length
end
assert_equal 1_000_000, bytes
end
should "read to a given separator" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
string = file.gets("5")
assert_equal 6, string.length
end
should "read a multi-character separator" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
string = file.gets("45")
assert_equal 6, string.length
string = file.gets("45")
assert_equal "678\n012345", string
string = file.gets("\n01")
assert_equal "678\n01", string
end
should "read a mult-character separator with a length" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
string = file.gets("45", 3)
assert_equal 3, string.length
end
should "tell position, eof, and rewind" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
file.read(1000)
assert_equal 1000, file.pos
assert !file.eof?
file.read
assert file.eof?
file.rewind
assert_equal 0, file.pos
assert_equal 1_000_000, file.read.length
end
end
context "Writing" do
setup do
@filename = 'test'
@length = 50000
@times = 10
end
should "correctly write multiple chunks from mutiple writes" do
file = GridIO.new(@files, @chunks, @filename, 'w')
@times.times do
file.write("1" * @length)
end
file.close
file = GridIO.new(@files, @chunks, @filename, 'r')
total_size = 0
while !file.eof?
total_size += file.read(@length).length
end
file.close
assert_equal total_size, @times * @length
end
end
context "Seeking" do
setup do
@filename = 'test'
@mode = 'w'
@data = "1" * 1024 * 1024
@file = GridIO.new(@files, @chunks, @filename, @mode)
@file.write(@data)
@file.close
end
should "read all data using read_length and then be able to seek" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
assert_equal @data, file.read(1024 * 1024)
file.seek(0)
assert_equal @data, file.read
end
should "read all data using read_all and then be able to seek" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
assert_equal @data, file.read
file.seek(0)
assert_equal @data, file.read
file.seek(1024 * 512)
assert_equal 524288, file.file_position
assert_equal @data.length / 2, file.read.length
assert_equal 1048576, file.file_position
assert_nil file.read
file.seek(1024 * 512)
assert_equal 524288, file.file_position
end
end
context "Grid MD5 check" do
should "run in safe mode" do
file = GridIO.new(@files, @chunks, 'smallfile', 'w', :safe => true)
file.write("DATA" * 100)
assert file.close
assert_equal file.server_md5, file.client_md5
end
should "validate with a large file" do
io = File.open(File.join(File.dirname(__FILE__), 'data', 'sample_file.pdf'), 'r')
file = GridIO.new(@files, @chunks, 'bigfile', 'w', :safe => true)
file.write(io)
assert file.close
assert_equal file.server_md5, file.client_md5
end
should "raise an exception when check fails" do
io = File.open(File.join(File.dirname(__FILE__), 'data', 'sample_file.pdf'), 'r')
@db.stubs(:command).returns({'md5' => '12345'})
file = GridIO.new(@files, @chunks, 'bigfile', 'w', :safe => true)
file.write(io)
assert_raise GridMD5Failure do
assert file.close
end
assert_not_equal file.server_md5, file.client_md5
end
end
context "Content types" do
if defined?(MIME)
should "determine common content types from the extension" do
file = GridIO.new(@files, @chunks, 'sample.pdf', 'w')
assert_equal 'application/pdf', file.content_type
file = GridIO.new(@files, @chunks, 'sample.txt', 'w')
assert_equal 'text/plain', file.content_type
end
end
should "default to binary/octet-stream when type is unknown" do
file = GridIO.new(@files, @chunks, 'sample.l33t', 'w')
assert_equal 'binary/octet-stream', file.content_type
end
should "use any provided content type by default" do
file = GridIO.new(@files, @chunks, 'sample.l33t', 'w', :content_type => 'image/jpg')
assert_equal 'image/jpg', file.content_type
end
end
end
end