Skip to content

Commit

Permalink
Fix global variable usage
Browse files Browse the repository at this point in the history
The combination of global variable and variable shadowing caused a race
condition that was only reproducible in containers. This commit removes
the both anti-patterns and fixes the issue.

Signed-off-by: Ales Raszka <[email protected]>
  • Loading branch information
Allda committed Sep 14, 2023
1 parent 917c304 commit 258ff3a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def setup_argparser() -> Any: # pragma: no cover


request_ids: Any = None
result_file: Any = None

# wait for signing response for a total of 5 min, at 5 second intervals
TIMEOUT_COUNT = 60
Expand All @@ -105,6 +104,10 @@ class UmbHandler(stomp.ConnectionListener): # type: ignore # pragma: no cover
UmbHandler class
"""

def __init__(self, output_file: str) -> None:
super().__init__()
self.output_file = output_file

def on_error(self, frame: Any) -> None:
"""
On error callback
Expand All @@ -122,7 +125,9 @@ def on_message(self, frame: Any) -> None:
frame (Any): Message frame
"""
# handle response from radas in a thread
thread = threading.Thread(target=process_message, args=[frame.body])
thread = threading.Thread(
target=process_message, args=[frame.body, self.output_file]
)
thread.start()

def on_disconnected(self: Any) -> None:
Expand All @@ -132,19 +137,20 @@ def on_disconnected(self: Any) -> None:
LOGGER.error("Disconnected from umb.")


def process_message(msg: Any) -> None:
def process_message(msg: Any, output_file: str) -> None:
"""
Process a message received from UMB.
Args:
msg: The message body received.
output_file (str): Path to an output file.
"""
msg = json.loads(msg)["msg"]

msg_request_id = msg.get("request_id")
if request_ids and msg_request_id in request_ids:
LOGGER.info("Received radas response: %s", msg)

result_file_path = f"{msg_request_id}-{result_file}"
result_file_path = f"{msg_request_id}-{output_file}"
with open(result_file_path, "w", encoding="utf-8") as result_file_handler:
json.dump(msg, result_file_handler)
LOGGER.info(
Expand Down Expand Up @@ -236,11 +242,13 @@ def request_signature( # pylint: disable=too-many-branches,too-many-statements,
)
sys.exit(1)

output_file = args.output

umb = start_umb_client(
hosts=[args.umb_url], client_name=args.umb_client_name, handler=UmbHandler()
hosts=[args.umb_url],
client_name=args.umb_client_name,
handler=UmbHandler(output_file=output_file),
)
global result_file # pylint: disable=global-statement
result_file = args.output

request_msgs = {}
global request_ids # pylint: disable=global-statement
Expand Down Expand Up @@ -284,11 +292,11 @@ def request_signature( # pylint: disable=too-many-branches,too-many-statements,

sig_received = set()
for request_id in request_ids:
if os.path.exists(f"{request_id}-{result_file}"):
if os.path.exists(f"{request_id}-{output_file}"):
with open(
f"{request_id}-{result_file}", "r", encoding="utf-8"
) as result_file:
result_json = json.load(result_file)
f"{request_id}-{output_file}", "r", encoding="utf-8"
) as result_file_handler:
result_json = json.load(result_file_handler)
signing_status = result_json["signing_status"]
if signing_status == "success":
results.append(result_json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ def create_test_umb_client() -> UmbClient:
def test_process_message_match(mock_exit: MagicMock) -> None:
mock_request_id = "request123"
request_signature.request_ids = [mock_request_id]
request_signature.result_file = "signing_response.json"
mock_open = mock.mock_open()
mock_msg = {"msg": {"request_id": mock_request_id}}
with mock.patch("builtins.open", mock_open):
request_signature.process_message(json.dumps(mock_msg))
request_signature.process_message(json.dumps(mock_msg), "signing_response.json")

mock_open.assert_called_once_with(
f"{mock_request_id}-signing_response.json", "w", encoding="utf-8"
Expand All @@ -39,7 +38,7 @@ def test_process_message_no_match() -> None:
mock_open = mock.mock_open()
mock_msg = {"msg": {"request_id": "no-match"}}
with mock.patch("builtins.open", mock_open):
request_signature.process_message(json.dumps(mock_msg))
request_signature.process_message(json.dumps(mock_msg), "signing_response.json")

mock_open.assert_not_called()

Expand Down

0 comments on commit 258ff3a

Please sign in to comment.