This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
227 lines (185 loc) · 5.96 KB
/
index.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
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
var crypto = require('crypto');
var AWS = require('aws-sdk');
var _ = require('underscore');
var stream = require('stream');
var Dyno = require('dyno');
var dynalite = require('dynalite')({
createTableMs: 0,
updateTableMs: 0,
deleteTableMs: 0
});
var listening = false;
module.exports = ddbtest;
module.exports.fixedName = function(test, tableName, tableDef) {
var dynamo = ddbtest(test, tableName, tableDef);
dynamo.tableName = dynamo.tableDef.TableName = tableName;
return dynamo;
};
function ddbtest(test, projectName, tableDef, region) {
var live = !!region;
tableDef = _(tableDef).clone();
function getKeys(item) {
var keyNames = tableDef.KeySchema.map(function(key) {
return key.AttributeName;
});
return keyNames.reduce(function(key, name) {
key[name] = item[name];
return key;
}, {});
}
var dynamodb = {};
dynamodb.tableName = tableDef.TableName = [
'test',
projectName,
crypto.randomBytes(4).toString('hex')
].join('-');
dynamodb.tableDef = tableDef;
var options = dynamodb.config = live ? { region: region } : {
region: 'fake',
accessKeyId: 'fake',
secretAccessKey: 'fake',
endpoint: 'http://localhost:4567'
};
dynamodb.dynamo = new AWS.DynamoDB(options);
dynamodb.dyno = Dyno(_({ table: dynamodb.tableName }).extend(options));
var tableRunning = false;
function start(assert, callback) {
if (live) assert.timeoutAfter(300000);
if (tableRunning) return assert.end();
function done(err) {
if (err) throw err;
tableRunning = true;
callback();
}
if (live) return dynamodb.dyno.createTable(tableDef, done);
if (listening) return dynamodb.dyno.createTable(tableDef, done);
dynalite.listen(4567, function(err) {
if (err) throw err;
listening = true;
dynamodb.dyno.createTable(tableDef, done);
});
}
dynamodb.start = function() {
test('[dynamodb-test] create table', function(assert) {
start(assert, function() {
assert.end();
});
});
};
dynamodb.delete = function() {
test('[dynamodb-test] delete table', function(assert) {
if (live) assert.timeoutAfter(300000);
if (!tableRunning) return assert.end();
dynamodb.dyno.deleteTable({
TableName: dynamodb.tableName
}, function(err) {
if (err) throw err;
tableRunning = false;
assert.end();
});
});
};
dynamodb.load = function(fixtures) {
test('[dynamodb-test] load fixtures', function(assert) {
if (!tableRunning) start(assert, load);
else load();
function load() {
if (live) assert.timeoutAfter(300000);
var params = { RequestItems: {} };
params.RequestItems[dynamodb.tableName] = fixtures.map(function(item) {
return { PutRequest: { Item: item } };
});
var requestSet = dynamodb.dyno.batchWriteItemRequests(params);
var attempts = 0;
(function write(requestSet) {
requestSet.sendAll(10, function(err, responses, unprocessed) {
if (err) throw err;
attempts++;
if (unprocessed && unprocessed.length)
return setTimeout(write, Math.pow(2, attempts), unprocessed);
assert.end();
});
})(requestSet);
}
});
};
dynamodb.empty = function() {
test('[dynamodb-test] empty table', function(assert) {
if (!tableRunning) start(assert, empty);
else empty();
function empty() {
if (live) {
assert.timeoutAfter(300000);
var deletes = new stream.Writable({ objectMode: true });
deletes.pending = false;
var keys = [];
keys.send = function(callback) {
var params = { RequestItems: {} };
params.RequestItems[dynamodb.tableName] = keys.splice(0, 25).map(function(key) {
return { DeleteRequest: { Key: key } };
});
(function destroy(params) {
deletes.pending = true;
dynamodb.dyno.batchWriteItem(params, function(err, data) {
deletes.pending = false;
if (err) throw err;
if (data.UnprocessedItems && Object.keys(data.UnprocessedItems).length)
return destroy({RequestItems: data.UnprocessedItems });
callback();
});
})(params);
};
deletes._write = function(item, enc, callback) {
keys.push(getKeys(item));
if (keys.length < 25) return callback();
keys.send(callback);
};
var end = deletes.end.bind(deletes);
deletes.end = function() {
if (deletes.pending) return setImmediate(deletes.end);
if (!keys.length) return end();
keys.send(end);
};
dynamodb.dyno.scanStream({ ConsistentRead: true })
.pipe(deletes)
.on('finish', function() {
assert.end();
});
return;
}
dynamodb.dyno.deleteTable(dynamodb.tableName, function(err) {
if (err) throw err;
tableRunning = false;
dynamodb.dyno.createTable(dynamodb.tableDef, function(err) {
if (err) throw err;
tableRunning = true;
assert.end();
});
});
}
});
};
dynamodb.test = function(name, fixtures, callback) {
dynamodb.empty();
if (typeof fixtures === 'function') {
callback = fixtures;
fixtures = null;
}
if (fixtures && fixtures.length) dynamodb.load(fixtures);
test(name, callback);
dynamodb.empty();
};
if (!live) dynamodb.close = function() {
test('[dynamodb-test] close dynalite', function(assert) {
dynamodb.dyno.deleteTable(dynamodb.tableName, function(err) {
if (err) throw err;
dynalite.close(function(err) {
if (err) throw err;
listening = false;
assert.end();
});
});
});
};
return dynamodb;
}