新增select语句

This commit is contained in:
2024-10-13 01:56:28 +08:00
parent 8e94cd996f
commit 4975b19a24
4 changed files with 81 additions and 4 deletions

View File

@ -1,4 +1,5 @@
import time
from select import select
from magicoca.chan import Chan
from multiprocessing import Process, set_start_method
@ -21,6 +22,7 @@ def p2f(chan: Chan[int]):
if recv_ans != list(range(10)) + [-1]:
raise ValueError("Chan Shift Test Failed")
class TestChan:
def test_test(self):
@ -63,3 +65,6 @@ class TestChan:
p2.start()
p1.join()
p2.join()

42
tests/test_select.py Normal file
View File

@ -0,0 +1,42 @@
from multiprocessing import Process
from magicoca import Chan, select
def sp1(chan: Chan[int]):
for i in range(10):
chan << i
def sp2(chan: Chan[int]):
for i in range(10):
chan << i
def rp(chans: list[Chan[int]]):
rl = []
for t in select(*chans):
rl.append(t)
if len(rl) == 20:
break
print(rl)
assert len(rl) == 20
class TestSelect:
def test_select(self):
chan1 = Chan[int]()
chan2 = Chan[int]()
print("Test Chan Select")
p1 = Process(target=sp1, args=(chan1,))
p2 = Process(target=sp2, args=(chan2,))
p3 = Process(target=rp, args=([chan1, chan2],))
p3.start()
p1.start()
p2.start()
p1.join()
p2.join()
p3.join()