Skip to content

Commit 649025e

Browse files
committed
test(cluster): simulate different cluster
1 parent 773402e commit 649025e

File tree

2 files changed

+77
-34
lines changed

2 files changed

+77
-34
lines changed

scripts/cluster.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var Cluster = function (params) {
1616

1717
if (this._params.concurrency === 1) {
1818
// Don't use a throttler
19-
this._throttler = null;
19+
this._throttler = undefined;
2020
} else {
2121
// Create a throttler with the specified or default concurrency
2222
var concurrency = this._params.concurrency ? this._params.concurrency : null;
@@ -28,7 +28,7 @@ Cluster.prototype.replicate = function () {
2828
var self = this;
2929
return self._sourceSlouch.db.all().each(function (db) {
3030
if (!self._params.skip || self._params.skip.indexOf(db) === -1) {
31-
return self._replicateDB(db);
31+
return self._replicateDB(db, db);
3232
}
3333
}, self._throttler);
3434
};
@@ -42,27 +42,27 @@ Cluster.prototype._createDBIfMissing = function (db) {
4242
});
4343
};
4444

45-
Cluster.prototype._replicateSecurity = function (db) {
45+
Cluster.prototype._replicateSecurity = function (sourceDB, targetDB) {
4646
var self = this;
47-
return self._sourceSlouch.security.get(db).then(function (security) {
48-
return self._targetSlouch.security.set(db, security);
47+
return self._sourceSlouch.security.get(sourceDB).then(function (security) {
48+
return self._targetSlouch.security.set(targetDB, security);
4949
});
5050
};
5151

52-
Cluster.prototype._replicateRawDB = function (db) {
52+
Cluster.prototype._replicateRawDB = function (sourceDB, targetDB) {
5353
return this._sourceSlouch.db.replicate({
54-
source: this._params.source + '/' + db,
55-
target: this._params.target + '/' + db
54+
source: this._params.source + '/' + sourceDB,
55+
target: this._params.target + '/' + targetDB
5656
});
5757
};
5858

59-
Cluster.prototype._replicateDB = function (db) {
59+
Cluster.prototype._replicateDB = function (sourceDB, targetDB) {
6060
var self = this;
61-
return self._createDBIfMissing(db).then(function () {
61+
return self._createDBIfMissing(targetDB).then(function () {
6262
// Replicate security first so that security is put in place before data is copied over
63-
return self._replicateSecurity(db);
63+
return self._replicateSecurity(sourceDB, targetDB);
6464
}).then(function () {
65-
return self._replicateRawDB(db);
65+
return self._replicateRawDB(sourceDB, targetDB);
6666
});
6767
};
6868

test/node-and-browser.js

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ describe('node and browser', function () {
1010
var slouch = null,
1111
id = 0,
1212
cluster = null,
13-
replicatedDBs = null;
13+
replicatedDBs = null,
14+
differentCluster = false;
1415

1516
var data = {
16-
test_db1: {
17+
db1: {
1718
security: {
1819
admins: {
1920
names: ['joe', 'phil'],
@@ -34,7 +35,7 @@ describe('node and browser', function () {
3435
}
3536
]
3637
},
37-
test_db2: {
38+
db2: {
3839
docs: [{
3940
_id: '3',
4041
foo: 'star'
@@ -49,7 +50,12 @@ describe('node and browser', function () {
4950

5051
var uniqueName = function (dbName) {
5152
// Use a unique id as back to back creation/deletion of the same DBs can lead to problems
52-
return dbName + '_' + id;
53+
return 'test_' + id + '_' + dbName;
54+
};
55+
56+
var otherClusterName = function (dbName) {
57+
// Simulate replicating to a different cluster by appending a suffix to the DB name
58+
return dbName + '_diff_cluster';
5359
};
5460

5561
var createDocs = function (db, docs) {
@@ -79,11 +85,11 @@ describe('node and browser', function () {
7985
};
8086

8187
var destroyData = function () {
82-
var promises = [];
83-
sporks.each(data, function (data, db) {
84-
promises.push(slouch.db.destroy(uniqueName(db)));
88+
return slouch.db.all().each(function (db) {
89+
if (isTestDB(db)) {
90+
return slouch.db.destroy(db);
91+
}
8592
});
86-
return Promise.all(promises);
8793
};
8894

8995
var docsShouldEql = function (db, docs) {
@@ -118,21 +124,35 @@ describe('node and browser', function () {
118124
var dataShouldEql = function () {
119125
var promises = [];
120126
sporks.each(data, function (data, db) {
121-
promises.push(dbDataShouldEql(uniqueName(db), data));
127+
db = uniqueName(db);
128+
db = differentCluster ? otherClusterName(db) : db;
129+
promises.push(dbDataShouldEql(db, data));
122130
});
123131
return Promise.all(promises);
124132
};
125133

134+
var isTestDB = function (db) {
135+
return db.indexOf('test_' + id) !== -1;
136+
};
137+
126138
var replicate = function (params) {
127139
cluster = new Cluster(params);
128140

129141
// Spy
130142
var _replicateDB = cluster._replicateDB;
131-
cluster._replicateDB = function (db) {
132-
if (db.indexOf('test_db') !== -1) {
133-
replicatedDBs.push(db);
143+
cluster._replicateDB = function (sourceDB /*, targetDB */ ) {
144+
var args = sporks.toArgsArray(arguments);
145+
146+
if (isTestDB(sourceDB)) {
147+
replicatedDBs.push(sourceDB);
148+
149+
if (differentCluster) {
150+
// Fake a different cluster by changing the target DB names
151+
args[1] = otherClusterName(sourceDB);
152+
}
134153
}
135-
return _replicateDB.apply(this, arguments);
154+
155+
return _replicateDB.apply(this, args);
136156
};
137157

138158
return cluster.replicate().then(function () {
@@ -143,10 +163,12 @@ describe('node and browser', function () {
143163
beforeEach(function () {
144164
slouch = new Slouch('http://admin:admin@localhost:5984');
145165

146-
id++;
166+
id = (new Date()).getTime();
147167

148168
replicatedDBs = [];
149169

170+
differentCluster = false;
171+
150172
return createData();
151173
});
152174

@@ -159,25 +181,46 @@ describe('node and browser', function () {
159181
source: 'http://admin:admin@localhost:5984',
160182
target: 'http://admin:admin@localhost:5984'
161183
}).then(function () {
162-
replicatedDBs.should.eql([uniqueName('test_db1'), uniqueName('test_db2')]);
184+
replicatedDBs.should.eql([uniqueName('db1'), uniqueName('db2')]);
163185
});
164186
});
165187

166188
it('should skip when replicating', function () {
167189
return replicate({
168190
source: 'http://admin:admin@localhost:5984',
169191
target: 'http://admin:admin@localhost:5984',
170-
skip: [uniqueName('test_db2')]
192+
skip: [uniqueName('db2')]
171193
}).then(function () {
172-
replicatedDBs.should.eql([uniqueName('test_db1')]);
194+
replicatedDBs.should.eql([uniqueName('db1')]);
173195
});
174196
});
175197

176-
// it('should support custom concurrency when replicating', function () {
177-
//
178-
// });
198+
it('should support custom concurrency when replicating', function () {
199+
return replicate({
200+
source: 'http://admin:admin@localhost:5984',
201+
target: 'http://admin:admin@localhost:5984',
202+
concurrency: 10
203+
}).then(function () {
204+
replicatedDBs.should.eql([uniqueName('db1'), uniqueName('db2')]);
205+
});
206+
});
179207

180-
// it('should replicate to different database', function () {
181-
// });
208+
it('should support synchronization when replicating', function () {
209+
return replicate({
210+
source: 'http://admin:admin@localhost:5984',
211+
target: 'http://admin:admin@localhost:5984',
212+
concurrency: 1
213+
}).then(function () {
214+
replicatedDBs.should.eql([uniqueName('db1'), uniqueName('db2')]);
215+
});
216+
});
217+
218+
it('should replicate to a different cluster', function () {
219+
differentCluster = true;
220+
return replicate({
221+
source: 'http://admin:admin@localhost:5984',
222+
target: 'http://admin:admin@localhost:5984'
223+
});
224+
});
182225

183226
});

0 commit comments

Comments
 (0)