def test_recv_on_closed_chan_raises_after_chan_empties(self): chan = self.makechan() be.run(chan.send, 'hi') self.assertEqual(chan.recv(), 'hi') chan.close() self.assertRaises(gochans.ChannelClosed, chan.recv)
def test_executes(self): def recv(): a.append(self.ch.recv()) a = [] be.run(recv) self.ca.exec_() self.assertEqual(a, [self.sendval])
def test_range_with_closed_channel(self): chan = self.makechan() sendCount = min(chan.maxsize, 5) data2send = range(sendCount) for data in data2send: be.run(chan.send, data) chan.close() items = [o for o in chan] self.assertEqual(items, data2send)
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_select_no_default_no_ready_blocks(self): chan1 = goless.chan() chan2 = goless.chan() a = [] cases = [goless.rcase(chan2), goless.rcase(chan1)] def sel(): a.append(goless.select(cases)) be.run(sel) self.assertEqual(a, []) chan1.send(5) be.yield_() self.assertEqual(len(a), 1) chosen, val = a[0] self.assertEqual(chosen, cases[1]) self.assertEqual(val, 5)
def _test_channel_raises_when_closed(self, chan_method_name): chan = self.makechan() method = getattr(chan, chan_method_name) marker = [] def catch_raise(): try: method() except gochans.ChannelClosed: marker.append(1) marker.append(2) be.run(catch_raise) chan.close() be.yield_() self.assertEqual(marker, [1, 2])
def test_be_has_correct_sender_receiver_priority(self): c = be.channel() r = [] def do_send(): r.append("s1") c.send(None) r.append("s2") def do_receive(): r.append("r1") c.receive() r.append("r2") be.run(do_receive) be.run(do_send) be.yield_() self.assertEqual(["r1", "s1", "r2", "s2"], r)
def test_ready(self): def assert_default_readiness(): self.assertEquals(self.ca.ready(), self.chansize > 0) assert_default_readiness() be.run(self.ch.send) self.assertFalse(self.ca.ready()) be.run(self.ch.recv) assert_default_readiness() be.run(self.ch.send) self.assertFalse(self.ca.ready()) be.run(self.ch.recv) assert_default_readiness()
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_exec_no_onselected(self): be.run(self.ch.recv) self.ca.exec_()
def test_exec_with_no_body(self): be.run(self.ch.send, 'a') ca = goless.rcase(self.ch) self.assertEqual(ca.exec_(), 'a')
def test_executes(self): be.run(self.ch.send, 'a') x = self.ca.exec_() self.assertEqual(x, 'a')
def test_ready(self): self.assertFalse(self.ca.ready()) be.run(self.ch.send, 1) self.assertTrue(self.ca.ready()) be.run(self.ch.recv) self.assertFalse(self.ca.ready())