Skip to content

Commit

Permalink
Merge pull request #504 from beer-garden/threading_check_current_request
Browse files Browse the repository at this point in the history
Fixed missing requester for spawned threads of SystemClient
  • Loading branch information
TheBurchLog authored Sep 11, 2024
2 parents 244e894 + eb0ad23 commit a03bff8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Brewtils Changelog
TBD

- Expanded PublishClient to support Registering and Unregistering commands to Topics after a plugin has been initialized
- Fixed bug where threading a SystemClient within a plugin lost current_request context and failed to map `requester`,
SystemClient will still drop the current_request context,but the requester field can be provided via `_requester`
- Expanded Garden model to support tracking the version of Beer Garden running

3.27.1
Expand Down
9 changes: 6 additions & 3 deletions brewtils/rest/system_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,16 @@ def _construct_bg_request(self, **kwargs):
publish = kwargs.pop("_publish", None)
topic = kwargs.pop("_topic", None)
propagate = kwargs.pop("_propagate", None)
requester = kwargs.pop("_requester", None)

if parent:
if (
not requester
and parent
and getattr(brewtils.plugin.request_context, "current_request", None)
):
requester = getattr(
brewtils.plugin.request_context.current_request, "requester", None
)
else:
requester = None

if system_display:
metadata["system_display_name"] = system_display
Expand Down
46 changes: 46 additions & 0 deletions test/rest/system_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,52 @@ def test_missing_field(self, monkeypatch, client, remove_kwarg):
with pytest.raises(ValidationError):
client._construct_bg_request(**kwargs)

def test_requester_field(self, monkeypatch, client):
monkeypatch.setattr(client, "_resolve_parameters", Mock())

kwargs = {
"_command": "",
"_system_name": "",
"_system_version": "",
"_instance_name": "",
"_requester": "test",
}

request = client._construct_bg_request(**kwargs)
assert request.requester == "test"

def test_requester_from_parent_field(self, monkeypatch, client):
monkeypatch.setattr(client, "_resolve_parameters", Mock())
monkeypatch.setattr(
brewtils.plugin,
"request_context",
Mock(current_request=Mock(id="1", requester="test")),
)

kwargs = {
"_command": "",
"_system_name": "",
"_system_version": "",
"_instance_name": "",
}

request = client._construct_bg_request(**kwargs)
assert request.requester == "test"

def test_no_requester_from_provided_parent(self, monkeypatch, client):
monkeypatch.setattr(client, "_resolve_parameters", Mock())

kwargs = {
"_command": "",
"_system_name": "",
"_system_version": "",
"_instance_name": "",
"_parent": Mock(id="1"),
}

request = client._construct_bg_request(**kwargs)
assert request.requester is None

def test_positional_parameter(self, client, easy_client, mock_success):
easy_client.create_request.return_value = mock_success

Expand Down

0 comments on commit a03bff8

Please sign in to comment.