Skip to content

Commit

Permalink
Revert "rework: threads all the way down (#44)" (#46)
Browse files Browse the repository at this point in the history
This reverts commit 7ed21c8.

Co-authored-by: Steve Bunting <[email protected]>
  • Loading branch information
stevenbunting and Steve Bunting authored Dec 6, 2024
1 parent 7ed21c8 commit 46c7414
Show file tree
Hide file tree
Showing 20 changed files with 433 additions and 356 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
pytest tests/test_core.py
pytest tests/test_ignored_domains.py
pytest tests/test_remote_config.py
pytest tests/test_repeating_thread.py
pytest tests/caching/test_location_request_body.py
pytest tests/caching/test_location_request_headers.py
pytest tests/redaction/test_no_redaction.py
Expand Down
271 changes: 204 additions & 67 deletions src/supergood/client.py

Large diffs are not rendered by default.

125 changes: 0 additions & 125 deletions src/supergood/worker.py

This file was deleted.

8 changes: 2 additions & 6 deletions tests/caching/test_location_request_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,11 @@ def test_request_body(self, httpserver, supergood_client):
url=httpserver.url_for("/200"),
data="blah scoobydoobydoo blah",
)
# verify that the event was not appended to the worker
entries = supergood_client.flush_thread.append.call_args
assert entries is None
supergood_client.flush_cache()
assert Api.post_events.call_args is None
requests.request(
method="get", url=httpserver.url_for("/200"), data="blah scrappydootoo blah"
)
# in this case the event _was_ added to the worker
entries = supergood_client.flush_thread.append.call_args[0][0]
supergood_client.flush_cache(entries)
supergood_client.flush_cache()
args = Api.post_events.call_args[0][0]
assert len(args) == 1
7 changes: 3 additions & 4 deletions tests/caching/test_location_request_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ def test_request_headers(self, httpserver, supergood_client):
}
)
requests.get(httpserver.url_for("/200"), headers={"X-test": "scoobydoo"})
entries = supergood_client.flush_thread.append.call_args
assert entries is None
supergood_client.flush_cache()
assert Api.post_events.call_args is None
requests.get(httpserver.url_for("/200"), headers={"X-test": "scrappydootoo"})
entries = supergood_client.flush_thread.append.call_args[0][0]
supergood_client.flush_cache(entries)
supergood_client.flush_cache()
args = Api.post_events.call_args[0][0]
assert len(args) == 1
7 changes: 3 additions & 4 deletions tests/caching/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ def test_method_matching(self, httpserver: HTTPServer, supergood_client):
)
# First call is ignored due to matching the ignored POST methods
assert response1.json()["string"] == "abc"
entries = supergood_client.flush_thread.append.call_args
assert entries is None
supergood_client.flush_cache()
assert Api.post_events.call_args is None
response2 = requests.request(method="get", url=httpserver.url_for("/200"))
# Second call is cached and flushed because it does not match (i.e. is a new endpoint)
assert response2.json()["string"] == "def"
entries = supergood_client.flush_thread.append.call_args[0][0]
supergood_client.flush_cache(entries)
supergood_client.flush_cache()
args = Api.post_events.call_args[0][0]
assert len(args) == 1
105 changes: 68 additions & 37 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,79 @@
from unittest.mock import MagicMock

import pytest

from supergood import Client
from tests.helper import get_config, get_remote_config


@pytest.fixture(scope="function")
@pytest.fixture(scope="session")
def broken_redaction(session_mocker):
session_mocker.patch(
"supergood.client.redact_values", side_effect=Exception
).start()
yield session_mocker


@pytest.fixture(scope="session")
def monkeysession():
with pytest.MonkeyPatch.context() as mp:
yield mp


@pytest.fixture
def supergood_client(request, mocker):
with pytest.MonkeyPatch.context() as mp:
config = get_config()
remote_config = get_remote_config()

if getattr(request, "param", None):
if request.param.get("config", None):
config = request.param["config"]
if request.param.get("remote_config", None):
remote_config = request.param["remote_config"]

Client.initialize(
client_id="client_id",
client_secret_id="client_secret_id",
base_url="https://api.supergood.ai",
telemetry_url="https://telemetry.supergood.ai",
config=config,
)
# first 3 are just to make sure we don't post anything externally
mocker.patch("supergood.api.Api.post_events", return_value=None).start()
mocker.patch("supergood.api.Api.post_errors", return_value=None).start()
mocker.patch("supergood.api.Api.post_telemetry", return_value=None).start()
# next we make sure we don't call get externally, and stub in our remote config
mocker.patch("supergood.api.Api.get_config", return_value=remote_config).start()
# Turns off the worker, pytest mocks don't always play well with threads
mocker.patch("supergood.worker.Worker.start", return_value=None).start()
mocker.patch("supergood.worker.Repeater.start", return_value=None).start()
mocker.patch("supergood.worker.Worker.append", return_value=True).start()
mp.setenv("SG_OVERRIDE_AUTO_FLUSH", "false")
mp.setenv("SG_OVERRIDE_AUTO_CONFIG", "false")
Client._get_config()
yield Client
Client.kill()
@pytest.fixture(scope="session")
def broken_client(broken_redaction, monkeysession):
config = get_config()
remote_config = get_remote_config()
broken_redaction.patch("supergood.api.Api.post_events", return_value=None).start()
broken_redaction.patch("supergood.api.Api.post_errors", return_value=None).start()
broken_redaction.patch(
"supergood.api.Api.get_config", return_value=remote_config
).start()
Client.initialize(
client_id="client_id",
client_secret_id="client_secret_id",
base_url="https://api.supergood.ai",
telemetry_url="https://telemetry.supergood.ai",
config=config,
)
monkeysession.setenv("SG_OVERRIDE_AUTO_FLUSH", "false")
monkeysession.setenv("SG_OVERRIDE_AUTO_CONFIG", "false")
Client._get_config()
yield Client
Client.kill() # on exit


