Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added windows specific code to check if mock service is stopped. #79

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions pact/pact.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,18 @@ def stop_service(self):
p = psutil.Process(self._process.pid)
for child in p.children(recursive=True):
child.terminate()
p.wait()
if psutil.pid_exists(self._process.pid):
raise RuntimeError(
'There was an error when stopping the Pact mock service.')

else:
self._process.terminate()

self._process.communicate()
if self._process.returncode != 0:
raise RuntimeError(
'There was an error when stopping the Pact mock service.')
self._process.communicate()
if self._process.returncode != 0:
raise RuntimeError(
'There was an error when stopping the Pact mock service.')

def upon_receiving(self, scenario):
"""
Expand Down
30 changes: 27 additions & 3 deletions pact/test/test_pact.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ def setUp(self):
pact.platform, 'platform', autospec=True).start()
self.mock_wait_for_server_start = patch.object(
pact.Pact, '_wait_for_server_start', autospec=True).start()
self.mock_Pid_exists = patch.object(
pact.psutil, 'pid_exists', autospec=True).start()

def test_start_fails(self):
self.mock_Popen.return_value.returncode = 1
Expand Down Expand Up @@ -289,18 +291,22 @@ def test_stop_windows(self):
self.mock_platform.return_value = 'Windows'
ruby_exe = Mock(spec=Process)
self.mock_Process.return_value.children.return_value = [ruby_exe]
self.mock_Pid_exists.return_value = False
pact = Pact(Consumer('consumer'), Provider('provider'))
pact._process = Mock(spec=Popen, pid=999, returncode=0)
pact._process = Mock(spec=Popen, pid=999)
pact.stop_service()

self.assertFalse(pact._process.terminate.called)
pact._process.communicate.assert_called_once_with()
self.assertFalse(pact._process.communicate.called)
self.mock_Process.assert_called_once_with(999)
self.mock_Process.return_value.children.assert_called_once_with(
recursive=True)
ruby_exe.terminate.assert_called_once_with()
self.mock_Process.return_value.wait.assert_called_once_with()
self.mock_Pid_exists.assert_called_once_with(999)

def test_stop_fails(self):
def test_stop_fails_posix(self):
self.mock_platform.return_value = 'Linux'
self.mock_Popen.return_value.returncode = 1
pact = Pact(Consumer('consumer'), Provider('provider'))
pact._process = Mock(spec=Popen, pid=999, returncode=1)
Expand All @@ -310,6 +316,24 @@ def test_stop_fails(self):
pact._process.terminate.assert_called_once_with()
pact._process.communicate.assert_called_once_with()

def test_stop_fails_windows(self):
self.mock_platform.return_value = 'Windows'
self.mock_Popen.return_value.returncode = 15
self.mock_Pid_exists.return_value = True

pact = Pact(Consumer('consumer'), Provider('provider'))
pact._process = Mock(spec=Popen, pid=999, returncode=15)
with self.assertRaises(RuntimeError):
pact.stop_service()

self.assertFalse(pact._process.terminate.called)
self.assertFalse(pact._process.communicate.called)
self.mock_Process.assert_called_once_with(999)
self.mock_Process.return_value.children.assert_called_once_with(
recursive=True)
self.mock_Process.return_value.wait.assert_called_once_with()
self.mock_Pid_exists.assert_called_once_with(999)


class PactWaitForServerStartTestCase(TestCase):
def setUp(self):
Expand Down