From 5ed70df8b14c8690610fc427f546293763ab00cb Mon Sep 17 00:00:00 2001 From: Nick Maynard Date: Thu, 19 Mar 2015 17:30:10 +0000 Subject: [PATCH 1/3] Add failing test for live replication updates --- test/test.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/test/test.js b/test/test.js index ea6bfe2..bee5ac2 100644 --- a/test/test.js +++ b/test/test.js @@ -709,4 +709,98 @@ function tests(dbName, dbType) { }); }); }); + + describe(dbType + ': live replication tests', function () { + + var db; + var remote; + + // Utility function - complex test incoming + var defer = function() { + var resolve, reject; + var promise = new Promise(function() { + resolve = arguments[0]; + reject = arguments[1]; + }); + return { + resolve: resolve, + reject: reject, + promise: promise + }; + }; + + beforeEach(function () { + db = new Pouch(dbName); + remote = new Pouch(dbName + '_other'); + }); + + afterEach(function () { + return Pouch.destroy(dbName).then(function () { + return Pouch.destroy(dbName + '_other'); + }); + }); + + it('test replication transforms incoming, and subsequent UPDATE works', function () { + + // Ongoing live replication + var replicateHandle = Pouch.replicate(remote, db, { + live: true + }); + + // We need to wait until the "incoming" change has happened. + // We'll use a re-assignable deferred object so we can wait multiple times + var d; + // Our transformation function + db.transform({ + incoming: function (doc) { + console.log("INCOMING!"); + doc.foo = 'baz'; + // Resolve anything that's waiting for the incoming handler to finish + setTimeout(function() { + d.resolve(); + }, 100); + return doc; + } + }); + + var setupPromise = new Promise(function(resolve, reject) { + // Wait for a second to give replication a chance + setTimeout(resolve, 500); + }); + + // The actual test + return setupPromise.then(function() { + // Reset the incoming listener + d = defer(); + return remote.put({_id: 'doc'}) + }).then(function() { + // Wait for the incoming listener - everything's updated + return d.promise; + }).then(function () { + return db.get('doc'); + }).then(function (doc) { + doc.foo.should.equal('baz'); + // Get the remote document so we can update it + return remote.get('doc'); + }).then(function(doc) { + // Reset the incoming listener + d = defer(); + // Add a new property to the object + return remote.put({_id: 'doc', _rev: doc._rev, moo: 'bar' }); + }).then(function() { + // Wait for the incoming listener - everything's updated + return d.promise; + }).then(function() { + return db.get('doc'); + }).then(function(doc) { + // And here - we fail + doc.should.have.property('moo'); + doc.moo.should.equal('bar'); + }); + + replicateHandle.cancel(); + + }); + + }); } From ef372c46af864f07e07238fe70f8a9adcf1f89ea Mon Sep 17 00:00:00 2001 From: Nick Maynard Date: Mon, 23 Mar 2015 08:58:57 +0000 Subject: [PATCH 2/3] Fix jshint errors --- test/test.js | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/test/test.js b/test/test.js index bee5ac2..38c937f 100644 --- a/test/test.js +++ b/test/test.js @@ -716,18 +716,18 @@ function tests(dbName, dbType) { var remote; // Utility function - complex test incoming - var defer = function() { + var defer = function () { var resolve, reject; - var promise = new Promise(function() { + var promise = new Promise(function () { resolve = arguments[0]; reject = arguments[1]; - }); + }); return { resolve: resolve, reject: reject, promise: promise - }; - }; + }; + }; beforeEach(function () { db = new Pouch(dbName); @@ -756,24 +756,24 @@ function tests(dbName, dbType) { console.log("INCOMING!"); doc.foo = 'baz'; // Resolve anything that's waiting for the incoming handler to finish - setTimeout(function() { + setTimeout(function () { d.resolve(); }, 100); return doc; } }); - var setupPromise = new Promise(function(resolve, reject) { + var setupPromise = new Promise(function (resolve) { // Wait for a second to give replication a chance setTimeout(resolve, 500); }); // The actual test - return setupPromise.then(function() { + var result = setupPromise.then(function () { // Reset the incoming listener d = defer(); - return remote.put({_id: 'doc'}) - }).then(function() { + return remote.put({_id: 'doc'}); + }).then(function () { // Wait for the incoming listener - everything's updated return d.promise; }).then(function () { @@ -782,23 +782,29 @@ function tests(dbName, dbType) { doc.foo.should.equal('baz'); // Get the remote document so we can update it return remote.get('doc'); - }).then(function(doc) { + }).then(function (doc) { // Reset the incoming listener d = defer(); // Add a new property to the object return remote.put({_id: 'doc', _rev: doc._rev, moo: 'bar' }); - }).then(function() { + }).then(function () { // Wait for the incoming listener - everything's updated return d.promise; - }).then(function() { + }).then(function () { return db.get('doc'); - }).then(function(doc) { + }).then(function (doc) { // And here - we fail doc.should.have.property('moo'); doc.moo.should.equal('bar'); }); - replicateHandle.cancel(); + + result.always(function () { + replicateHandle.cancel(); + }); + + + return result; }); From deeb1c61038628fe653169488891023ce21f0a34 Mon Sep 17 00:00:00 2001 From: Nick Maynard Date: Mon, 23 Mar 2015 09:04:25 +0000 Subject: [PATCH 3/3] Fix silly mistake in promise handling --- test/test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index 38c937f..68f8a90 100644 --- a/test/test.js +++ b/test/test.js @@ -799,7 +799,9 @@ function tests(dbName, dbType) { }); - result.always(function () { + result.then(function () { + replicateHandle.cancel(); + }, function () { replicateHandle.cancel(); });