Skip to content

Commit

Permalink
Fix message race when using clear_messages()
Browse files Browse the repository at this point in the history
This handles a scenario that a message arrives between a call to get_messages() and clear_messages(). clear_messages() will only clear the messages that has been evaluated atleast once.

A new method clear_all_messages() has been added to clear the entire message stack and is used between scenarios to reset the list.
  • Loading branch information
forslund committed Jul 9, 2021
1 parent 80f9eb0 commit 5f36a62
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion test/integrationtests/voight_kampff/features/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self):
self.messages = []
self.message_lock = Lock()
self.new_message_available = Event()
self._processed_messages = 0

def on_message(self, message):
with self.message_lock:
Expand All @@ -52,18 +53,32 @@ def on_message(self, message):

def get_messages(self, msg_type):
with self.message_lock:
self._processed_messages = len(self.messages)
if msg_type is None:
return [m for m in self.messages]
else:
return [m for m in self.messages if m.msg_type == msg_type]

def remove_message(self, msg):
with self.message_lock:
if msg not in self.messages:
raise ValueError(f'{msg} was not found in '
'the list of messages.')
# Update processed message count if a read message was removed
if self.messages.index(msg) < self._processed_messages:
self._processed_messages -= 1

self.messages.remove(msg)

def clear_messages(self):
with self.message_lock:
self.messages = self.messages[:self._processed_messages]
self._processed_messages = 0

def clear_all_messages(self):
with self.message_lock:
self.messages = []
self._processed_messages = 0


def before_all(context):
Expand Down Expand Up @@ -119,6 +134,6 @@ def after_scenario(context, scenario):
# TODO wait for skill handler complete
sleep(0.5)
wait_while_speaking()
context.bus.clear_messages()
context.bus.clear_all_messages()
context.matched_message = None
context.step_timeout = 10 # Reset the step_timeout to 10 seconds

0 comments on commit 5f36a62

Please sign in to comment.