Esempio n. 1
0
 def setUp(self):
     """
     Create a L{MemCachePool}.
     """
     TestCase.setUp(self)
     self.reactor = StubReactor()
     self.pool = MemCachePool(
         TCP4ClientEndpoint(self.reactor, MC_ADDRESS.host, MC_ADDRESS.port),
         maxClients=5, reactor=self.reactor
     )
     realClientFactory = self.pool.clientFactory
     self.clientFactories = []
     def capturingClientFactory(*a, **k):
         cf = realClientFactory(*a, **k)
         self.clientFactories.append(cf)
         return cf
     self.pool.clientFactory = capturingClientFactory
 def setUp(self):
     """
     Create a L{MemCachePool}.
     """
     TestCase.setUp(self)
     self.reactor = StubReactor()
     self.pool = MemCachePool(MC_ADDRESS,
                              maxClients=5,
                              reactor=self.reactor)
Esempio n. 3
0
 def setUp(self):
     """
     Create a L{MemCachePool}.
     """
     TestCase.setUp(self)
     self.reactor = StubReactor()
     self.pool = MemCachePool(
         TCP4ClientEndpoint(self.reactor, MC_ADDRESS.host, MC_ADDRESS.port),
         maxClients=5, reactor=self.reactor
     )
     realClientFactory = self.pool.clientFactory
     self.clientFactories = []
     def capturingClientFactory(*a, **k):
         cf = realClientFactory(*a, **k)
         self.clientFactories.append(cf)
         return cf
     self.pool.clientFactory = capturingClientFactory
Esempio n. 4
0
class MemCachePoolTests(TestCase):
    """
    Tests for L{MemCachePool}.

    @ivar reactor: A L{StubReactor} instance.
    @ivar pool: A L{MemCachePool} for testing.
    """
    def setUp(self):
        """
        Create a L{MemCachePool}.
        """
        TestCase.setUp(self)
        self.reactor = StubReactor()
        self.pool = MemCachePool(
            TCP4ClientEndpoint(self.reactor, MC_ADDRESS.host, MC_ADDRESS.port),
            maxClients=5, reactor=self.reactor
        )
        realClientFactory = self.pool.clientFactory
        self.clientFactories = []
        def capturingClientFactory(*a, **k):
            cf = realClientFactory(*a, **k)
            self.clientFactories.append(cf)
            return cf
        self.pool.clientFactory = capturingClientFactory


    def test_clientFreeAddsNewClient(self):
        """
        Test that a client not in the busy set gets added to the free set.
        """
        p = MemCacheClientFactory().buildProtocol(None)
        self.pool.clientFree(p)

        self.assertEquals(self.pool._freeClients, set([p]))


    def test_clientFreeAddsBusyClient(self):
        """
        Test that a client in the busy set gets moved to the free set.
        """
        p = MemCacheClientFactory().buildProtocol(None)

        self.pool.clientBusy(p)
        self.pool.clientFree(p)

        self.assertEquals(self.pool._freeClients, set([p]))
        self.assertEquals(self.pool._busyClients, set([]))


    def test_clientBusyAddsNewClient(self):
        """
        Test that a client not in the free set gets added to the busy set.
        """
        p = MemCacheClientFactory().buildProtocol(None)
        self.pool.clientBusy(p)

        self.assertEquals(self.pool._busyClients, set([p]))


    def test_clientBusyAddsFreeClient(self):
        """
        Test that a client in the free set gets moved to the busy set.
        """
        p = MemCacheClientFactory().buildProtocol(None)

        self.pool.clientFree(p)
        self.pool.clientBusy(p)

        self.assertEquals(self.pool._busyClients, set([p]))
        self.assertEquals(self.pool._freeClients, set([]))


    def test_clientGoneRemovesFreeClient(self):
        """
        Test that a client in the free set gets removed when
        L{MemCachePool.clientGone} is called.
        """
        p = MemCacheClientFactory().buildProtocol(None)
        self.pool.clientFree(p)
        self.assertEquals(self.pool._freeClients, set([p]))
        self.assertEquals(self.pool._busyClients, set([]))

        self.pool.clientGone(p)
        self.assertEquals(self.pool._freeClients, set([]))


    def test_clientGoneRemovesBusyClient(self):
        """
        Test that a client in the busy set gets removed when
        L{MemCachePool.clientGone} is called.
        """
        p = MemCacheClientFactory().buildProtocol(None)
        self.pool.clientBusy(p)
        self.assertEquals(self.pool._busyClients, set([p]))
        self.assertEquals(self.pool._freeClients, set([]))

        self.pool.clientGone(p)
        self.assertEquals(self.pool._busyClients, set([]))


    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')])


    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


    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)

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

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

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


    def test_pendingConnectionsCountAgainstMaxClients(self):
        """
        Test that L{MemCachePool.performRequest} will not initiate a new
        connection if there are pending connections that count towards max
        clients.
        """
        self.pool.suggestMaxClients(1)

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

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

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

        self.pool.performRequest('get', 'bar')
        self.assertEquals(self.reactor.calls, [])
