Ejemplo n.º 1
0
    def test_cache_set(self):
        test_set_key = random_str(3)
        self.assertTrue(test_cache_mgr.get(test_set_key) == test_data)

        test_set_data = {random_str(): random_str()}
        test_cache_mgr.set(test_set_key, test_set_data)
        self.assertTrue(test_cache_mgr.get(test_set_key) == test_set_data)

        test_set_data = {"test": 1}
        test_cache_mgr.set(test_set_key, test_set_data)
        self.assertTrue(test_cache_mgr.get(test_set_key) == test_set_data)

        test_set_data = {random_str(): random_str()}
        test_cache_mgr.set(test_set_key, test_set_data)
        self.assertTrue(test_cache_mgr.get(test_set_key) == test_set_data)
Ejemplo n.º 2
0
    def test_cache_pop_ls(self):
        ls_len = 3
        key_ls = [random_str(3) for _ in xrange(ls_len)]
        [test_cache_mgr.get(k) for k in key_ls]
        test_cache_mgr.pop_ls(key_ls)

        for k in key_ls:
            self.assertTrue(not test_cache_mgr.has(k))
Ejemplo n.º 3
0
    def test_cache_expire(self):
        cache_expire_second = 3
        test_cache_mgr = CacheMgr.instance(TestLoader, cache_expire_second)
        test_set_key = random_str(3)
        self.assertTrue(test_cache_mgr.get(test_set_key) == test_data)
        time.sleep(cache_expire_second * 2)
        self.assertTrue(test_cache_mgr.has(test_set_key) is None)

        self.assertTrue(test_cache_mgr.get(test_set_key) == test_data)
Ejemplo n.º 4
0
    def test_cache_expire_extend(self):
        cache_expire_second = 3
        test_cache_mgr = CacheMgr.instance(TestLoader, cache_expire_second)
        test_set_key = random_str(3)
        self.assertTrue(test_cache_mgr.get(test_set_key) == test_data)

        reget_times = 3
        for _ in xrange(reget_times):
            time.sleep(cache_expire_second)
            test_cache_mgr.get(test_set_key)

        self.assertTrue(test_cache_mgr.has(test_set_key))
Ejemplo n.º 5
0
    def test_hashring_self_next_node(self):
        """
    判断是否有屏蔽到自己的下一个节点
        """

        memcache_servers = ['192.168.0.246:11212']

        ring = HashRing(memcache_servers, replicas=1)
        test_str = random_str()

        select_node = ring.get_node(test_str)
        next_nodes = ring.next_nodes(select_node)
        self.assertTrue(not next_nodes)
Ejemplo n.º 6
0
    def test_hashring_self_next_node(self):
        """
    判断是否有屏蔽到自己的下一个节点
        """

        memcache_servers = ['192.168.0.246:11212']

        ring = HashRing(memcache_servers, replicas=1)
        test_str = random_str()

        select_node = ring.get_node(test_str)
        next_nodes = ring.next_nodes(select_node)
        self.assertTrue(not next_nodes)
Ejemplo n.º 7
0
    def test_hashring_next_nodes(self):
        """
    判断某个节点删除以后,HashRing给某个相同字符串的定位节点是否和HashRing的next_nodes函数取得的节点一致
        """

        memcache_servers = ['192.168.0.246:11212',
                            '192.168.0.247:11212',
                            '192.168.0.248:11212',
                            '192.168.0.249:11212']

        text_cont = 30
        for i in xrange(text_cont):
            ring = HashRing(memcache_servers, replicas=1)

            test_str = random_str(i)

            select_node = ring.get_node(test_str)
            next_nodes = ring.next_nodes(select_node)
            ring.remove_node(select_node)
            reselect_node = ring.get_node(test_str)
            self.assertTrue(reselect_node in next_nodes)
Ejemplo n.º 8
0
    def test_hashring_next_nodes(self):
        """
    判断某个节点删除以后,HashRing给某个相同字符串的定位节点是否和HashRing的next_nodes函数取得的节点一致
        """

        memcache_servers = [
            '192.168.0.246:11212', '192.168.0.247:11212',
            '192.168.0.248:11212', '192.168.0.249:11212'
        ]

        text_cont = 30
        for i in xrange(text_cont):
            ring = HashRing(memcache_servers, replicas=1)

            test_str = random_str(i)

            select_node = ring.get_node(test_str)
            next_nodes = ring.next_nodes(select_node)
            ring.remove_node(select_node)
            reselect_node = ring.get_node(test_str)
            self.assertTrue(reselect_node in next_nodes)
Ejemplo n.º 9
0
 def test_cache_pop(self):
     test_set_key = random_str(3)
     self.assertTrue(test_cache_mgr.get(test_set_key) == test_data)
     test_cache_mgr.pop(test_set_key)
     self.assertTrue(not test_cache_mgr.has(test_set_key))
Ejemplo n.º 10
0
 def test_cache_has(self):
     test_set_key = random_str(3)
     self.assertTrue(not test_cache_mgr.has(test_set_key))
     test_cache_mgr.get(test_set_key)
     self.assertTrue(test_cache_mgr.has(test_set_key))
Ejemplo n.º 11
0
 def test_cache_get_ls(self):
     get_len = 10
     keys = [random_str(3) for _ in xrange(get_len)]
     caches = test_cache_mgr.get_ls(keys)
     self.assertTrue(len(caches) == get_len)
     [self.assertTrue(caches[idx] == test_data) for idx, _ in enumerate(keys)]
Ejemplo n.º 12
0
 def test_cache_get(self):
     test_set_key = random_str(3)
     self.assertTrue(test_cache_mgr.get(test_set_key) == test_data)
Ejemplo n.º 13
0
#!/usr/bin/python2.7
# coding=utf-8
"""
Created on 2015-8-28

@author: Jay
"""
from lib.common import *
from utils.data.cache import CacheLoader, CacheMgr
from utils.comm_func import random_str

test_data = {random_str(): random_str(), random_str(): random_str()}

class TestLoader(CacheLoader):
    @classmethod
    def load(cls, key):
        return test_data

    @classmethod
    def load_ls(cls, key_ls, **kwargs):
        return [test_data for _ in key_ls]

    @classmethod
    def check(cls, data):
        assert isinstance(data, dict)

test_cache_mgr = CacheMgr.instance(TestLoader)

class CacheTest(unittest.TestCase):

    # 初始化工作