Example #1
0
    def test_performRequestUsesFreeConnection(self):
        """
        Test that L{MemCachePool.performRequest} doesn't create a new connection
        to be created if there is a free connection.
        """
        def _checkResult(result):
            self.assertEquals(result, (0, 'bar'))
            self.assertEquals(self.reactor.calls, [])

        p = InMemoryMemcacheProtocol()
        p.set('foo', 'bar')

        self.pool.clientFree(p)

        d = self.pool.performRequest('get', 'foo')
        d.addCallback(_checkResult)
        return d
Example #2
0
    def test_performRequestCreatesConnection(self):
        """
        Test that L{MemCachePool.performRequest} on a fresh instance causes
        a new connection to be created.
        """
        results = []
        p = InMemoryMemcacheProtocol()
        p.set('foo', 'bar')

        d = self.pool.performRequest('get', 'foo')
        d.addCallback(results.append)

        args, _ignore_kwargs = self.reactor.calls.pop()

        self.assertEquals(args[:2], (MC_ADDRESS.host, MC_ADDRESS.port))

        self.clientFactories[-1].deferred.callback(p)
        self.assertEquals(results, [(0, 'bar')])
Example #3
0
    def setUp(self):
        super(MemcacheResponseCacheTests, self).setUp()

        memcacheStub = InMemoryMemcacheProtocol()
        self.rc = MemcacheResponseCache(None, cachePool=memcacheStub)
        self.tokens = {}

        self.tokens['/calendars/__uids__/cdaboo/'] = 'uriToken0'
        self.tokens['/calendars/__uids__/cdaboo/calendars/'] = 'childToken0'
        self.tokens['/principals/__uids__/cdaboo/'] = 'principalToken0'
        self.tokens['/principals/__uids__/dreid/'] = 'principalTokenX'

        def _getToken(uri, cachePoolHandle=None):
            return succeed(self.tokens.get(uri))

        self.rc._tokenForURI = _getToken

        self.expected_response = (200, Headers({}), "Foo")

        expected_key = hashlib.md5(':'.join([str(t) for t in (
            'PROPFIND',
            '/principals/__uids__/cdaboo/',
            '/calendars/__uids__/cdaboo/',
            '1',
            hash('foobar'),
        )])).hexdigest()

        memcacheStub._cache[expected_key] = (
            0, #flags
            cPickle.dumps((
                'principalToken0',
                StubDirectoryRecord('cdaboo').cacheToken(),
                'uriToken0',
                {'/calendars/__uids__/cdaboo/calendars/': 'childToken0'},
                (
                    self.expected_response[0],
                    dict(list(self.expected_response[1].getAllRawHeaders())),
                    self.expected_response[2]
                )
            ))
        )

        self.memcacheStub = memcacheStub
Example #4
0
    def test_performRequestMaxBusyQueuesRequest(self):
        """
        Test that L{MemCachePool.performRequest} queues the request if
        all clients are busy.
        """
        def _checkResult(result):
            self.assertEquals(result, (0, 'bar'))
            self.assertEquals(self.reactor.calls, [])

        p = InMemoryMemcacheProtocol()
        p.set('foo', 'bar')

        p1 = InMemoryMemcacheProtocol()
        p1.set('foo', 'baz')

        self.pool.suggestMaxClients(2)

        self.pool.clientBusy(p)
        self.pool.clientBusy(p1)

        d = self.pool.performRequest('get', 'foo')
        d.addCallback(_checkResult)

        self.pool.clientFree(p)

        return d
    def test_performRequestCreatesConnectionsUntilMaxBusy(self):
        """
        Test that L{MemCachePool.performRequest} will create new connections
        until it reaches the maximum number of busy clients.
        """
        def _checkResult(result):
            self.assertEquals(result, (0, 'baz'))

        self.pool.suggestMaxClients(2)

        p = InMemoryMemcacheProtocol()
        p.set('foo', 'bar')

        p1 = InMemoryMemcacheProtocol()
        p1.set('foo', 'baz')


        self.pool.clientBusy(p)

        d = self.pool.performRequest('get', 'foo')

        args, kwargs = self.reactor.calls.pop()

        self.assertEquals(args[:2], (MC_ADDRESS.host, MC_ADDRESS.port))
        self.failUnless(isinstance(args[2], MemCacheClientFactory))
        self.assertEquals(kwargs, {})

        args[2].deferred.callback(p1)

        return d
    def test_performRequestCreatesConnection(self):
        """
        Test that L{MemCachePool.performRequest} on a fresh instance causes
        a new connection to be created.
        """
        def _checkResult(result):
            self.assertEquals(result, (0, 'bar'))


        p = InMemoryMemcacheProtocol()
        p.set('foo', 'bar')

        d = self.pool.performRequest('get', 'foo')
        d.addCallback(_checkResult)

        args, kwargs = self.reactor.calls.pop()

        self.assertEquals(args[:2], (MC_ADDRESS.host, MC_ADDRESS.port))
        self.failUnless(isinstance(args[2], MemCacheClientFactory))
        self.assertEquals(kwargs, {})

        args[2].deferred.callback(p)

        return d
    def test_performRequestCreatesConnectionsUntilMaxBusy(self):
        """
        Test that L{MemCachePool.performRequest} will create new connections
        until it reaches the maximum number of busy clients.
        """
        def _checkResult(result):
            self.assertEquals(result, (0, 'baz'))

        self.pool.suggestMaxClients(2)

        p = InMemoryMemcacheProtocol()
        p.set('foo', 'bar')

        p1 = InMemoryMemcacheProtocol()
        p1.set('foo', 'baz')

        self.pool.clientBusy(p)

        self.pool.performRequest('get', 'foo')

        args, _ignore_kwargs = self.reactor.calls.pop()

        self.assertEquals(args[:2], (MC_ADDRESS.host, MC_ADDRESS.port))
Example #8
0
    def test_performRequestCreatesConnectionsUntilMaxBusy(self):
        """
        Test that L{MemCachePool.performRequest} will create new connections
        until it reaches the maximum number of busy clients.
        """
        def _checkResult(result):
            self.assertEquals(result, (0, 'baz'))

        self.pool.suggestMaxClients(2)

        p = InMemoryMemcacheProtocol()
        p.set('foo', 'bar')

        p1 = InMemoryMemcacheProtocol()
        p1.set('foo', 'baz')

        self.pool.clientBusy(p)

        self.pool.performRequest('get', 'foo')

        args, _ignore_kwargs = self.reactor.calls.pop()

        self.assertEquals(args[:2], (MC_ADDRESS.host, MC_ADDRESS.port))
 def setUp(self):
     super(MemcacheTests, self).setUp()
     self.memcache = InMemoryMemcacheProtocol()
     self.db.reserver = MemcachedUIDReserver(self.db, self.memcache)