forked from ericnorris/node-cdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdb-random-test.js
133 lines (111 loc) · 3.57 KB
/
cdb-random-test.js
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
'use strict';
var vows = require('vows'),
assert = require('assert'),
fs = require('fs'),
writable = require('../src/writable-cdb'),
readable = require('../src/readable-cdb'),
randomFile = 'test/random';
try {
fs.unlinkSync(randomFile);
} catch (err) {}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function getRandomString(minLength, maxLength) {
var length = getRandomInt(minLength, maxLength);
var stringArray = [];
for (var i = 0; i < length; i++) {
stringArray.push(String.fromCharCode(getRandomInt(97, 122)));
}
return stringArray.join('');
}
function generateRandomRecords(count) {
var randomRecords = {};
for (var i = 0; i < count; i++) {
var key = getRandomString(5, 10);
var data = getRandomString(20, 30);
if (key in randomRecords) {
randomRecords[key].push(data);
} else {
randomRecords[key] = [data];
}
}
return randomRecords;
}
function iterateOverRecords(records, callback) {
var keys = Object.keys(records);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var data = records[key];
for (var j = 0; j < data.length; j++) {
callback(key, j, data[j]);
}
}
}
var recordCount = 1000;
var randomRecords = generateRandomRecords(recordCount);
vows.describe('cdb-random-test').addBatch({
'An opened writable cdb': {
topic: function() {
(new writable(randomFile)).open(this.callback);
},
'should not error': function(err, cdb) {
assert.equal(err, null);
},
'should add records without exception': function(err, cdb) {
assert.doesNotThrow(function() {
iterateOverRecords(randomRecords, function(key, offset, data) {
cdb.put(key, data);
});
}, Error);
},
'should close': {
topic: function(cdb) {
cdb.close(this.callback);
},
'without error': function(err, cdb) {
assert.equal(err, null);
}
}
}
}).addBatch({
'An opened readable cdb': {
topic: function() {
(new readable(randomFile)).open(this.callback);
},
'should not error': function(err, cdb) {
assert.equal(err, null);
},
'when searching for existing keys': {
topic: function(cdb) {
var found = 0;
var notFound = 0;
var count = recordCount;
var callback = this.callback;
function checkRecord(expected) {
return function(err, data) {
if (err || data != expected) {
notFound++;
} else {
found++;
}
if (--count === 0) {
callback(notFound, found);
}
};
}
iterateOverRecords(randomRecords, function(key, offset, data) {
cdb.get(key, offset, checkRecord(data));
});
},
'should find all of them': function(notFound, found) {
assert.equal(notFound, null);
assert.equal(found, recordCount);
}
},
teardown: function(cdb) {
cdb.close();
fs.unlinkSync(randomFile);
}
}
}).export(module);