def test_iterate(): """ You can read a Channel to the end by iterating it. """ chnl = Channel() chnl.put_many(range(5)) chnl.end() assert list(chnl) == [0, 1, 2, 3, 4]
def test_thread_source_channel(): """ Everything thread_source does, it can do when the output channel is specified by the user insted of created by the call to thread_source() """ my_chnl = Channel() my_chnl.put_many(range(5)) # This is the basic behaviour. chnl = channel.thread_source(range(10), channel=my_chnl) assert chnl is my_chnl assert list(chnl) == list(range(5)) + list(range(10))
def test_suppressed(): """ The Channel exceptions both have a special suppress helper. """ chnl = Channel() chnl.end() with channel.Finished.suppress(): chnl.get() chnl = Channel() chnl.cancel() with channel.Cancelled.suppress(): chnl.get()
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
def test_cancelled(): """ Once you cancel a Channel, get() raises Cancelled. """ chnl = Channel() chnl.cancel() with pytest.raises(channel.Cancelled): chnl.get() with pytest.raises(channel.Cancelled): chnl.get()
def test_suppressed_decorator(): """ Channel exception suppression also works as a decorator. """ @channel.Finished.suppress() def get(): chnl.get() chnl = Channel() chnl.end() get() chnl = Channel() chnl.cancel() with pytest.raises(channel.Cancelled): get() channel.Cancelled.suppress()(get)()
def test_channel_param(tmp_path): """ recurse_files() and recurse_filestats() accept a channel parameter. """ with tempfiles(tmp_path): assert set(file.recurse_files(tmp_path)) == set(file.recurse_files(tmp_path, channel=QueueChannel())) assert set(file.recurse_filestats(tmp_path)) == set(file.recurse_filestats(tmp_path, channel=QueueChannel()))
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
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
def test_finished(): """ Once you end a channel, get() raises Finished. """ chnl = Channel() chnl.end() with pytest.raises(channel.Finished): chnl.get() with pytest.raises(channel.Finished): chnl.get() chnl = Channel() chnl.put_many([1, 2, 3]).end() assert chnl.get() == 1 assert chnl.get() == 2 assert chnl.get() == 3 with pytest.raises(channel.Finished): chnl.get() with pytest.raises(channel.Finished): chnl.get()
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()