async def main(): from_ch = chan() a_ch, b_ch = chan(), chan() p = c.pub(from_ch, lambda x: x[0], lambda x: None if x == 'a' else 2) p.sub('a', a_ch) p.sub('b', b_ch) await from_ch.put('a1') await from_ch.put('a2') await asyncio.sleep(0.1) self.assertIs(from_ch.offer('a fail'), False) self.assertEqual(await a_ch.get(), 'a1') self.assertEqual(await a_ch.get(), 'a2') await asyncio.sleep(0.1) self.assertIsNone(a_ch.poll()) await from_ch.put('b1') await from_ch.put('b2') await from_ch.put('b3') await from_ch.put('b4') await asyncio.sleep(0.1) self.assertIs(from_ch.offer('b fail'), False) self.assertEqual(await b_ch.get(), 'b1') self.assertEqual(await b_ch.get(), 'b2') self.assertEqual(await b_ch.get(), 'b3') self.assertEqual(await b_ch.get(), 'b4') await asyncio.sleep(0.1) self.assertIsNone(b_ch.poll())
async def main(): from_ch, to_ch = chan(), chan(1) m = c.mix(to_ch) m.admix(from_ch) from_ch.close() await asyncio.sleep(0.1) self.assertIs(await to_ch.put('success'), True)
async def main(): src, dest = chan(), chan(1) c.pipe(src, dest, close=False) src.close() await asyncio.sleep(0.1) dest.put('success') self.assertEqual(await dest.get(), 'success')
async def main(): src, dest = chan(), chan() c.pipe(src, dest) src.f_put(1) src.f_put(2) src.close() self.assertEqual(await a_list(dest), [1, 2])
async def main(): to_ch = chan(1) solo_ch1, solo_ch2, non_solo_ch = chan(1), chan(1), chan(1) m = c.mix(to_ch) m.solo_mode('pause') m.toggle({ solo_ch1: { 'solo': True }, solo_ch2: { 'solo': True }, non_solo_ch: {} }) await solo_ch1.put('solo_ch1 not paused') self.assertEqual(await to_ch.get(), 'solo_ch1 not paused') await solo_ch2.put('solo_ch2 not paused') self.assertEqual(await to_ch.get(), 'solo_ch2 not paused') await non_solo_ch.put('stay in non_solo_ch') await asyncio.sleep(0.1) self.assertEqual(await non_solo_ch.get(), 'stay in non_solo_ch') m.toggle({solo_ch1: {'solo': False}, solo_ch2: {'solo': False}}) await asyncio.sleep(0.1) await solo_ch1.put('solo_ch1 still not paused') self.assertEqual(await to_ch.get(), 'solo_ch1 still not paused') await solo_ch2.put('solo_ch2 still not paused') self.assertEqual(await to_ch.get(), 'solo_ch2 still not paused') await non_solo_ch.put('non_solo_ch not paused') self.assertEqual(await to_ch.get(), 'non_solo_ch not paused')
async def main(): solo_ch1, solo_ch2, non_solo_ch = chan(), chan(), chan() to_ch = chan(1) m = c.mix(to_ch) m.solo_mode('mute') m.toggle({ solo_ch1: { 'solo': True }, solo_ch2: { 'solo': True }, non_solo_ch: {} }) await solo_ch1.put('solo_ch1 not muted') self.assertEqual(await to_ch.get(), 'solo_ch1 not muted') await solo_ch2.put('solo_ch2 not muted') self.assertEqual(await to_ch.get(), 'solo_ch2 not muted') await non_solo_ch.put('drop me') await asyncio.sleep(0.1) self.assertIsNone(to_ch.poll()) m.toggle({solo_ch1: {'solo': False}, solo_ch2: {'solo': False}}) await asyncio.sleep(0.1) await solo_ch1.put('solo_ch1 still not muted') self.assertEqual(await to_ch.get(), 'solo_ch1 still not muted') await solo_ch2.put('solo_ch2 still not muted') self.assertEqual(await to_ch.get(), 'solo_ch2 still not muted') await non_solo_ch.put('non_solo_ch not muted') self.assertEqual(await to_ch.get(), 'non_solo_ch not muted')
async def main(): src_ch, tap_ch = chan(), chan() m = c.mult(src_ch) src_ch.close() await asyncio.sleep(0.1) m.tap(tap_ch) self.assertIsNone(await tap_ch.get())
async def main(): src, dest = chan(), chan() m = c.mult(src) m.tap(dest) await src.put('success') self.assertEqual(await dest.get(), 'success') src.close()
async def main(): to_ch, from_ch = chan(), chan(1) admix_ch, unmix_ch, pause_ch = chan(1), chan(1), chan(1) m = c.mix(to_ch) m.toggle({from_ch: {}, unmix_ch: {}}) # Start waiting put to to_ch await from_ch.put('successful transfer') await asyncio.sleep(0.1) # Apply operations while mix is waiting on to_ch m.admix(admix_ch) m.unmix(unmix_ch) m.toggle({pause_ch: {'pause': True}}) # Confirm state is correct self.assertEqual(await to_ch.get(), 'successful transfer') await admix_ch.put('admix_ch added') self.assertEqual(await to_ch.get(), 'admix_ch added') await unmix_ch.put('unmix_ch removed') await asyncio.sleep(0.1) self.assertEqual(await unmix_ch.get(), 'unmix_ch removed') await pause_ch.put('pause_ch paused') await asyncio.sleep(0.1) self.assertEqual(await pause_ch.get(), 'pause_ch paused')
async def main(): from_ch, to_ch = chan(1), chan(1) m = c.mix(to_ch) m.toggle({from_ch: {'pause': True, 'mute': True}}) await from_ch.put('stay in from_ch') await asyncio.sleep(0.1) self.assertEqual(await from_ch.get(), 'stay in from_ch')
async def main(): unpaused_ch, paused_ch, to_ch = chan(1), chan(1), chan(1) m = c.mix(to_ch) m.toggle({ unpaused_ch: { 'pause': False }, paused_ch: { 'pause': True } }) await unpaused_ch.put('not paused') self.assertEqual(await to_ch.get(), 'not paused') await paused_ch.put('remain in paused_ch') await asyncio.sleep(0.1) self.assertEqual(await paused_ch.get(), 'remain in paused_ch') m.toggle({ unpaused_ch: { 'pause': True }, paused_ch: { 'pause': False } }) await paused_ch.put('no longer paused') self.assertEqual(await to_ch.get(), 'no longer paused') await unpaused_ch.put('paused now') await asyncio.sleep(0.1) self.assertEqual(await unpaused_ch.get(), 'paused now')
async def main(): from_ch, to_ch = chan(1), chan() p = c.pub(from_ch, xf.identity) p.sub('a', to_ch) p.unsub('b', chan()) await from_ch.put('a') self.assertEqual(await to_ch.get(), 'a')
async def main(): src, dest1, dest2 = chan(), chan(), chan() c.thread(lambda: thread(src, dest1, dest2)) await asyncio.sleep(0.1) self.assertIs(await src.put('dropMe'), True) await asyncio.sleep(0.1) self.assertIsNone(dest1.poll()) self.assertIsNone(dest2.poll())
async def main(): src1, src2 = chan(), chan() m = c.merge([src1, src2], 2) await src1.put('src1') await src2.put('src2') src1.close() src2.close() self.assertEqual([x async for x in m], ['src1', 'src2'])
async def main(): src, close_dest, no_close_dest = chan(), chan(1), chan(1) m = c.mult(src) m.tap(close_dest) m.tap(no_close_dest, close=False) src.close() await asyncio.sleep(0.1) self.assertIs(await close_dest.put('closed'), False) self.assertIs(await no_close_dest.put('not closed'), True)
async def main(): success_ch, fail_ch = chan(), chan() alt_future = c.alt(fail_ch, success_ch) self.assertIs(await success_ch.put('success'), True) # cancel() will end up calling set_result() since # set_result_threadsafe() callback won't have been called yet self.assertIs(alt_future.cancel(), False) self.assertEqual(alt_future.result(), ('success', success_ch))
async def main(): from_ch, close_ch, open_ch = chan(1), chan(1), chan(1) p = c.pub(from_ch, xf.identity) p.sub('close', close_ch) p.sub('open', open_ch, close=False) from_ch.close() await asyncio.sleep(0.1) self.assertIs(await close_ch.put('fail'), False) self.assertIs(await open_ch.put('success'), True)
async def main(): from_ch, to_ch = chan(1), chan() m = c.mix(to_ch) m.admix(from_ch) await asyncio.sleep(0.1) to_ch.close() await from_ch.put('mix consumes me') await from_ch.put('mix ignores me') await asyncio.sleep(0.1) self.assertEqual(await from_ch.get(), 'mix ignores me')
async def main(): from_ch1, from_ch2, to_ch = chan(), chan(), chan(1) m = c.mix(to_ch) m.admix(from_ch1) await from_ch1.put('from_ch1') self.assertEqual(await to_ch.get(), 'from_ch1') m.admix(from_ch2) await from_ch1.put('from_ch1 again') self.assertEqual(await to_ch.get(), 'from_ch1 again') await from_ch2.put('from_ch2') self.assertEqual(await to_ch.get(), 'from_ch2')
async def main(): src, dest1, dest2 = chan(), chan(), chan() m = c.mult(src) m.tap(dest1) m.tap(dest2) await src.put('item') await dest1.get() await asyncio.sleep(0.1) self.assertIs(src.offer('failure'), False) await dest2.get() src.close()
async def main(): src, dest = chan(3), chan(1) c.onto_chan(src, ['intoDest1', 'intoDest2', 'dropMe'], close=False) c.pipe(src, dest) await asyncio.sleep(0.1) dest.close() self.assertEqual(await dest.get(), 'intoDest1') self.assertEqual(await dest.get(), 'intoDest2') self.assertIsNone(await dest.get()) await asyncio.sleep(0.1) self.assertIsNone(src.poll())
def test_alt_put_after_wait(self): get_ch, put_ch = chan(), chan() async def putter(): await asyncio.sleep(0.1) await put_ch.get() async def main(): c.go(putter()) return await c.alt([put_ch, 'success'], get_ch, priority=True) self.assertEqual(asyncio.run(main()), (True, put_ch))
def test_alt_get_no_wait(self): get_ch, put_ch = chan(), chan() async def putter(): await get_ch.put('success') async def main(): c.go(putter()) await asyncio.sleep(0.1) return await c.alt([put_ch, 'noSend'], get_ch, priority=True) self.assertEqual(asyncio.run(main()), ('success', get_ch))
async def main(): src, dest1, dest2 = chan(), chan(), chan() m = c.mult(src) m.tap(dest1) m.tap(dest2) await src.put('item') await dest1.get() await dest2.get() m.untap_all() self.assertIs(await src.put('dropMe'), True) await asyncio.sleep(0.1) self.assertIsNone(dest1.poll()) self.assertIsNone(dest2.poll())
async def main(): src, dest1, dest2 = chan(), chan(), chan() m = c.mult(src) m.tap(dest1) m.tap(dest2) await src.put('item1') await dest1.get() await dest2.get() m.untap(dest2) await src.put('item2') await dest1.get() await asyncio.sleep(0.1) self.assertIsNone(dest2.poll()) src.close()
async def main(): from_ch1, from_ch2, to_ch = chan(1), chan(1), chan(1) m = c.mix(to_ch) m.admix(from_ch1) await from_ch1.put('from_ch1') self.assertEqual(await to_ch.get(), 'from_ch1') m.admix(from_ch2) m.unmix(from_ch1) await from_ch2.put('from_ch2') self.assertEqual(await to_ch.get(), 'from_ch2') await from_ch1.put('remain in from_ch1') await asyncio.sleep(0.1) self.assertIsNone(to_ch.poll()) self.assertEqual(await from_ch1.get(), 'remain in from_ch1')
async def main(): unmuted_ch, muted_ch = chan(), chan() to_ch = chan(1) m = c.mix(to_ch) m.toggle({unmuted_ch: {'mute': False}, muted_ch: {'mute': True}}) await unmuted_ch.put('not muted') self.assertEqual(await to_ch.get(), 'not muted') await muted_ch.put('mute me') self.assertIsNone(to_ch.poll()) m.toggle({unmuted_ch: {'mute': True}, muted_ch: {'mute': False}}) await muted_ch.put('the mute can now talk') self.assertEqual(await to_ch.get(), 'the mute can now talk') await unmuted_ch.put('i made a deal with Ursula') self.assertIsNone(to_ch.poll())
async def main(): in_ch = chan() in_ch.close() result_ch = c.reduce( xf.multi_arity(lambda: 100, xf.identity, lambda x, y: x + y), in_ch) self.assertEqual(await result_ch.get(), 100)
async def main(): to_ch = chan(5, xf.take(5)) from_ch = c.to_chan(range(20)) c.pipeline(5, to_ch, xf.identity, from_ch, mode=mode) await asyncio.sleep(0.1) self.assertEqual(await a_list(to_ch), [0, 1, 2, 3, 4]) self.assertTrue(len(await a_list(from_ch)) > 5)
async def main(): ch = chan() getter_thread = threading.Thread(target=getter, args=[ch]) getter_thread.start() self.assertIs(await ch.put('success'), True) getter_thread.join() self.assertEqual(result, 'success')