Skip to content

Commit 03ad2bf

Browse files
authored
Merge pull request #342 from share/use-async
Remove `util.callInSeries`
2 parents 7f9d98f + a8eac5d commit 03ad2bf

File tree

5 files changed

+175
-336
lines changed

5 files changed

+175
-336
lines changed

test/client/doc.js

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var Backend = require('../../lib/backend');
22
var expect = require('chai').expect;
3-
var util = require('../util');
3+
var async = require('async');
44

55
describe('Doc', function() {
66
beforeEach(function() {
@@ -319,26 +319,24 @@ describe('Doc', function() {
319319
});
320320

321321
it('rolls the doc back to a usable state', function(done) {
322-
util.callInSeries([
322+
async.series([
323323
function(next) {
324324
doc.submitOp(invalidOp, function(error) {
325-
expect(error).instanceOf(Error);
325+
expect(error).to.be.instanceOf(Error);
326326
next();
327327
});
328328
},
329-
function(next) {
330-
doc.whenNothingPending(next);
331-
},
329+
doc.whenNothingPending.bind(doc),
332330
function(next) {
333331
expect(doc.data).to.eql({name: 'Scooby'});
334-
doc.submitOp(validOp, next);
332+
next();
335333
},
334+
doc.submitOp.bind(doc, validOp),
336335
function(next) {
337336
expect(doc.data).to.eql({name: 'Scooby', snacks: true});
338337
next();
339-
},
340-
done
341-
]);
338+
}
339+
], done);
342340
});
343341

344342
it('rescues an irreversible op collision', function(done) {
@@ -366,29 +364,18 @@ describe('Doc', function() {
366364
}
367365
});
368366

369-
util.callInSeries([
370-
function(next) {
371-
doc1.create({colours: ['white']}, next);
372-
},
373-
function(next) {
374-
doc1.whenNothingPending(next);
375-
},
376-
function(next) {
377-
doc2.fetch(next);
378-
},
379-
function(next) {
380-
doc2.whenNothingPending(next);
381-
},
367+
async.series([
368+
doc1.create.bind(doc1, {colours: ['white']}),
369+
doc1.whenNothingPending.bind(doc1),
370+
doc2.fetch.bind(doc2),
371+
doc2.whenNothingPending.bind(doc2),
382372
// Both documents start off at the same v1 state, with colours as a list
383373
function(next) {
384374
expect(doc1.data).to.eql({colours: ['white']});
385375
expect(doc2.data).to.eql({colours: ['white']});
386376
next();
387377
},
388-
// doc1 successfully submits an op which changes our list into a string in v2
389-
function(next) {
390-
doc1.submitOp({p: ['colours'], oi: 'white,black'}, next);
391-
},
378+
doc1.submitOp.bind(doc1, {p: ['colours'], oi: 'white,black'}),
392379
// This next step is a little fiddly. We abuse the middleware to pause the op submission and
393380
// ensure that we get this repeatable sequence of events:
394381
// 1. doc2 is still on v1, where 'colours' is a list (but it's a string in v2)
@@ -416,9 +403,8 @@ describe('Doc', function() {
416403
expect(doc1.data).to.eql({colours: 'white,black'});
417404
expect(doc2.data).to.eql(doc1.data);
418405
doc2.submitOp({p: ['colours'], oi: 'white,black,red'}, next);
419-
},
420-
done
421-
]);
406+
}
407+
], done);
422408
});
423409
});
424410
});

test/client/snapshot-timestamp-request.js

Lines changed: 44 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
var Backend = require('../../lib/backend');
22
var expect = require('chai').expect;
3-
var util = require('../util');
43
var lolex = require('lolex');
54
var MemoryDb = require('../../lib/db/memory');
65
var MemoryMilestoneDb = require('../../lib/milestone-db/memory');
76
var sinon = require('sinon');
7+
var async = require('async');
88

