def test_sequential_ids(self): before = interpreters.list_all_channels() channels1 = interpreters.create_channel() channels2 = interpreters.create_channel() channels3 = interpreters.create_channel() after = interpreters.list_all_channels() self.assertEqual(len(set(after) - set(before)), len({channels1, channels2, channels3}))
def test_send_recv_nowait_main(self): r, s = interpreters.create_channel() orig = b'spam' s.send_nowait(orig) obj = r.recv_nowait() self.assertEqual(obj, orig) self.assertIsNot(obj, orig)
def test_list_all(self): self.assertEqual(interpreters.list_all_channels(), []) created = set() for _ in range(3): ch = interpreters.create_channel() created.add(ch) after = set(interpreters.list_all_channels()) self.assertEqual(after, created)
def test_send_recv_nowait_different_interpreters(self): r1, s1 = interpreters.create_channel() r2, s2 = interpreters.create_channel() orig1 = b'spam' s1.send_nowait(orig1) out = _run_output( interpreters.create(), dedent(f""" obj1 = r.recv_nowait() assert obj1 == b'spam', 'expected: obj1 == orig1' # When going to another interpreter we get a copy. assert id(obj1) != {id(orig1)}, 'expected: obj1 is not orig1' orig2 = b'eggs' print(id(orig2)) s.send_nowait(orig2) """), channels=dict(r=r1, s=s2), ) obj2 = r2.recv_nowait() self.assertEqual(obj2, b'eggs') self.assertNotEqual(id(obj2), int(out))
def test_recv_nowait_default(self): default = object() rch, sch = interpreters.create_channel() obj1 = rch.recv_nowait(default) sch.send_nowait(None) sch.send_nowait(1) sch.send_nowait(b'spam') sch.send_nowait(b'eggs') obj2 = rch.recv_nowait(default) obj3 = rch.recv_nowait(default) obj4 = rch.recv_nowait() obj5 = rch.recv_nowait(default) obj6 = rch.recv_nowait(default) self.assertIs(obj1, default) self.assertIs(obj2, None) self.assertEqual(obj3, 1) self.assertEqual(obj4, b'spam') self.assertEqual(obj5, b'eggs') self.assertIs(obj6, default)
def test_send_recv_different_threads(self): r, s = interpreters.create_channel() def f(): while True: try: obj = r.recv() break except interpreters.ChannelEmptyError: time.sleep(0.1) s.send(obj) t = threading.Thread(target=f) t.start() s.send(b'spam') t.join() obj = r.recv() self.assertEqual(obj, b'spam')
def test_send_recv_nowait_same_interpreter(self): interp = interpreters.create() out = _run_output( interp, dedent(""" from test.support import interpreters r, s = interpreters.create_channel() orig = b'spam' s.send(orig) obj = r.recv_nowait() assert obj is not orig assert obj == orig """)) r, s = interpreters.create_channel() def f(): while True: try: obj = r.recv_nowait() break except _interpreters.ChannelEmptyError: time.sleep(0.1) s.send(obj)
def test_recv_nowait_empty(self): ch, _ = interpreters.create_channel() with self.assertRaises(interpreters.ChannelEmptyError): ch.recv_nowait()
def test_send_recv_nowait_main_with_default(self): r, _ = interpreters.create_channel() obj = r.recv_nowait(None) self.assertIsNone(obj)
def test_equality(self): _, ch1 = interpreters.create_channel() _, ch2 = interpreters.create_channel() self.assertEqual(ch1, ch1) self.assertNotEqual(ch1, ch2)
def test_id_type(self): _, sch = interpreters.create_channel() self.assertIsInstance(sch.id, _interpreters.ChannelID)
def test_create(self): r, s = interpreters.create_channel() self.assertIsInstance(r, interpreters.RecvChannel) self.assertIsInstance(s, interpreters.SendChannel)