Ejemplo n.º 1
0
def test_class_method():
    import random

    memoizer = memoize(manual_flush=True, instance_method=True)

    class foo(object):
        @memoizer
        def bar(self, *args):
            return random.random()

    x = foo()
    x2 = foo()

    assert x.bar('a', 'b') != x2.bar('a', 'b')
    assert x.bar('a', 'b') == x.bar('a', 'b')
    assert x.bar('a', 'b') != x.bar('a', 'd')
    assert x2.bar('a', 'b') == x2.bar('a', 'b')

    # the memoizer should have made private caches for each instance
    assert len(memoizer.cache) == 0

    # now make sure that they don't share caches
    res1 = x.bar('a', 'b')
    res2 = x2.bar('a', 'b')
    x.bar.flush_cache()
    assert res1 != x.bar('a', 'b')
    assert res2 == x2.bar('a', 'b')
Ejemplo n.º 2
0
def test_class_method():
    import random

    memoizer = memoize(manual_flush=True, instance_method=True)

    class foo(object):
        @memoizer
        def bar(self, *args):
            return random.random()

    x = foo()
    x2 = foo()

    assert x.bar('a', 'b') != x2.bar('a', 'b')
    assert x.bar('a', 'b') == x.bar('a', 'b')
    assert x.bar('a', 'b') != x.bar('a', 'd')
    assert x2.bar('a', 'b') == x2.bar('a', 'b')

    # the memoizer should have made private caches for each instance
    assert len(memoizer.cache) == 0

    # now make sure that they don't share caches
    res1 = x.bar('a', 'b')
    res2 = x2.bar('a', 'b')
    x.bar.flush_cache()
    assert res1 != x.bar('a', 'b')
    assert res2 == x2.bar('a', 'b')
Ejemplo n.º 3
0
def test_auto_flush():
    memoize_inst = memoize(timeout=.1)

    @memoize_inst
    def fn(*args, **kwargs):
        pass

    fn(1, 2, 3)
    assert len(memoize_inst.cache.keys()) == 1
    time.sleep(.2)
    fn(1, 2, 3)
    assert len(memoize_inst.cache.keys()) == 1
Ejemplo n.º 4
0
def test_auto_flush():
    memoize_inst = memoize(timeout=.1)

    @memoize_inst
    def fn(*args, **kwargs):
        pass

    fn(1, 2, 3)
    assert len(memoize_inst.cache.keys()) == 1
    time.sleep(.2)
    fn(1, 2, 3)
    assert len(memoize_inst.cache.keys()) == 1
Ejemplo n.º 5
0
def test_manual_flush():
    memoize_inst = memoize(timeout=.1, manual_flush=True)

    @memoize_inst
    def fn(*args, **kwargs):
        pass

    fn(1, 2, 3)
    assert len(memoize_inst.cache.keys()) == 1
    time.sleep(.2)
    fn(3, 4, 5)
    assert len(memoize_inst.cache.keys()) == 2
    time.sleep(.2)
    fn.flush_cache()
    assert len(memoize_inst.cache.keys()) == 0
Ejemplo n.º 6
0
def test_manual_flush():
    memoize_inst = memoize(timeout=.1, manual_flush=True)

    @memoize_inst
    def fn(*args, **kwargs):
        pass

    fn(1, 2, 3)
    assert len(memoize_inst.cache.keys()) == 1
    time.sleep(.2)
    fn(3, 4, 5)
    assert len(memoize_inst.cache.keys()) == 2
    time.sleep(.2)
    fn.flush_cache()
    assert len(memoize_inst.cache.keys()) == 0
    def __init__(self, host, port, user='******', password='', database='information_schema'):
        """ Initialize the RandomAggregatorPool with connection
        information for an aggregator in a MemSQL Distributed System.

        All aggregator connections will share the same user/password/database.
        """
        self.logger = logging.getLogger('memsql.random_aggregator_pool')
        self._pool = ConnectionPool()
        self._refresh_aggregator_list = memoize(30)(self._update_aggregator_list)
        self._lock = threading.RLock()

        self._primary_aggregator = (host, port)
        self._user = user
        self._password = password
        self._database = database
        self._aggregators = []
        self._aggregator = None
        self._master_aggregator = None
Ejemplo n.º 8
0
def test_fail_instance_method():
    """ Test that memoize without instance_method creates a globally
        shared memoize instance (shared by all instances of the class)
    """
    memoizer = memoize(manual_flush=True)

    class foo(object):
        def __init__(self, x):
            self._x = x

        @memoizer
        def bar(self):
            return self._x

    x = foo(1)
    x2 = foo(2)

    assert x.bar() != x2.bar()

    # note that they share the same cache
    assert len(memoizer.cache) == 2
Ejemplo n.º 9
0
def test_fail_instance_method():
    """ Test that memoize without instance_method creates a globally
        shared memoize instance (shared by all instances of the class)
    """
    memoizer = memoize(manual_flush=True)

    class foo(object):
        def __init__(self, x):
            self._x = x

        @memoizer
        def bar(self):
            return self._x

    x = foo(1)
    x2 = foo(2)

    assert x.bar() != x2.bar()

    # note that they share the same cache
    assert len(memoizer.cache) == 2