def test_recv_with_no_items_blocks(self): chan = gochans.BufferedChannel(1) markers = [] def recvall(): markers.append(chan.recv()) markers.append(chan.recv()) be.run(recvall) self.assertEqual(markers, []) chan.send(1) self.assertEqual(markers, [1]) chan.send(2) self.assertEqual(markers, [1, 2])
def test_recv_and_send_with_full_buffer_block(self): chan = gochans.BufferedChannel(2) markers = [] def sendall(): markers.append(chan.send(4)) markers.append(chan.send(3)) markers.append(chan.send(2)) markers.append(chan.send(1)) sender = be.run(sendall) self.assertEqual(len(markers), 2) got = [chan.recv(), chan.recv()] be.resume(sender) self.assertEqual(len(markers), 4) self.assertEqual(got, [4, 3]) got.extend([chan.recv(), chan.recv()]) self.assertEqual(got, [4, 3, 2, 1])
def test_recv_and_send_with_room_do_not_block(self): resultschan = gochans.BufferedChannel(5) endchan = gochans.SyncChannel() def square(x): return x * x def func(): for num in range(5): resultschan.send(square(num)) endchan.send() goless.go(func) # Waiting on the endchan tells us our results are # queued up in resultschan endchan.recv() got = [resultschan.recv() for _ in range(5)] ideal = [square(i) for i in range(5)] self.assertEqual(got, ideal)
def makechan(self): return gochans.BufferedChannel(2)