Skip to content

Commit

Permalink
Fixed race-condition with TestApplication pickle file.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbarber authored and carltongibson committed Apr 15, 2020
1 parent 5cf15bd commit 1765187
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions daphne/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import tempfile
import traceback
from concurrent.futures import CancelledError
from functools import partial


class DaphneTestingInstance:
Expand All @@ -21,6 +22,7 @@ def __init__(self, xff=False, http_timeout=None):
self.xff = xff
self.http_timeout = http_timeout
self.host = "127.0.0.1"
self.lock = multiprocessing.Lock()

def __enter__(self):
# Clear result storage
Expand All @@ -38,7 +40,7 @@ def __enter__(self):
# Start up process
self.process = DaphneProcess(
host=self.host,
application=TestApplication,
application=partial(TestApplication, lock=self.lock),
kwargs=kwargs,
setup=self.process_setup,
teardown=self.process_teardown,
Expand Down Expand Up @@ -82,7 +84,8 @@ def get_received(self):
raises them.
"""
try:
inner_result = TestApplication.load_result()
with self.lock:
inner_result = TestApplication.load_result()
except FileNotFoundError:
raise ValueError("No results available yet.")
# Check for exception
Expand Down Expand Up @@ -167,8 +170,9 @@ class TestApplication:
setup_storage = os.path.join(tempfile.gettempdir(), "setup.testio")
result_storage = os.path.join(tempfile.gettempdir(), "result.testio")

def __init__(self, scope):
def __init__(self, scope, lock):
self.scope = scope
self.lock = lock
self.messages = []

async def __call__(self, send, receive):
Expand All @@ -178,8 +182,10 @@ async def __call__(self, send, receive):
while True:
# Receive a message and save it into the result store
self.messages.append(await receive())
self.lock.acquire()
logging.debug("test app received %r", self.messages[-1])
self.save_result(self.scope, self.messages)
self.lock.release()
# See if there are any messages to send back
setup = self.load_setup()
self.delete_setup()
Expand Down

0 comments on commit 1765187

Please sign in to comment.