Skip to content

Instantly share code, notes, and snippets.

@zoepage
Created June 10, 2015 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoepage/8f71bf9d35b2cbfed929 to your computer and use it in GitHub Desktop.
Save zoepage/8f71bf9d35b2cbfed929 to your computer and use it in GitHub Desktop.
test for error eventEmmiter
'use strict'
var toId = require('./utils/to-id')
module.exports = push
/**
* pushes one or multiple objects from local to remote database
*
* @param {String,Object} docsOrIds object, ID of object or array of objects/ids (all optional)
* @return {Promise}
*/
function push (state, options, docsOrIds) {
var pushedObjects = []
var Promise = this.constructor.utils.Promise
var errors = this.constructor.Errors
var defer = {}
defer.promise = new Promise(function (resolveCallback, rejectCallback) {
defer.resolve = function resolve () {
resolveCallback.apply(null, arguments)
}
defer.reject = function reject () {
rejectCallback.apply(null, arguments)
}
})
if (Array.isArray(docsOrIds)) {
docsOrIds = docsOrIds.map(toId)
} else {
docsOrIds = docsOrIds && [toId(docsOrIds)]
}
if (docsOrIds && docsOrIds.filter(Boolean).length !== docsOrIds.length) {
return Promise.reject(errors.NOT_AN_OBJECT)
}
var replication = this.replicate.to(options.remote, {
create_target: true,
doc_ids: docsOrIds,
include_docs: true,
retry: false
})
replication.on('complete', function () {
defer.resolve(pushedObjects)
})
replication.on('error', defer.reject)
replication.on('change', function (change) {
pushedObjects = pushedObjects.concat(change.docs)
for (var i = 0; i < change.docs.length; i++) {
state.emitter.emit('push', change.docs[i])
}
})
return defer.promise
}
test.only('api.push({}) error event', function (t) {
t.plan(1)
var db = dbFactory('hoodieDB8')
var PouchDB = db.constructor
var remoteName = PouchDB.utils.uuid(10)
var api = db.hoodieSync({remote: remoteName})
var obj1 = {_id: 'test1', foo1: 'bar1'}
var obj2 = {_id: 'test2', foo1: 'bar2'}
db.bulkDocs([obj1, obj2])
db.destroy()
.then(function () {
return api.push()
})
.catch(function (error) {
t.pass(error)
})
})
@nolanlawson
Copy link

Just FYI, utils.uuid may be removed in the future. We can move it to the "extras" API if that helps!

It seems the bug may be that you're destroying the database at the same time that you're inserting documents in it. Did you mean:

  db.bulkDocs([obj1, obj2]).then(function () {
    return db.destroy();
  }).then(function () {
    return api.push()
  }).catch(function (error) {
    t.pass(error)
  })  

?

@zoepage
Copy link
Author

zoepage commented Jun 10, 2015

re: utils.uuid thanks for the hint. good to know :)

This makes sense, yes 😄 But when the db gets destroyed, I can't push, so the error event doesn't get fired. Just trying to wrap my head around, how to get the test passed and catch the error.

Not sure, if destroying the db is the right approach here...

@nolanlawson
Copy link

Hm, why do you expect an error in the first place? Guess I don't understand what the test is supposed to capture...

And yeah, destroy() is actually the trickiest part of the PouchDB API, especially in IndexedDB. When you delete a complete database in IndexedDB (indexedDB.deleteDatabase()), wild stuff happens, and ongoing transactions throw bizarre errors. For that reason, I've considered replacing it with a "soft" delete that doesn't destroy the entire database but rather removes all the objects, but I haven't gotten that far yet.

@zoepage
Copy link
Author

zoepage commented Jun 11, 2015

I think, the most common scenarios would be:

  1. the remote DB dies during a push/pull/sync
  2. because of a merge conflict (would the error event fire here?)

@nolanlawson
Copy link

Wait, so what you're saying is that you've seen bulkDocs() throw an error in production somewhere, and now you're trying to reproduce it? Or are you trying to capture an error event from the replicate()?

As for merge conflicts: that wouldn't throw an error, but yes, if the remote DB dies (or the user goes offline), that would cause an error to be thrown, at least during non-live, non-retry replication.

Here's how we simulate the user going offline in one of our tests. Let me know if that helps.

@zoepage
Copy link
Author

zoepage commented Jun 11, 2015

Or are you trying to capture an error event from the replicate()? THIS! :)

The test looks like exactly what we need. Thanks! I'll give it a try. :) 👍

@zoepage
Copy link
Author

zoepage commented Jun 18, 2015

Just FYI... This is my solution:

test('api.push() error', function (t) {
  t.plan(1)
  var db = dbFactory('hoodieDB8')
  var PouchDB = db.constructor
  var remoteName = PouchDB.utils.uuid(10)
  var api = db.hoodieSync({remote: remoteName})

  var obj1 = {_id: 'test1', foo1: 'bar1'}

  var first = true
  var data = {
    get _id () {
      if (first) {
        first = false
        return 'test1'
      } else {
        return {}
      }
    },
    foo: 'bar'
  }

  db.bulkDocs([data, obj1])
  .then(function () {
    return api.push(data)
  })
  .then(
    function (resolve) {
      t.pass('The error event was not fired!')
    },
    function (reject) {
      t.pass('The error event was fired!')
    }
  )
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment