Exemple #1
0
class DecoratorTestCase(unittest.TestCase):
    """Base test class"""
    def setUp(self):
        self.client = InprocCacheClient(100)
        self.client2 = InprocCacheClient(100)
        self.tiered_client = TieredInclusiveClient(self.client2, self.client)

    def tearDown(self):
        self.client.clear()
        self.client2.clear()
Exemple #2
0
    def setUpClass(cls):
        ipsub.IPSub.register_default_pyobj()

        ctx = zmq.Context.instance()
        s1 = ctx.socket(zmq.REQ)
        s2 = ctx.socket(zmq.REQ)
        port1 = s1.bind_to_random_port("tcp://127.0.0.1")
        port2 = s2.bind_to_random_port("tcp://127.0.0.1")
        s1.close()
        s2.close()
        del s1, s2

        cls.ipsub = ipsub.IPSub([
            dict(rep="tcp://127.0.0.1:%d" % port1,
                 pub="tcp://127.0.0.1:%d" % port2)
        ],
                                ctx=ctx)
        cls.ipsub_thread = threading.Thread(target=cls.ipsub.run)
        cls.ipsub_thread.daemon = True

        cls.ipsub2 = ipsub.IPSub([
            dict(rep="tcp://127.0.0.1:%d" % port1,
                 pub="tcp://127.0.0.1:%d" % port2)
        ],
                                 ctx=ctx)
        cls.ipsub2_thread = threading.Thread(target=cls.ipsub2.run)
        cls.ipsub2_thread.daemon = True

        cls.private = InprocCacheClient(100)
        cls.private2 = InprocCacheClient(100)
        cls.shared = InprocCacheClient(100)

        time.sleep(0.1)

        cls.ipsub_thread.start()
        cls.ipsub2_thread.start()

        for i in xrange(11):
            time.sleep(0.1)
            if cls.ipsub2.is_running and cls.ipsub.is_running:
                break
        time.sleep(0.1)
Exemple #3
0
class CoherentDeferTest(unittest.TestCase):
    def setUp(self):
        self.private = InprocCacheClient(100)
        self.shared = InprocCacheClient(100)
        self.tiered = TieredInclusiveClient(self.private, self.shared)
        self.called = False

    def _call_target(self):
        self.called = True

    def test_stale_reget(self):
        key = 'blarg'
        self.private.put(key, 10, 30)
        self.shared.put(key, 15, 300)
        self.assertTrue(self.shared.contains(key, 60))
        self.assertTrue(self.tiered.contains(key, 60))
        self.assertEqual(self.tiered.get(key), 10)

        defer = coherent.CoherentDefer(
            self._call_target,
            key=key,
            manager=None,
            expired=lambda: not self.shared.contains(key, 60),
            expire_private=self.private.expire,
            timeout=1)
        defer.future = async .Future()
        value = defer.undefer()

        self.assertIs(value, async .REGET)
        self.assertEqual(self.tiered.get(key), 15)
Exemple #4
0
    def testGetPromoteCB(self):
        from chorde.clients.inproc import InprocCacheClient
        from chorde.clients.tiered import TieredInclusiveClient
        client = self.client
        client2 = TieredInclusiveClient(InprocCacheClient(100), client)
        client.put(4, 10, 10)
        promotions = []

        def register_promotion(key, value, ttl):
            promotions.append((key, value))

        self.assertRaises(CacheMissError, client.get, 3)
        self.assertEqual(
            client.get(3, None, promote_callback=register_promotion), None)
        self.assertEqual(client.get(3, 1, promote_callback=register_promotion),
                         1)
        self.assertEqual(client.get(4, 1, promote_callback=register_promotion),
                         10)
        self.assertEqual(promotions, [])
        self.assertEqual(
            client2.get(4, 1, promote_callback=register_promotion), 10)
        self.assertEqual(promotions, [(4, 10)])
Exemple #5
0
 def setUp(self):
     self.client = InprocCacheClient(100)
     self.client2 = InprocCacheClient(100)
     self.tiered_client = TieredInclusiveClient(self.client2, self.client)
Exemple #6
0
 def setUpClient(self):
     from chorde.clients.inproc import InprocCacheClient
     from chorde.clients.tiered import TieredInclusiveClient
     self.l1 = InprocCacheClient(100)
     self.l2 = InprocCacheClient(100)
     return TieredInclusiveClient(self.l1, self.l2)
Exemple #7
0
class TieredTest(CacheClientTestMixIn, unittest.TestCase):
    def setUpClient(self):
        from chorde.clients.inproc import InprocCacheClient
        from chorde.clients.tiered import TieredInclusiveClient
        self.l1 = InprocCacheClient(100)
        self.l2 = InprocCacheClient(100)
        return TieredInclusiveClient(self.l1, self.l2)

    def testReadThrough(self):
        self.l2.put(15, 3, 10)
        self.assertEqual(3, self.client.get(15))

    def testWriteThrough(self):
        self.client.put(14, 4, 10)
        self.assertEqual(4, self.l1.get(14))
        self.assertEqual(4, self.l2.get(14))

    def testTTLSkip(self):
        self.l2.put(5, 1, 10)
        self.l1.put(5, 2, 2)
        self.assertEqual(1, self.client.get(5, ttl_skip=5))

    def testPromote(self):
        self.l2.put(13, 5, 10)
        self.assertEqual(5, self.client.get(13))
        self.assertEqual(5, self.l1.get(13))

    def testPromoteCallback(self):
        notified = []

        def callback(key, rv, ttl):
            notified.append((key, rv))

        self.l2.put(12, 6, 10)
        self.assertEqual(6, self.client.get(12, promote_callback=callback))
        self.assertEqual(notified, [(12, 6)])

    def testReadThroughMulti(self):
        self.l2.put(15, 3, 10)
        self.l2.put(16, 4, 10)
        self.assertItemsEqual([(14, None), (15, 3), (16, 4)],
                              list(self.client.getMulti([14, 15, 16], None)))

    def testTTLSkipMulti(self):
        self.l2.put(5, 1, 10)
        self.l1.put(5, 2, 2)
        self.l2.put(6, 3, 10)
        self.l1.put(6, 4, 2)
        self.l2.put(7, 5, 3)
        self.l1.put(7, 6, 2)
        self.l2.put(8, 7, 7)
        self.l1.put(8, 8, 10)
        self.assertItemsEqual([(4, None), (5, 1), (6, 3), (7, None), (8, 8)],
                              list(
                                  self.client.getMulti([4, 5, 6, 7, 8],
                                                       None,
                                                       ttl_skip=5)))

    def testPromoteMulti(self):
        self.l2.put(13, 5, 10)
        self.assertEqual(5, self.client.get(13))
        self.assertEqual(5, self.l1.get(13))

    def testPromoteCallbackMulti(self):
        notified = []

        def callback(key, rv, ttl):
            notified.append((key, rv))

        self.l2.put(12, 6, 10)
        self.assertEqual(6, self.client.get(12, promote_callback=callback))
        self.assertEqual(notified, [(12, 6)])

    def testLRU(self):
        client = self.client
        cap = client.capacity[0]

        for i in xrange(cap):
            client.put(i, i, 86400)
            self.assertEqual(client.usage[0], i + 1)

        self.assertTrue(client.contains(0))
        client.put(cap, cap, 86400)

        # 1 should still be on the l2
        # The touch on 0 only happens against the l1,
        # so l1 evits 1, l2 evicts 0
        self.assertTrue(client.contains(1))
        self.assertTrue(client.contains(0))

        self.assertFalse(self.l1.contains(1))
        self.assertTrue(self.l1.contains(0))

        self.assertTrue(self.l2.contains(1))
        self.assertFalse(self.l2.contains(0))

    def testLimit(self):
        client = self.client
        cap = client.capacity[0]

        self.assertEqual(client.usage[0], 0)

        for i in xrange(cap):
            client.put(i, i, 86400)
            self.assertEqual(client.usage[0], i + 1)

        for i in xrange(cap):
            i += cap
            client.put(i, i, 86400)
            self.assertEqual(client.usage[0], cap)

    def testPurge(self):
        client = self.client
        client.put(4, 15, 0)
        client.put(5, 16, 2)
        for c in client.clients:
            self.assertIsNotNone(c.getTtl(4, None)[0])
            self.assertEqual(c.get(5), 16)
        client.purge(86400)
        for c in client.clients:
            self.assertIsNotNone(c.getTtl(4, None)[0])
            self.assertEqual(c.get(5), 16)
        client.purge(0)
        for c in client.clients:
            self.assertIsNone(c.getTtl(4, None)[0])
            self.assertEqual(c.get(5), 16)
Exemple #8
0
 def setUpClient(self):
     from chorde.clients.inproc import InprocCacheClient
     from clru.cuckoocache.cycuckoocache import LazyCuckooCache
     return InprocCacheClient(100, store_class=LazyCuckooCache)
Exemple #9
0
 def setUpClient(self):
     from chorde.clients.inproc import InprocCacheClient
     return InprocCacheClient(100, store_class=pylrucache.LRUCache)
Exemple #10
0
 def setUpClient(self):
     from chorde.clients.inproc import InprocCacheClient
     return InprocCacheClient(100)
Exemple #11
0
 def setUpClient(self):
     from chorde.clients.inproc import InprocCacheClient
     from chorde.clients.async import AsyncWriteCacheClient
     return AsyncWriteCacheClient(InprocCacheClient(100), 100, 1)
Exemple #12
0
 def setUp(self):
     from chorde.clients.async import AsyncCacheProcessor
     from chorde.clients.inproc import InprocCacheClient
     from chorde.threadpool import ThreadPool
     self.client = AsyncCacheProcessor(1, InprocCacheClient(100),
         threadpool = functools.partial(ThreadPool(1).subqueue, "meh"))
Exemple #13
0
 def setUp(self):
     from chorde.clients.async import AsyncCacheProcessor
     from chorde.clients.inproc import InprocCacheClient
     self.client = AsyncCacheProcessor(1, InprocCacheClient(100))
Exemple #14
0
 def setUpClient(self):
     from chorde.clients.inproc import InprocCacheClient
     from chorde.clients.async import AsyncWriteCacheClient
     from chorde.threadpool import ThreadPool
     return AsyncWriteCacheClient(InprocCacheClient(100), 100, 1,
         threadpool = functools.partial(ThreadPool(1).subqueue, "meh"))
Exemple #15
0
 def setUp(self):
     self.private = InprocCacheClient(100)
     self.shared = InprocCacheClient(100)
     self.tiered = TieredInclusiveClient(self.private, self.shared)
     self.called = False
Exemple #16
0
 def ClientClass(*p, **kw):
     return InprocCacheClient(*p, store_class=CuckooCache, **kw)