async def test_select_closed(self): """Select on a closed channel should return None""" sel = Selector() chan = Channel(2) await chan.close() sel.pop(chan) val = await sel.gather() self.assertEqual(val, (0, None))
async def test_select_both_push(self): """Push and popping should perform the first available op""" sel1, sel2 = Selector(), Selector() async with Channel(1) as chan1, Channel(1) as chan2: # c1 will be empty (cannot pop) sel1.pop(chan1) sel1.push(chan2, 10) val = await sel1.gather() self.assertEqual(val, (1, True)) # c2 will be empty (cannot pop) self.assertEqual(await chan2.pop(), 10) sel2.push(chan1, 20) sel2.pop(chan2) val = await sel2.gather() self.assertEqual(val, (0, True))
async def test_select_pop(self): """Popping on two channels should return the one with data""" sel1, sel2 = Selector(), Selector() async with Channel(1) as chan1, Channel(1) as chan2: # Pop from C2 first sel1.pop(chan1) sel1.pop(chan2) await chan2.push(10) val = await sel1.gather() self.assertEqual(val, (1, 10)) # Now pop from C1 sel2.pop(chan1) sel2.pop(chan2) await chan1.push(20) val = await sel2.gather() self.assertEqual(val, (0, 20))