Esempio n. 1
0
    def setUp(self):
        BaseTestCase.setUp(self)

        notifier = changes.ChangeNotifier(self.db, 'test')
        notifier.addListener(self)

        d = self.db.createDB('test')
        d.addCallback(lambda _: notifier.start())
        return d
Esempio n. 2
0
    def testChanges(self):
        notifier = changes.ChangeNotifier(self.db, 'test')
        notifier.addListener(self)

        d = notifier.start()

        def create(_):
            changeD = self.waitForChange()

            saveD = self.db.saveDoc('test', {'key': 'value'})
            saveD.addCallback(lambda r: setattr(self, 'firstid', r['id']))

            dl = defer.DeferredList([saveD, changeD])

            def check(_):
                c = self.lastChange
                self.assertEquals(c['id'], self.firstid)
                self.assertEquals(len(c['changes']), 1)
                self.assertEquals(c['changes'][0]['rev'][:2], '1-')

            dl.addCallback(check)

            dl.addCallback(lambda _: self.db.openDoc('test', self.firstid))
            dl.addCallback(lambda r: setattr(self, 'first', r))

            return dl

        d.addCallback(create)

        def update(_):
            changeD = self.waitForChange()

            self.first['key'] = 'othervalue'
            saveD = self.db.saveDoc('test', self.first, docId=self.firstid)

            dl = defer.DeferredList([saveD, changeD])

            def check(_):
                c = self.lastChange
                self.assertEquals(c['id'], self.firstid)
                self.assertEquals(len(c['changes']), 1)
                self.assertEquals(c['changes'][0]['rev'][:2], '2-')

            dl.addCallback(check)

            return dl

        d.addCallback(update)

        d.addCallback(lambda _: notifier.stop())
        d.addCallback(lambda _: self.waitForNextCycle())
        return d
Esempio n. 3
0
 def _createNotifier(self, **options):
     notifier = changes.ChangeNotifier(self.db, 'test', **options)
     notifier.addListener(self)
     return notifier
Esempio n. 4
0
    def testChangesFiltered(self):
        """
        This tests that we can use a filter to only receive notifications
        for documents that interest us.
        """
        notifier = changes.ChangeNotifier(self.db, 'test')
        notifier.addListener(self)

        d = defer.Deferred()

        filterjs = """
function(doc, req) {
    log(req.query);
    var docids = eval('(' + req.query.docids + ')');
    log(docids);
    if (docids.indexOf(doc._id) > -1) {
        return true;
    } else {
        return false;
    }
}
"""

        d.addCallback(lambda _: self.db.saveDoc('test', {
            'filters': {
                "test": filterjs,
            },
        }, '_design/design_doc'))

        d.addCallback(lambda _: notifier.start(filter='design_doc/test',
                                               docids=client.json.dumps([
                                                   'one',
                                               ])))

        def create(_):
            changeD = self.waitForChange()

            saveD = self.db.saveDoc('test', {'key': 'value'}, docId='one')
            saveD.addCallback(lambda r: setattr(self, 'firstid', r['id']))

            dl = defer.DeferredList([saveD, changeD])

            def check(_):
                c = self.lastChange
                self.assertEquals(c['id'], self.firstid)
                self.assertEquals(len(c['changes']), 1)
                self.assertEquals(c['changes'][0]['rev'][:2], '1-')
                self.assertEquals(c['seq'], 2)

            dl.addCallback(check)

            dl.addCallback(lambda _: self.db.openDoc('test', self.firstid))
            dl.addCallback(lambda r: setattr(self, 'first', r))

            return dl

        d.addCallback(create)

        def update(_):
            changeD = self.waitForChange()

            self.first['key'] = 'othervalue'
            saveD = self.db.saveDoc('test', self.first, docId=self.firstid)
            saveD.addCallback(lambda r: setattr(self, 'firstrev', r['rev']))

            dl = defer.DeferredList([saveD, changeD])

            def check(_):
                c = self.lastChange
                self.assertEquals(c['id'], self.firstid)
                self.assertEquals(len(c['changes']), 1)
                self.assertEquals(c['changes'][0]['rev'][:2], '2-')
                self.assertEquals(c['seq'], 3)

            dl.addCallback(check)

            return dl

        d.addCallback(update)

        def createTwoAndUpdateOne(_):
            # since createTwo is not supposed to trigger a change, we can't
            # assert that it didn't until we make another change that is
            # detected.
            changeD = self.waitForChange()

            saveD = self.db.saveDoc('test', {'key': 'othervalue'}, docId='two')

            def update(_):
                self.first['key'] = 'thirdvalue'
                self.first['_rev'] = self.firstrev
                return self.db.saveDoc('test', self.first, docId=self.firstid)

            saveD.addCallback(update)

            dl = defer.DeferredList([saveD, changeD])

            # FIXME: this failure actually gets swallowed, even though
            # DeferredList should not do that; so force DeferredList to fail
            # to reproduce, remove the line that updates firstrev, and
            # don't add the eb below

            def eb(failure):
                dl.errback(failure)
                # self.fail('Could not update: %r' % failure)
                return failure

            saveD.addErrback(eb)

            def check(_):
                c = self.lastChange
                self.assertEquals(c['id'], self.firstid)
                self.assertEquals(len(c['changes']), 1)
                self.assertEquals(c['changes'][0]['rev'][:2], '3-')
                # Note that we didn't receive change with seq 4,
                # which was the creation of doc two
                self.assertEquals(c['seq'], 5)

            dl.addCallback(check)

            return dl

        d.addCallback(createTwoAndUpdateOne)
        d.addCallback(lambda _: notifier.stop())
        d.addCallback(lambda _: self.waitForNextCycle())

        d.callback(None)
        return d