async def test_connection_receive_twice(self): pc = Connection(*pipe()) m = Message({}, []) async with pc: await pc.write(m) self.assertIs(m, await pc.read()) with self.assertRaises(TimeoutError): await wait_for(pc.read(), 1)
async def test_run_after_release(self): task: Optional[Task] = None pipe_r, pipe_w = pipe() reader = ReaderTask(pipe_r, coroutine(lambda m: None)) async with reader: task = reader._task self.assertIsNotNone(task) self.assertFalse(task.done()) self.assertTrue(task.done())
async def _acquire(self) -> NoReturn: if self.name in self.multiplexer.streams: raise RuntimeError("Stream already registered.") self.multiplexer.streams[self.name] = self self.ingress: Connection = Connection(*pipe()) await self.ingress.acquire() register(self, self.ingress) self.egress: ChannelOutputStream = ChannelOutputStream(self) await self.egress.acquire() register(self, self.egress)
async def test_connection_read_with_wait(self): pc = Connection(*pipe()) m = Message({}, []) async def _concurrent(): t0 = time.time() self.assertIs(m, await pc.read()) t1 = time.time() self.assertGreaterEqual(t1-t0, 1) async with pc: task = get_running_loop().create_task(_concurrent()) await sleep(1) await pc.write(m) await task
async def test_connection_close_with_wait(self): pc = Connection(*pipe()) m = Message({}, []) async def _concurrent(): t0 = time.time() with self.assertRaises(TimeoutError): await pc.read() t1 = time.time() self.assertGreaterEqual(t1-t0, 1) async with pc: task = get_running_loop().create_task(_concurrent()) await sleep(1.2) await pc.close() await task
async def test_receiving_messages(self): m1 = Message({}, []) m2 = Message({}, []) messages = [] finished = Event() async def _r(m: Message): messages.append(m) if len(m) == 2: finished.set() pipe_r, pipe_w = pipe() reader = ReaderTask(pipe_r, _r) async with reader, pipe_w: await pipe_w.write(m1) await pipe_w.write(m2) await wait_for(finished.wait(), 5) self.assertIs(m1, messages[0]) self.assertIs(m2, messages[1])
async def test_connection_wait(self): pc = Connection(*pipe()) async with pc: await pc.close() self.assertIsNone(await pc.read())
async def test_connection_receive(self): pc = Connection(*pipe()) m = Message({}, []) async with pc: await pc.write(m) self.assertIs(m, await pc.read())