forked from samus/mongodb-csharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GridFileStreamTest.cs
executable file
·374 lines (318 loc) · 13.3 KB
/
GridFileStreamTest.cs
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
using System;
using System.IO;
using NUnit.Framework;
using MongoDB.Driver;
namespace MongoDB.GridFS
{
[TestFixture]
public class GridFileStreamTest
{
Mongo db = new Mongo();
GridFile fs;
String filesystem = "gfstream";
[Test]
public void TestWrite(){
GridFileStream gfs = fs.Create("test.txt");
Object id = gfs.GridFileInfo.Id;
for(byte b = (byte)0; b < 128; b++){
gfs.WriteByte(b);
}
gfs.Close();
Assert.AreEqual(1, CountChunks(id));
Document chunk = GrabChunk(id, 0);
Binary bin = (Binary)chunk["data"];
Assert.AreEqual(127, bin.Bytes[127]);
Assert.AreEqual(0, bin.Bytes[0]);
}
[Test]
public void TestWriteMultipleBytes(){
GridFileStream gfs = fs.Create("multiplebytes.txt");
Object id = gfs.GridFileInfo.Id;
for(int x = 0; x < 256; x++){
gfs.Write(BitConverter.GetBytes(x),0,4);
}
gfs.Close();
Assert.AreEqual(1, CountChunks(id));
}
[Test]
public void TestWriteTo3Chunks(){
GridFileStream gfs = fs.Create("largewrite.txt");
Object id = gfs.GridFileInfo.Id;
int chunks = 3;
int buffsize = 256 * 1024 * chunks;
Byte[] buff = CreateBuffer(buffsize, (byte)6);
gfs.Write(buff,0,buff.Length);
Assert.AreEqual(buff.Length, gfs.Position);
gfs.Close();
Assert.AreEqual(chunks, CountChunks(id));
}
[Test]
public void TestWriteWithMultipleFlushes(){
string filename = "multiflush.txt";
GridFileStream gfs = fs.Create(filename);
Object id = gfs.GridFileInfo.Id;
int size = gfs.GridFileInfo.ChunkSize * 2;
byte[] buff;
int x = 0;
for(int i = 0; i < size; i+=4){
buff = BitConverter.GetBytes(x);
gfs.Write(buff,0,buff.Length);
x++;
if(i % size/4 == 0){
gfs.Flush();
}
}
gfs.Close();
gfs = fs.OpenRead(filename);
int read;
int val;
buff = new byte[4];
for(int i = 0; i < size/4; i++){
read = gfs.Read(buff,0,4);
val = BitConverter.ToInt32(buff, 0);
Assert.AreEqual(4, read, "Not enough bytes were read. Pos: " + gfs.Position);
Assert.AreEqual(i,val, "value read back was not the same as written. Pos: " + gfs.Position);
}
}
[Test]
public void TestWriteMultipleBytesWithOffset(){
String filename = "multioffset.txt";
int offset = 4;
int chunks = 2;
int chunksize = 100;
int size = chunks * chunksize;
Object id = CreateDummyFile(filename, size, chunksize, offset);
Assert.AreEqual(2, CountChunks(id));
GridFileStream gfs = fs.OpenRead(filename);
byte[] buff = new Byte[4];
int read;
int val;
for(int i = 1; i <= size/4 - offset; i++){
read = gfs.Read(buff,0,4);
val = BitConverter.ToInt32(buff, 0);
Assert.AreEqual(4, read, "Not enough bytes were read. Pos: " + gfs.Position);
Assert.AreEqual(i,val, "value read back was not the same as written. Pos: " + gfs.Position);
}
}
[Test]
public void TestNonSequentialWriteToOneChunk(){
string filename = "nonsequential1.txt";
GridFileStream gfs = fs.Create(filename);
Object id = gfs.GridFileInfo.Id;
int chunksize = gfs.GridFileInfo.ChunkSize;
gfs.Seek(chunksize/2, SeekOrigin.Begin);
byte[] two = new byte[]{2};
for(int i = chunksize; i > chunksize/2; i--){
gfs.Write(two, 0, 1);
}
gfs.Seek(0, SeekOrigin.Begin);
byte[] one = new byte[]{1};
for(int i = 0; i < chunksize/2; i++){
gfs.Write(one, 0, 1);
}
gfs.Close();
Assert.AreEqual(1, CountChunks(id));
Document chunk = GrabChunk(id, 0);
Binary b = (Binary)chunk["data"];
Assert.AreEqual(2, b.Bytes[chunksize-1]);
Assert.AreEqual(2, b.Bytes[chunksize/2]);
Assert.AreEqual(1, b.Bytes[chunksize/2 -1]);
Assert.AreEqual(1, b.Bytes[0]);
}
[Test]
public void TestNonSequentialWriteToPartialChunk(){
string filename = "nonsequentialpartial.txt";
GridFileStream gfs = fs.Create(filename);
Object id = gfs.GridFileInfo.Id;
int chunksize = gfs.GridFileInfo.ChunkSize;
gfs.Write(BitConverter.GetBytes(0),0,4);
Assert.AreEqual(4, gfs.Position);
gfs.Write(BitConverter.GetBytes(7),0,4);
Assert.AreEqual(8, gfs.Position);
gfs.Seek(0, SeekOrigin.Begin);
gfs.Write(BitConverter.GetBytes(15),0,4);
gfs.Close();
Document chunk = GrabChunk(id, 0);
Binary b = (Binary)chunk["data"];
Assert.AreEqual(8,b.Bytes.Length);
}
[Test]
public void TestNonSequentialWriteToTwoChunks(){
GridFileStream gfs = fs.Create("nonsequential2.txt");
Object id = gfs.GridFileInfo.Id;
int chunks = 3;
int buffsize = 256 * 1024;
for(int c = 0; c < chunks; c++){
Byte[] buff = CreateBuffer(buffsize, (byte)c);
if(c == 2){
gfs.Seek(0, SeekOrigin.Begin); //On last chunk seek to start.
}
gfs.Write(buff,0,buff.Length);
}
Assert.AreEqual(buffsize, gfs.Position, "Position is incorrect");
gfs.Close();
Assert.AreEqual(chunks - 1, CountChunks(id));
Document chunk = GrabChunk(id, 0);
Binary b = (Binary)chunk["data"];
Assert.AreEqual(2, b.Bytes[buffsize-1]);
Assert.AreEqual(2, b.Bytes[0]);
chunk = GrabChunk(id, 1);
b = (Binary)chunk["data"];
Assert.AreEqual(1, b.Bytes[buffsize-1]);
}
[Test]
public void TestRead(){
string filename = "readme.txt";
GridFileStream gfs = fs.Create(filename);
for(int i = 1; i <= 50; i++){
gfs.Write(BitConverter.GetBytes(i), 0, 4);
}
gfs.Close();
gfs = fs.OpenRead(filename);
Byte[] buff = new Byte[4];
int read;
for(int i = 1; i <= 50; i++){
read = gfs.Read(buff,0,4);
Assert.AreEqual(4, read, "Not enough bytes were read");
Assert.AreEqual(i,BitConverter.ToInt32(buff, 0), "value read back was not the same as written");
}
}
[Test]
public void TestReadIntoBufferBiggerThanChunk(){
string filename = "largereadbuffer.txt";
int size = 100;
Object id = CreateDummyFile(filename,size,50,0);
GridFileStream gfs = fs.OpenRead(filename);
byte[] buff = new byte[size];
int read = gfs.Read(buff,0,size);
Assert.AreEqual(size, read, "Not all bytes read back from file.");
int expected = 0;
for(int i = 0; i < size; i+=4){
int val = BitConverter.ToInt32(buff,i);
Assert.AreEqual(expected, val, "Val was not same as expected. Pos: " + i);
expected++;
}
}
[Test]
public void TestSetLengthBigger(){
string filename = "setlengthbigger.txt";
GridFileStream gfs = fs.Create(filename);
Object id = gfs.GridFileInfo.Id;
long length = 256 * 1024 * 5;
gfs.WriteByte(1);
gfs.SetLength(length);
gfs.WriteByte(2);
gfs.Close();
GridFileInfo gfi = new GridFileInfo(db["tests"],filesystem,filename);
Assert.AreEqual(length + 1, gfi.Length);
Assert.AreEqual(6, CountChunks(id));
}
[Test]
public void TestSetLengthSmaller(){
string filename = "setlengthsmaller.txt";
int chunksize = 256 * 1024;
int chunks = 4;
int size = chunks * chunksize;
int newsize = (size / 2) - (chunksize/2);
Object id = this.CreateDummyFile(filename, size,chunksize,0);
GridFileStream gfs = fs.Open(filename,FileMode.Open,FileAccess.ReadWrite);
gfs.SetLength(newsize);
gfs.Close();
Assert.AreEqual(newsize, gfs.GridFileInfo.Length);
}
[Test]
public void TestReadLengthIsSameAsWriteLength(){
string filename = "readwritelength.txt";
GridFileStream gfs = fs.Create(filename);
int length = 0;
for(int i = 1; i <= 50; i++){
gfs.Write(BitConverter.GetBytes(i), 0, 4);
length += 4;
}
gfs.Close();
Assert.AreEqual(length, gfs.GridFileInfo.Length, "File length written is not the same as in gridfileinfo");
gfs = fs.OpenRead(filename);
byte[] buffer = new byte[16];
int read = 0;
int readLength = read;
while((read = gfs.Read(buffer,0,buffer.Length)) > 0){
readLength += read;
}
Assert.AreEqual(length, readLength, "Too much read back.");
}
#region File API compatibility
[Test]
public void TestSeekingBeyondEOF(){
int buffsize = 256;
string filename = "seektest.txt";
FileStream gfs = File.Create(filename,buffsize);//,FileOptions.DeleteOnClose);
int chunks = 3;
//int buffsize = 256 * 1024;
for(int c = 0; c < chunks; c++){
Byte[] buff = new byte[buffsize];
for(int i = 0; i < buff.Length; i++){
buff[i] = (byte)(c);
}
if(c == 2) gfs.Seek(0, SeekOrigin.Begin); //On last chunk seek to start.
gfs.Write(buff,0,buff.Length);
gfs.Seek(5,SeekOrigin.Current);
}
gfs.Seek(50,SeekOrigin.End);
gfs.Write(new byte[]{5},0,1);
gfs.Close();
File.Delete(filename);
}
#endregion
[TestFixtureSetUp]
public void Init(){
db.Connect();
fs = new GridFile(db["tests"], filesystem);
CleanDB(); //Run here instead of at the end so that the db can be examined after a run.
}
[TestFixtureTearDown]
public void Dispose(){
db.Disconnect();
}
protected void CleanDB(){
//Any collections that we might want to delete before the tests run should be done here.
DropGridFileSystem(filesystem);
}
protected void DropGridFileSystem(string filesystem){
try{
db["tests"].MetaData.DropCollection(filesystem + ".files");
db["tests"].MetaData.DropCollection(filesystem + ".chunks");
}catch(MongoCommandException){}//if it fails it is because the collection isn't there to start with.
}
protected long CountChunks(Object fileid){
return db["tests"][filesystem + ".chunks"].Count(new Document().Append("files_id", fileid));
}
protected Document GrabChunk(Object fileid, int chunk){
return db["tests"][filesystem + ".chunks"].FindOne(new Document().Append("files_id", fileid).Append("n", chunk));
}
protected Object CreateDummyFile(string filename, int size, int chunksize, int initialOffset){
GridFileInfo gfi = new GridFileInfo(db["tests"], "gfstream", filename);
gfi.ChunkSize = chunksize;
GridFileStream gfs = gfi.Create();
Object id = gfs.GridFileInfo.Id;
byte[] buff = CreateIntBuffer(size);
gfs.Write(buff,initialOffset,buff.Length - initialOffset);
gfs.Close();
return id;
}
protected byte[] CreateBuffer(int size, byte fill){
Byte[] buff = new byte[size];
for(int i = 0; i < buff.Length; i++){
buff[i] = fill;
}
return buff;
}
protected byte[] CreateIntBuffer(int size){
byte[] buff = new byte[size];
int x = 1;
for(int i = 4; i < buff.Length; i+=4){
Array.Copy(BitConverter.GetBytes(x++),0,buff,i,4);
}
return buff;
}
}
}