Esempio n. 1
0
    def test_non_empty_poll(self):
        jslink = JsLink.JsLink(self.linkFactory)

        data = doRequest(jslink, data=[{"type": "handshake"}])

        assert data['clientId']

        clientId = ObjectId(data['clientId'])

        val = BlobVal('bar')
        val.addref(clientId)
        val.large_blob = 2

        DB.update(self.ctx.database.clients,
                  {'_id': clientId},
                  {'$set': {'updates': [{'foo': 42, 'bar': val}]}})
        self.sync()

        assert DB.find(self.database.blobvals.files,
                       {'metadata.references.value': {'$in': [clientId]}}).count() == 1

        data = doRequest(jslink, data=[{
                'type': 'poll',
                'clientId' : str(clientId),
                }])
        self.sync()

        assert data == [{'foo': 42,
                         'bar': {'size': 3,
                                 'content_type': None,
                                 'filename': None}}]
        assert DB.find(self.database.blobvals.files,
                       {'metadata.references.value': {'$in': [clientId]}}).count() == 0
Esempio n. 2
0
    def test_poll_touches_timestamp(self, monkeypatch):
        class LinkFactory(object):
            def iter(self, spec):
                return []
        jslink = JsLink.JsLink(LinkFactory())

        now = 1
        monkeypatch.setattr(time, 'time', lambda : now)

        data = doRequest(jslink, data=[{"type": "handshake"}])
        clientId = data['clientId']
        self.sync()

        timestamp = self.ctx.database.clients.find_one(
            {'_id': ObjectId(clientId)})['timestamp']
        assert timestamp == 1 # sanity, set by handshake

        now = 2

        doRequest(jslink, data=[{'type': 'poll',
                                 'clientId' : clientId}])
        self.sync()
        timestamp = self.ctx.database.clients.find_one(
            {'_id': ObjectId(clientId)})['timestamp']
        assert timestamp == 1 # untouched

        now += jslink.TOUCH_INTERVAL

        doRequest(jslink, data=[{'type': 'poll',
                                 'clientId' : clientId}])
        self.sync()
        timestamp = self.ctx.database.clients.find_one(
            {'_id': ObjectId(clientId)})['timestamp']
        assert timestamp == now # touched
Esempio n. 3
0
    def test_bad_clientId(self):
        jslink = JsLink.JsLink(self.linkFactory)

        #req = FakeRequest("/jslink")

        clientId = 'Unknown_client_ID'

        resp = invokeRender(jslink, data=[{
                        'type': 'poll',
                        'clientId' : clientId,
                        }])

        assert resp.status_code == 403
Esempio n. 4
0
    def test_poll_reruns_outdated_links(self, monkeypatch):
        links = []
        class LinkFactory():
            class Link(object):
                def __init__(self, clientId, linkId):
                    self.clientId = clientId
                    self.linkId = linkId
                    self.ran = False
                def run(_self):
                    DB.update(self.ctx.database.clients,
                              {'_id': ObjectId(clientId)},
                              {'$push': {'updates': {'update': _self.linkId}}})

                    _self.ran = True

            def create(name, clientId, linkId):
                links.append(self.Link(clientId, linkId))
                return links[-1]

            def iter(_self, query):
                assert query['outdatedBy']
                assert query['client'] == ObjectId(clientId)
                _links = [_self.Link(link['client'], link['link']) for link in
                          self.ctx.database.links.find(query)]
                links.extend(_links)
                return _links

        jslink = JsLink.JsLink(LinkFactory())
        data = doRequest(jslink, data=[{"type": "handshake"}])
        clientId = data['clientId']

        DB.update(self.ctx.database.clients,
                  {'_id': ObjectId(clientId)},
                  {'$set': {'updates': [{'foo': 42}]}})
        DB.insert(self.ctx.database.links, {'client': ObjectId(clientId),
                                            'link': 1,
                                            'outdatedBy': None})
        DB.insert(self.ctx.database.links, {'client': ObjectId(clientId),
                                            'link': 2,
                                            'outdatedBy': ObjectId()})
        self.sync()

        data = doRequest(jslink, data=[{
                'type': 'poll',
                'clientId' : clientId,
                }])

        assert data == [{'foo': 42}, {'update': 2}]
        assert len(links) == 1
        assert links[0].linkId == 2
        assert links[0].ran
Esempio n. 5
0
    def test_handshake_with_register(self, monkeypatch):
        calls = []
        def register(client, request):
            calls.append(client)

        jslink = JsLink.JsLink(lambda x:None, registerClient=register)

        monkeypatch.setattr(time, 'time', self.time)

        data = doRequest(jslink, data=[{"type": "handshake"}])

        clientId = data['clientId']
        assert data['extraInfo'] == {}

        assert calls == [ObjectId(clientId)]

        self.sync()
        client = DB.find_one(self.database.clients)
        assert str(client['_id']) == clientId
        assert client['timestamp'] == 1
        assert client['updates'] == []
Esempio n. 6
0
    def test_empty_poll(self, monkeypatch):
        jslink = JsLink.JsLink(self.linkFactory)

        data = doRequest(jslink, data=[{"type": "handshake"}])

        assert data['clientId']

        clientId = data['clientId']

        # _command = jslink.database.command
        # def command(*args, **kw):
        #     # we want to make sure no mongodb query during this poll
        #     # demands to speak to the primary
        #     assert kw.get('_use_master') == False
        #     return _command(*args, **kw)

        # monkeypatch.setattr(jslink.database, 'command', command)

        data = doRequest(jslink, data=[{
                        'type': 'poll',
                        'clientId' : clientId,
                        }])

        assert data == []
Esempio n. 7
0
    def setup_method(self, method):
        super(TestJsLinkCalls, self).setup_method(method)

        self.jslink = JsLink.JsLink(FakeLinkFactory(self), logout=self.logout)
        data = doRequest(self.jslink, data=[{"type": "handshake"}])
        self.clientId = data['clientId']
Esempio n. 8
0
 def test_simple(self):
     jslink = JsLink.JsLink(lambda x:None)