示例#1
0
def test_many_open_connections(real_redis):
    """
    Test the connection pooling and see if we do not fail on too many.
    This test acquires 1000 open connections and runs some gets on it,
    to assert that the connection is actually being used.

    NOTE: This test assumes a high enough (> 1000) maxclients in redis.conf
    """
    prox = Proxy(
        ['ImageCache'],
        lock_acquire_timeout=100,
        lock_expire_timeout=105,
    )

    # Just set it to some dummy value.
    prox.set("url", "https://...")

    n_threads = 200
    barrier = Barrier(n_threads)

    def _use_me():
        """Helper function that does some dummy work"""
        # Wait for all threads to start.
        # We want to make sure the connection getting happens
        # at the same time for all threads.
        barrier.wait()

        for _ in range(100):
            assert prox["url"].exists()
            assert prox.val() == {"url": "https://..."}
            assert prox["url"].val() == "https://..."

    with background_thread(_use_me, (), jobs=n_threads-1):
        # Just use the main thread as one extra worker (thus -1)
        _use_me()
示例#2
0
def test_sequential_lock(fake_redis):
    """
    Simply test if incrementing a value locking in a single thread
    works (other tests always test concurrent access)
    """
    locked_val = Proxy('LockMe').set("x", 0)
    for _ in range(1000):
        with locked_val:
            locked_val.set("x", locked_val.get("x").val() + 1)
示例#3
0
def test_basic_locking(fake_redis):
    """Check if single process locking works"""
    root_prox = Proxy(path=('QualityControl', ))
    root_prox.clear()

    root_prox.acquire()
    assert root_prox.is_locked()
    root_prox.set('child', {})
    root_prox.release()
示例#4
0
 def _many_increments():
     """Make a lot of individually locked increments.
     In practice you should of course do the lock around the for loop
     to reduce lock contention, but here we want to trigger races.
     """
     # Note: Every process needs it's own lock.
     # Obvious, but easy to forget in unittests.
     proxy = Proxy(['QC'])
     for _ in range(n_increments):
         with proxy:
             proxy.set("x", proxy.get("x").val() + 1)
示例#5
0
def test_alternative_db(real_redis):
    """Test if we can set different values for the same key in different
    redis databases. The actual redis db depends on the config.
    """
    Pool().reload(cfg={
        "names": {
            "snmp": 1,
            "img": 2,
        },
    })

    default_prox = Proxy("cache")
    default_prox.set("x", 0)

    snmp_prox = Proxy("cache", db_name="snmp")
    snmp_prox.set("x", 1)

    img_prox = Proxy("cache", db_name="img")
    img_prox.set("x", 2)

    assert default_prox.get("x").val() == 0
    assert snmp_prox.get("x").val() == 1
    assert img_prox.get("x").val() == 2

    not_prox = Proxy("cache", db_name="not_there")
    not_prox.set("x", 3)

    # Not existing db_names will land it db 0, same as default:
    assert default_prox.get("x").val() == 3
    assert not_prox.get("x").val() == 3
示例#6
0
def test_basic_set(fake_redis):
    """See if the very basic testcases work"""
    root_prox = Proxy(path=('TestQualityControl', ))
    root_prox.clear()

    root_prox.set('a', 2)
    assert root_prox.get('a').val() == 2

    root_prox.set('a.b', 3)
    assert root_prox.get('a.b').val() == 3
    assert root_prox.get('a').val() == {'b': 3}

    root_prox.set('a.c', None)
    assert root_prox.get('a.c').val() is None
    assert root_prox.get('a').val() == {'b': 3, 'c': None}

    root_prox.set('a', {'b': 42, 'e': 3})
    assert root_prox.get('a').val() == {'b': 42, 'e': 3}