55
66import contextlib
77import errno
8- from datetime import datetime
9- from io import BytesIO , StringIO
108import os
119import pathlib
1210import pickle
1311import stat
1412import sys
1513import threading
1614import time
15+ from datetime import datetime
16+ from io import BytesIO , StringIO
1717from unittest import SkipTest , mock
1818
1919import ddt
4444 stream_copy ,
4545)
4646from test .cleanup import cleanup_directory
47- from test .lib .fixtures import writable_fixture
4847from test .lib import TestBase , requires_symlinks , with_rw_repo
48+ from test .lib .fixtures import writable_fixture
4949
5050
5151@pytest .mark .parametrize (
@@ -69,38 +69,20 @@ def read(self, size):
6969@pytest .mark .skipif (sys .platform == "win32" , reason = "requires POSIX unbuffered pipe reads" )
7070def test_stream_copy_keeps_reading_an_unbuffered_pipe ():
7171 read_fd , write_fd = os .pipe ()
72- first_copied = threading .Event ()
73- errors = []
7472
7573 class Destination (BytesIO ):
7674 def write (self , data ):
7775 result = super ().write (data )
78- if data :
79- first_copied .set ()
76+ if data and not writer .closed :
77+ # Make the second chunk available only after the first short read is copied.
78+ writer .write (b"second" )
79+ writer .close ()
8080 return result
8181
82- def produce ():
83- try :
84- os .write (write_fd , b"first-" )
85- if not first_copied .wait (5 ):
86- raise RuntimeError ("first pipe chunk was not copied" )
87- os .write (write_fd , b"second" )
88- except Exception as error :
89- errors .append (error )
90- finally :
91- os .close (write_fd )
92-
9382 destination = Destination ()
94- with os .fdopen (read_fd , "rb" , buffering = 0 ) as source :
95- thread = threading .Thread (target = produce )
96- thread .start ()
97- try :
98- copied = stream_copy (source , destination , chunk_size = 1024 )
99- finally :
100- first_copied .set ()
101- thread .join (5 )
102- assert not thread .is_alive ()
103- assert not errors
83+ with os .fdopen (read_fd , "rb" , buffering = 0 ) as source , os .fdopen (write_fd , "wb" , buffering = 0 ) as writer :
84+ writer .write (b"first-" )
85+ copied = stream_copy (source , destination , chunk_size = 1024 )
10486 assert copied == len (b"first-second" )
10587 assert destination .getvalue () == b"first-second"
10688
0 commit comments