Esempio n. 5
0
class MemCachePoolTests(TestCase):
    """
    Tests for L{MemCachePool}.

    @ivar reactor: A L{StubReactor} instance.
    @ivar pool: A L{MemCachePool} for testing.
    """
    def setUp(self):
        """
        Create a L{MemCachePool}.
        """
        TestCase.setUp(self)
        self.reactor = StubReactor()
        self.pool = MemCachePool(
            TCP4ClientEndpoint(self.reactor, MC_ADDRESS.host, MC_ADDRESS.port),
            maxClients=5, reactor=self.reactor
        )
        realClientFactory = self.pool.clientFactory
        self.clientFactories = []
        def capturingClientFactory(*a, **k):
            cf = realClientFactory(*a, **k)
            self.clientFactories.append(cf)
            return cf
        self.pool.clientFactory = capturingClientFactory


    def test_clientFreeAddsNewClient(self):
        """
        Test that a client not in the busy set gets added to the free set.
        """
        p = MemCacheClientFactory().buildProtocol(None)
        self.pool.clientFree(p)

        self.assertEquals(self.pool._freeClients, set([p]))


    def test_clientFreeAddsBusyClient(self):
        """
        Test that a client in the busy set gets moved to the free set.
        """
        p = MemCacheClientFactory().buildProtocol(None)

        self.pool.clientBusy(p)
        self.pool.clientFree(p)

        self.assertEquals(self.pool._freeClients, set([p]))
        self.assertEquals(self.pool._busyClients, set([]))


    def test_clientBusyAddsNewClient(self):
        """
        Test that a client not in the free set gets added to the busy set.
        """
        p = MemCacheClientFactory().buildProtocol(None)
        self.pool.clientBusy(p)

        self.assertEquals(self.pool._busyClients, set([p]))


    def test_clientBusyAddsFreeClient(self):
        """
        Test that a client in the free set gets moved to the busy set.
        """
        p = MemCacheClientFactory().buildProtocol(None)

        self.pool.clientFree(p)
        self.pool.clientBusy(p)

        self.assertEquals(self.pool._busyClients, set([p]))
        self.assertEquals(self.pool._freeClients, set([]))


    def test_clientGoneRemovesFreeClient(self):
        """
        Test that a client in the free set gets removed when
        L{MemCachePool.clientGone} is called.
        """
        p = MemCacheClientFactory().buildProtocol(None)
        self.pool.clientFree(p)
        self.assertEquals(self.pool._freeClients, set([p]))
        self.assertEquals(self.pool._busyClients, set([]))

        self.pool.clientGone(p)
        self.assertEquals(self.pool._freeClients, set([]))


    def test_clientGoneRemovesBusyClient(self):
        """
        Test that a client in the busy set gets removed when
        L{MemCachePool.clientGone} is called.
        """
        p = MemCacheClientFactory().buildProtocol(None)
        self.pool.clientBusy(p)
        self.assertEquals(self.pool._busyClients, set([p]))
        self.assertEquals(self.pool._freeClients, set([]))

        self.pool.clientGone(p)
        self.assertEquals(self.pool._busyClients, set([]))


    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')])


    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


    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)

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

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

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


    def test_pendingConnectionsCountAgainstMaxClients(self):
        """
        Test that L{MemCachePool.performRequest} will not initiate a new
        connection if there are pending connections that count towards max
        clients.
        """
        self.pool.suggestMaxClients(1)

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

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

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

        self.pool.performRequest('get', 'bar')
        self.assertEquals(self.reactor.calls, [])