Skip to content

Commit

Permalink
Fix for testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
ptr-yudai committed Apr 24, 2024
1 parent 85464df commit 48f4999
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
4 changes: 4 additions & 0 deletions ptrlib/connection/proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def __init__(self,
@property
def returncode(self) -> Optional[int]:
return self._returncode

@property
def pid(self) -> int:
return self._proc.pid

#
# Implementation of Tube methods
Expand Down
12 changes: 2 additions & 10 deletions ptrlib/connection/tube.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ def decorator(self, *args, **kwargs):
return method(self, *args, **kwargs)
return decorator

def tube_is_alive(method):
"""Ensure that connection is not *implicitly* closed
"""
def decorator(self, *args, **kwargs):
assert isinstance(self, Tube), "Invalid usage of decorator"
if not self.is_alive():
raise BrokenPipeError("Connection has already been closed by {str(args[0])}")
return method(self, *args, **kwargs)
return decorator

def tube_is_send_open(method):
"""Ensure that sender connection is not explicitly closed
"""
Expand Down Expand Up @@ -746,6 +736,8 @@ def is_alive(self) -> bool:
print(tube.recv())
```
"""
if self._is_closed:
return False
return self._is_alive_impl()

def shutdown(self, target: Literal['send', 'recv']):
Expand Down
23 changes: 20 additions & 3 deletions ptrlib/connection/winproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,21 @@ def __init__(self,
win32pipe.ConnectNamedPipe(self._stdout.handle)
win32pipe.ConnectNamedPipe(self._stderr.handle)

self._returncode = None

logger.info(f"Successfully created new process {str(self)}")

#
# Property
#
@property
def returncode(self) -> Optional[int]:
return self._returncode

@property
def pid(self) -> int:
return self._pid

#
# Implementation of Tube
#
Expand Down Expand Up @@ -266,17 +279,21 @@ def _is_alive_impl(self) -> bool:
bool: True if process is alive, otherwise False
"""
status = win32process.GetExitCodeProcess(self._proc)
return status == win32con.STILL_ACTIVE
if status == win32con.STILL_ACTIVE:
return True
else:
self._returncode = status
return False

def _shutdown_recv_impl(self):
"""Kill receiver connection
"""
self._stdin.close()
self._stdout.close()

def _shutdown_send_impl(self):
"""Kill sender connection
"""
self._stdout.close()
self._stdin.close()

def __str__(self) -> str:
return f'{self._filepath} (PID={self._pid})'
4 changes: 2 additions & 2 deletions tests/connection/test_windows_proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_basic(self):
with self.assertLogs(module_name) as cm:
p = Process("./tests/test.bin/test_echo.pe.exe")
self.assertEqual(len(cm.output), 1)
self.assertRegex(cm.output[0], fr'^INFO:{module_name}:Successfully created new process \(PID=\d+\)$')
self.assertEqual(cm.output[0], f'INFO:{module_name}:Successfully created new process {str(p)}')
pid = p.pid

# send / recv
Expand Down Expand Up @@ -58,7 +58,7 @@ def test_timeout(self):
with self.assertLogs(module_name) as cm:
p = Process("./tests/test.bin/test_echo.pe.exe")
self.assertEqual(len(cm.output), 1)
self.assertRegex(cm.output[0], fr'^INFO:{module_name}:Successfully created new process \(PID=\d+\)$')
self.assertEqual(cm.output[0], f'INFO:{module_name}:Successfully created new process {str(p)}')

with self.assertRaises(TimeoutError):
p.recvuntil("*** never expected ***", timeout=1)
Expand Down

0 comments on commit 48f4999

Please sign in to comment.