@@ -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