99
describe('SnapshotTimestampRequest', function() {
1010
var backend;
@@ -70,112 +70,95 @@ describe('SnapshotTimestampRequest', function() {
7070

7171
beforeEach(function(done) {
7272
var doc = backend.connect().get('books', 'time-machine');
73-
util.callInSeries([
74-
function(next) {
75-
doc.create({title: 'The Time Machine'}, next);
76-
},
73+
async.series([
74+
doc.create.bind(doc, {title: 'The Time Machine'}),
7775
function(next) {
7876
clock.tick(ONE_DAY);
7977
doc.submitOp({p: ['author'], oi: 'HG Wells'}, next);
8078
},
8179
function(next) {
8280
clock.tick(ONE_DAY);
8381
doc.submitOp({p: ['author'], od: 'HG Wells', oi: 'H.G. Wells'}, next);
84-
},
85-
done
86-
]);
82+
}
83+
], done);
8784
});
8885

8986
it('fetches the version at exactly day 1', function(done) {
90-
util.callInSeries([
91-
function(next) {
92-
backend.connect().fetchSnapshotByTimestamp('books', 'time-machine', day1, next);
93-
},
87+
var connection = backend.connect();
88+
async.waterfall([
89+
connection.fetchSnapshotByTimestamp.bind(connection, 'books', 'time-machine', day1),
9490
function(snapshot, next) {
9591
expect(snapshot).to.eql(v1);
9692
next();
97-
},
98-
done
99-
]);
93+
}
94+
], done);
10095
});
10196

10297
it('fetches the version at exactly day 2', function(done) {
103-
util.callInSeries([
104-
function(next) {
105-
backend.connect().fetchSnapshotByTimestamp('books', 'time-machine', day2, next);
106-
},
98+
var connection = backend.connect();
99+
async.waterfall([
100+
connection.fetchSnapshotByTimestamp.bind(connection, 'books', 'time-machine', day2),
107101
function(snapshot, next) {
108102
expect(snapshot).to.eql(v2);
109103
next();
110-
},
111-
done
112-
]);
104+
}
105+
], done);
113106
});
114107

115108
it('fetches the version at exactly day 3', function(done) {
116-
util.callInSeries([
117-
function(next) {
118-
backend.connect().fetchSnapshotByTimestamp('books', 'time-machine', day3, next);
119-
},
109+
var connection = backend.connect();
110+
async.waterfall([
111+
connection.fetchSnapshotByTimestamp.bind(connection, 'books', 'time-machine', day3),
120112
function(snapshot, next) {
121113
expect(snapshot).to.eql(v3);
122114
next();
123-
},
124-
done
125-
]);
115+
}
116+
], done);
126117
});
127118

128119
it('fetches the day 2 version when asking for a time halfway between days 2 and 3', function(done) {
129120
var halfwayBetweenDays2and3 = (day2 + day3) * 0.5;
130-
util.callInSeries([
131-
function(next) {
132-
backend.connect().fetchSnapshotByTimestamp('books', 'time-machine', halfwayBetweenDays2and3, next);
133-
},
121+
var connection = backend.connect();
122+
async.waterfall([
123+
connection.fetchSnapshotByTimestamp.bind(connection, 'books', 'time-machine', halfwayBetweenDays2and3),
134124
function(snapshot, next) {
135125
expect(snapshot).to.eql(v2);
136126
next();
137-
},
138-
done
139-
]);
127+
}
128+
], done);
140129
});
141130

142131
it('fetches the day 3 version when asking for a time after day 3', function(done) {
143-
util.callInSeries([
144-
function(next) {
145-
backend.connect().fetchSnapshotByTimestamp('books', 'time-machine', day4, next);
146-
},
132+
var connection = backend.connect();
133+
async.waterfall([
134+
connection.fetchSnapshotByTimestamp.bind(connection, 'books', 'time-machine', day4),
147135
function(snapshot, next) {
148136
expect(snapshot).to.eql(v3);
149137
next();
150-
},
151-
done
152-
]);
138+
}
139+
], done);
153140
});
154141