@pytest.fixture(scope="session")
def supergood_client(request, session_mocker, monkeysession):
# Allows for a param dictionary to control behavior
# currently looks for "config" "remote_config" and "auto"
config = get_config()
auto = False
remote_config = get_remote_config()
if getattr(request, "param", None):
if request.param.get("config", None):
config = request.param["config"]
if request.param.get("remote_config", None):
remote_config = request.param["remote_config"]
if request.param.get("auto", None):
auto = request.param["auto"]

session_mocker.patch("supergood.api.Api.post_events", return_value=None).start()
session_mocker.patch("supergood.api.Api.post_errors", return_value=None).start()
session_mocker.patch(
"supergood.api.Api.get_config", return_value=remote_config
).start()
session_mocker.patch("supergood.api.Api.post_telemetry", return_value=None).start()

if not auto:
monkeysession.setenv("SG_OVERRIDE_AUTO_FLUSH", "false")
monkeysession.setenv("SG_OVERRIDE_AUTO_CONFIG", "false")

Client.initialize(
client_id="client_id",
client_secret_id="client_secret_id",
base_url="https://api.supergood.ai",
telemetry_url="https://telemetry.supergood.ai",
config=config,
)
Client._get_config()
yield Client
Client.kill() # on exit
35 changes: 14 additions & 21 deletions tests/redaction/test_no_redaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,23 @@
class TestNoRedaction:
def test_ignore_redaction(self, httpserver, supergood_client):
supergood_client.base_config["ignoreRedaction"] = True
# httpserver.expect_request("/200").respond_with_json(
# {
# "string": "abc",
# "complex_string": "Alex Klarfeld 911!",
# }
# )
# requests.get(httpserver.url_for("/200"))
inputs = {
"request_id": {
"request": {
"body": "",
"headers": {},
"method": "GET",
"url": httpserver.url_for("/200"),
},
"response": {"body": {"string": "abc"}, "headers": {}},
httpserver.expect_request("/200").respond_with_json(
{
"string": "abc",
"complex_string": "Alex Klarfeld 911!",
}
}
supergood_client.flush_cache(inputs)
)
requests.get(httpserver.url_for("/200"))
supergood_client.flush_cache()
args = Api.post_events.call_args[0][0]
response_body = args[0]["response"]["body"]
assert response_body["string"] == "abc" # not redacted!
assert response_body["complex_string"] == "Alex Klarfeld 911!"
assert "metadata" in args[0]
assert args[0]["metadata"] == {
"endpointId": "endpoint-id",
"vendorId": "vendor-id",
}

def test_no_redaction(self, httpserver, supergood_client):
httpserver.expect_request("/201").respond_with_json(
Expand All @@ -44,9 +39,7 @@ def test_no_redaction(self, httpserver, supergood_client):
}
)
requests.get(httpserver.url_for("/201"))
append_call = supergood_client.flush_thread.append.call_args[0][0]
supergood_client.flush_cache(append_call)

supergood_client.flush_cache()
args = Api.post_events.call_args[0][0]
body = args[0]["response"]["body"]
assert body["string"] == "abc"
Expand Down
3 changes: 1 addition & 2 deletions tests/redaction/test_redact_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def test_redact_all(self, httpserver, supergood_client):
}
)
requests.get(httpserver.url_for("/200"))
entries = supergood_client.flush_thread.append.call_args[0][0]
supergood_client.flush_cache(entries)
supergood_client.flush_cache()
args = Api.post_events.call_args[0][0]
response_body = args[0]["response"]["body"]
metadata = args[0]["metadata"]
Expand Down
9 changes: 3 additions & 6 deletions tests/redaction/test_redact_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def test_redact_array_element(self, httpserver, supergood_client):
}
)
requests.get(httpserver.url_for("/200"))
entries = supergood_client.flush_thread.append.call_args[0][0]
supergood_client.flush_cache(entries)
supergood_client.flush_cache()
args = Api.post_events.call_args[0][0]
body = args[0]["response"]["body"]
metadata = args[0]["metadata"]
Expand All @@ -52,8 +51,7 @@ def test_redact_nested_array_element(self, httpserver, supergood_client):
}
)
requests.get(httpserver.url_for("/200"))
entries = supergood_client.flush_thread.append.call_args[0][0]
supergood_client.flush_cache(entries)
supergood_client.flush_cache()
args = Api.post_events.call_args[0][0]
body = args[0]["response"]["body"]
metadata = args[0]["metadata"]
Expand All @@ -79,8 +77,7 @@ def test_redact_array_sub_element(self, httpserver, supergood_client):
}
)
requests.get(httpserver.url_for("/200"))
entries = supergood_client.flush_thread.append.call_args[0][0]
supergood_client.flush_cache(entries)
supergood_client.flush_cache()
args = Api.post_events.call_args[0][0]
body = args[0]["response"]["body"]
assert len(body) == 1
Expand Down
Loading

0 comments on commit 46c7414

Please sign in to comment.