예제 #1
0
def test_timeout(fake_redis):
    """See if the timeout is set correctly (regression test)"""
    sec = section("t", lock_acquire_timeout=0, lock_expire_timeout=-1)
    # pylint: disable=protected-access
    assert sec._redis_lock._acquire_seconds == 1
    # pylint: disable=protected-access
    assert sec._redis_lock._expire_seconds == 1

    sec = section("y")
    # pylint: disable=protected-access
    assert sec._redis_lock._acquire_seconds == 10
    # pylint: disable=protected-access
    assert sec._redis_lock._expire_seconds == 30
예제 #2
0
def test_value_exists(fake_redis):
    """Test if the exists method works."""
    sec = section("dummy")

    assert not sec["x"].exists()
    sec["x"] = 42
    assert sec["x"].exists()
예제 #3
0
def test_add(fake_redis):
    """See if adding on a key works"""
    sec = section("dummy")
    sec.clear()

    assert sec.get("x").val() is None
    sec.get("x").add(1)
    assert sec.get("x").val() == 1
    sec.get("x").add(1)
    assert sec.get("x").val() == 2
예제 #4
0
def test_val_default(fake_redis):
    """Test if the default param of val() works"""
    sec = section("dummy")
    sec.clear()

    assert sec["x"].val() is None
    assert sec["x"].val(default=1) is 1
    assert sec["x"].val(default=None) is None

    sec["x"] = 10
    assert sec["x"].val(default=1) is 10
    assert sec["x"].val(default=None) is 10
예제 #5
0
def test_iter_children(fake_redis):
    """See if getting all children (only leaf nodes!) work"""
    sec = section("dummy")
    sec.clear()

    sec.set("a.b.c", 2)
    sec.set("a.b.d", 3)

    children = [(prx.key(), prx.val()) for prx in sec.iter_children()]

    assert children == [('dummy.a.b.c', 2), ('dummy.a.b.d', 3)]
    assert fake_redis.get('v:.dummy.a.b.c') == b'2'
    assert fake_redis.get('v:.dummy.a.b.d') == b'3'
예제 #6
0
def test_nested_locking(fake_redis):
    """Check if locking is possible """
    sec = section("nested")
    sec.clear()

    sec['a.b.c.d'] = 10

    # Locking this will work (since locks are recursive here)
    sec['a.b'].acquire()
    sec['a.b.c'].acquire()

    checks = {"thread-died": False}

    def _lock_me_dead():
        """This should block, so thread_died_yet should be True
        since it's set after the join timeout.
        """
        with sec['a.b.c.d']:
            time.sleep(0.05)

        checks["thread-died"] = True

    thr = threading.Thread(target=_lock_me_dead)
    thr.start()
    thr.join(0.5)

    # If the lock would not block, the join would give the thread
    # enough time to set the flag.
    assert checks['thread-died'] is False

    # Release the top node.
    sec['a.b'].release()

    # Thread should still not be able to acquire the lock
    # since a.b.c was locked (which locked a.b in turn)
    assert checks['thread-died'] is False
    time.sleep(0.1)
    assert checks['thread-died'] is False

    sec['a.b.c'].release()

    # Now finally the thread should be able to acquire the lock.
    time.sleep(0.5)
    assert checks['thread-died'] is True
예제 #7
0
def test_section_proxy(fake_redis):
    """See if the section helper works"""
    with section("QualityConytrol") as sec:
        sec.clear()
        sec["x"] = 2
        assert sec["x"].val() == 2