Ejemplo n.º 1
0
def test_put_get():
    """
    Channel is a FIFO queue. Write with put() or put_many(), then read with
    get().
    """
    chnl = Channel()
    chnl.put(1)
    chnl.put(2)
    assert chnl.get() == 1
    chnl.put(3)
    assert chnl.get() == 2
    assert chnl.get() == 3
    chnl.put_many([4, 5])
    assert chnl.get() == 4
    assert chnl.get() == 5
Ejemplo n.º 2
0
def test_end_states():
    """
    Once cancelled, a Channel stays that way. If ended, though,
    it can be cancelled to prevent any remaining data being read.
    """
    chnl = Channel()
    chnl.cancel()
    with pytest.raises(channel.Cancelled):
        chnl.put(1)
    with pytest.raises(channel.Cancelled):
        chnl.get()
    chnl.cancel()
    with pytest.raises(channel.Cancelled):
        chnl.get()
    chnl.end()
    with pytest.raises(channel.Cancelled):
        chnl.get()

    chnl = Channel()
    chnl.end()
    with pytest.raises(channel.Finished):
        chnl.get()
    with pytest.raises(channel.Finished):
        chnl.put(1)
    chnl.end()
    with pytest.raises(channel.Finished):
        chnl.get()
    chnl.cancel()
    with pytest.raises(channel.Cancelled):
        chnl.get()

    chnl = Channel()
    chnl.put_many([1, 2, 3])
    chnl.end()
    assert chnl.get() == 1
    chnl.cancel()
    with pytest.raises(channel.Cancelled):
        chnl.get()
Ejemplo n.º 3
0
def test_priority_callable():
    """
    Prioritised channel can have custom sort order.
    """
    chnl = Channel(priority=lambda value: -value)
    values = (3, 2, 1, 1, 2, 3, 7)
    chnl.put_many(values).end()
    assert tuple(chnl) == tuple(sorted(values, reverse=True))
    # And this still works with the gets mixed into the puts
    chnl = Channel(priority=lambda value: -value)
    chnl.put(2)
    chnl.put(1)
    assert chnl.get() == 2
    chnl.put(0)
    assert chnl.get() == 1
    chnl.put(3)
    assert chnl.get() == 3
    assert chnl.get() == 0
Ejemplo n.º 4
0
def test_priority():
    """
    FIFO behaviour can be changed to a priority queue.
    """
    chnl = Channel(priority=True)
    values = (3, 2, 1, 1, 2, 3, 7)
    chnl.put_many(values).end()
    assert tuple(chnl) == tuple(sorted(values))
    # And this still works with the gets mixed into the puts
    chnl = Channel(priority=True)
    chnl.put(2)
    chnl.put(1)
    assert chnl.get() == 1
    chnl.put(0)
    assert chnl.get() == 0
    chnl.put(3)
    assert chnl.get() == 2
    assert chnl.get() == 3