Ejemplo n.º 1
0
def test_scripting():
    r = redis.StrictRedis()
    try:
        q = PriorityQueue(r, 'test_pqueue1')

        script = """
        local result = redis.call('zrange', KEYS[1], 0, 0)
        local msg
        if result then
            msg = result[1]
            redis.call('zrem', KEYS[1], msg)
        else
            msg = nil
        end
        return msg
        """
        zpop = r.register_script(script)
        item = zpop(keys=['test_pqueue1'], client=r)
        q.push("hi", 1)
        assert item is None
        item = zpop(keys=['test_pqueue1'], client=r)
        assert 'hi' == item
        item = zpop(keys=['test_pqueue1'], client=r)
        assert not item
    finally:
        r.delete('test_pqueue1')
Ejemplo n.º 2
0
def test_pqueue():
    r = redis.StrictRedis()
    try:
        q = PriorityQueue(r, 'test_pqueue2')

        items = [("one", 0), ("two", -1.0), ("three", 5.2)]
        for msg, score in items:
            q.push(msg, score)

        assert len(q) == len(items)

        expected = ["two", 'one', 'three']
        found = []
        while True:
            msg = q.pop()
            if msg is None:
                break
            else:
                found.append(msg)

        assert expected == found
    finally:
        r.delete('test_pqueue2')