Skip to content
Snippets Groups Projects
Commit 6f61a139 authored by Nathaniel Manista's avatar Nathaniel Manista
Browse files

Add block_until_paused method to PauseFailControl

This method will be used in tests that want to ensure before
proceeding that the system under test has progressed to the control
point.
parent 704fb3f9
No related branches found
No related tags found
No related merge requests found
...@@ -60,10 +60,16 @@ class Control(six.with_metaclass(abc.ABCMeta)): ...@@ -60,10 +60,16 @@ class Control(six.with_metaclass(abc.ABCMeta)):
class PauseFailControl(Control): class PauseFailControl(Control):
"""A Control that can be used to pause or fail code under control.""" """A Control that can be used to pause or fail code under control.
This object is only safe for use from two threads: one of the system under
test calling control and the other from the test system calling pause,
block_until_paused, and fail.
"""
def __init__(self): def __init__(self):
self._condition = threading.Condition() self._condition = threading.Condition()
self._pause = False
self._paused = False self._paused = False
self._fail = False self._fail = False
...@@ -72,19 +78,31 @@ class PauseFailControl(Control): ...@@ -72,19 +78,31 @@ class PauseFailControl(Control):
if self._fail: if self._fail:
raise Defect() raise Defect()
while self._paused: while self._pause:
self._paused = True
self._condition.notify_all()
self._condition.wait() self._condition.wait()
self._paused = False
@contextlib.contextmanager @contextlib.contextmanager
def pause(self): def pause(self):
"""Pauses code under control while controlling code is in context.""" """Pauses code under control while controlling code is in context."""
with self._condition: with self._condition:
self._paused = True self._pause = True
yield yield
with self._condition: with self._condition:
self._paused = False self._pause = False
self._condition.notify_all() self._condition.notify_all()
def block_until_paused(self):
"""Blocks controlling code until code under control is paused.
May only be called within the context of a pause call.
"""
with self._condition:
while not self._paused:
self._condition.wait()
@contextlib.contextmanager @contextlib.contextmanager
def fail(self): def fail(self):
"""Fails code under control while controlling code is in context.""" """Fails code under control while controlling code is in context."""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment