def test_behavior(self): # Obviously we cannot test an infinite buffer, # but we can just test a huge one's behavior. chan = gochans.AsyncChannel() for _ in range(10000): chan.send() chan.close() for _ in chan: pass
def test_range_with_closed_channel(self): chan = self.makechan() sendCount = min(chan.maxsize, 5) data2send = list(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_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 prime(): count = 1 if platform.python_implementation() == 'PyPy': count = 10 global WRITE_ENABLED WRITE_ENABLED = False for _ in range(count): bench_channels() bench_selects() WRITE_ENABLED = True
def bench_channel(chan_size): c = chan(chan_size) def func(): for _ in range(QUEUE_LEN): c.send(0) c.close() count = 0 go(func) start = time.clock() for _ in range(QUEUE_LEN): c.recv() count += 1 end = time.clock() return end - start
def bench_select(use_default): c = chan(0) cases = [ selecting.scase(c, 1), selecting.rcase(c), selecting.scase(c, 1), selecting.rcase(c), ] if use_default: cases.append(selecting.dcase()) def sender(): while True: c.send(0) c.recv() go(sender) start = time.clock() for _ in range(QUEUE_LEN): selecting.select(cases) end = time.clock() return end - start
def func(): for num in range(5): resultschan.send(square(num)) endchan.send()
def func(): for _ in range(QUEUE_LEN): c.send(0) c.close()