Skip to content

Commit 8e6b033

Browse files
committed
test: updated read tests
1 parent 32e0cc3 commit 8e6b033

1 file changed

Lines changed: 50 additions & 45 deletions

File tree

tests/test_read.py

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,50 @@
1-
import subprocess as sp
2-
from namedpipe import NPopen
3-
4-
5-
def run_ffmpeg(pipe):
6-
sz = [320, 240]
7-
return (
8-
sp.Popen(
9-
# fmt:off
10-
[
11-
"ffmpeg",
12-
"-y",
13-
"-f", "lavfi",
14-
"-i", f"testsrc=s={sz[0]}x{sz[1]}:d=5",
15-
"-f", "rawvideo",
16-
"-pix_fmt", "rgb24",
17-
f'{pipe}',
18-
]
19-
# fmt:on
20-
),
21-
sz[0] * sz[1] * 3,
22-
)
23-
24-
25-
def test_read_all():
26-
with NPopen("r") as pipe:
27-
assert pipe.readable()
28-
assert not pipe.writable()
29-
proc, nbytes = run_ffmpeg(pipe)
30-
f = pipe.wait()
31-
while f.read(nbytes):
32-
pass
33-
proc.wait()
34-
35-
36-
def test_read_some():
37-
with NPopen("r") as pipe:
38-
proc, nbytes = run_ffmpeg(pipe)
39-
f = pipe.wait()
40-
for i in range(30):
41-
f.read(nbytes)
42-
proc.kill()
43-
44-
if __name__=='__main__':
45-
test_read_all()
1+
import multiprocessing as mp
2+
3+
import namedpipe as npipe
4+
5+
6+
def worker_npopen_rt(pipe_path, msg):
7+
8+
with open(pipe_path, "wt") as f:
9+
f.write(msg)
10+
11+
12+
def test_multiprocessing_read_default():
13+
14+
# Use 'spawn' context to remain safe across OS platforms (Windows/macOS/Linux)
15+
ctx = mp.get_context("spawn")
16+
17+
# Create a named pipe Initialize the process
18+
msg = "hello reader!!"
19+
with npipe.NPopen("rt") as pipe:
20+
p = ctx.Process(target=worker_npopen_rt, args=(pipe.path, msg))
21+
p.start()
22+
stream = pipe.wait()
23+
rmsg = stream.read(len(msg) * 2)
24+
p.join()
25+
26+
assert rmsg == msg
27+
28+
29+
# def test_read_all():
30+
# with NPopen("r") as pipe:
31+
# assert pipe.readable()
32+
# assert not pipe.writable()
33+
# proc, nbytes = run_ffmpeg(pipe)
34+
# f = pipe.wait()
35+
# while f.read(nbytes):
36+
# pass
37+
# proc.wait()
38+
39+
40+
# def test_read_some():
41+
# with NPopen("r") as pipe:
42+
# proc, nbytes = run_ffmpeg(pipe)
43+
# f = pipe.wait()
44+
# for i in range(30):
45+
# f.read(nbytes)
46+
# proc.kill()
47+
48+
49+
# if __name__ == "__main__":
50+
# test_read_all()

0 commit comments

Comments
 (0)