155142
it('fetches the most recent version when not specifying a timestamp', function(done) {
156-
util.callInSeries([
157-
function(next) {
158-
backend.connect().fetchSnapshotByTimestamp('books', 'time-machine', next);
159-
},
143+
var connection = backend.connect();
144+
async.waterfall([
145+
connection.fetchSnapshotByTimestamp.bind(connection, 'books', 'time-machine'),
160146
function(snapshot, next) {
161147
expect(snapshot).to.eql(v3);
162148
next();
163-
},
164-
done
165-
]);
149+
}
150+
], done);
166151
});
167152

168153
it('fetches an empty snapshot if the timestamp is before the document creation', function(done) {
169-
util.callInSeries([
170-
function(next) {
171-
backend.connect().fetchSnapshotByTimestamp('books', 'time-machine', day0, next);
172-
},
154+
var connection = backend.connect();
155+
async.waterfall([
156+
connection.fetchSnapshotByTimestamp.bind(connection, 'books', 'time-machine', day0),
173157
function(snapshot, next) {
174158
expect(snapshot).to.eql(v0);
175159
next();
176-
},
177-
done
178-
]);
160+
}
161+
], done);
179162
});
180163

181164
it('throws if the timestamp is undefined', function() {
@@ -407,10 +390,8 @@ describe('SnapshotTimestampRequest', function() {
407390

408391
var doc = backendWithMilestones.connect().get('books', 'mocking-bird');
409392

410-
util.callInSeries([
411-
function(next) {
412-
doc.create({title: 'To Kill a Mocking Bird'}, next);
413-
},
393+
async.series([
394+
doc.create.bind(doc, {title: 'To Kill a Mocking Bird'}),
414395
function(next) {
415396
clock.tick(ONE_DAY);
416397
doc.submitOp({p: ['author'], oi: 'Harper Lea'}, next);
@@ -426,9 +407,8 @@ describe('SnapshotTimestampRequest', function() {
426407
function(next) {
427408
clock.tick(ONE_DAY);
428409
doc.submitOp({p: ['year'], od: 1959, oi: 1960}, next);
429-
},
430-
done
431-
]);
410+
}
411+
], done);
432412
});
433413

434414
it('fetches a snapshot between two milestones using the milestones', function(done) {

test/client/snapshot-version-request.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var expect = require('chai').expect;
33
var MemoryDb = require('../../lib/db/memory');
44
var MemoryMilestoneDb = require('../../lib/milestone-db/memory');
55
var sinon = require('sinon');
6-
var util = require('../util');
6+
var async = require('async');
77

88
describe('SnapshotVersionRequest', function() {
99
var backend;
@@ -419,16 +419,10 @@ describe('SnapshotVersionRequest', function() {
419419
it('fetches a snapshot using the milestone', function(done) {
420420
var doc = backendWithMilestones.connect().get('books', 'mocking-bird');
421421

422-
util.callInSeries([
423-
function(next) {
424-
doc.create({title: 'To Kill a Mocking Bird'}, next);
425-
},
426-
function(next) {
427-
doc.submitOp({p: ['author'], oi: 'Harper Lea'}, next);
428-
},
429-
function(next) {
430-
doc.submitOp({p: ['author'], od: 'Harper Lea', oi: 'Harper Lee'}, next);
431-
},
422+
async.waterfall([
423+
doc.create.bind(doc, {title: 'To Kill a Mocking Bird'}),
424+
doc.submitOp.bind(doc, {p: ['author'], oi: 'Harper Lea'}),
425+
doc.submitOp.bind(doc, {p: ['author'], od: 'Harper Lea', oi: 'Harper Lee'}),
432426
function(next) {
433427
sinon.spy(milestoneDb, 'getMilestoneSnapshot');
434428
sinon.spy(db, 'getOps');
@@ -440,9 +434,8 @@ describe('SnapshotVersionRequest', function() {
440434
expect(snapshot.v).to.equal(3);
441435
expect(snapshot.data).to.eql({title: 'To Kill a Mocking Bird', author: 'Harper Lee'});
442436
next();
443-
},
444-
done
445-
]);
437+
}
438+
], done);
446439
});
447440
});
448441
});

0 commit comments

Comments
 (0)