Skip to content

Commit

Permalink
fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylphrex committed Mar 18, 2024
1 parent f44dec5 commit 95331f5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 55 deletions.
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,3 +652,15 @@ def patch_start_tracing_child(fake_transaction_is_none=False):
return_value=fake_transaction,
):
yield fake_start_child


class ApproxDict(dict):
def __eq__(self, other):
# For an ApproxDict to equal another dict, the other dict just needs to contain
# all the keys from the ApproxDict with the same values.
#
# The other dict may contain additional keys with any value.
return all(key in other and other[key] == value for key, value in self.items())

def __ne__(self, other):
return not self.__eq__(other)
13 changes: 9 additions & 4 deletions tests/integrations/boto3/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from sentry_sdk import Hub
from sentry_sdk.integrations.boto3 import Boto3Integration
from tests.conftest import ApproxDict
from tests.integrations.boto3.aws_mock import MockResponse
from tests.integrations.boto3 import read_fixture

Expand Down Expand Up @@ -65,12 +66,12 @@ def test_streaming(sentry_init, capture_events):
span1 = event["spans"][0]
assert span1["op"] == "http.client"
assert span1["description"] == "aws.s3.GetObject"
assert span1["data"] == {
assert span1["data"] == ApproxDict({
"http.method": "GET",
"aws.request.url": "https://bucket.s3.amazonaws.com/foo.pdf",
"http.fragment": "",
"http.query": "",
}
})

span2 = event["spans"][1]
assert span2["op"] == "http.client.stream"
Expand Down Expand Up @@ -123,7 +124,11 @@ def test_omit_url_data_if_parsing_fails(sentry_init, capture_events):
transaction.finish()

(event,) = events
assert event["spans"][0]["data"] == {
assert event["spans"][0]["data"] == ApproxDict({
"http.method": "GET",
# no url data
}
})

assert "aws.request.url" not in event["spans"][0]["data"]
assert "http.fragment" not in event["spans"][0]["data"]
assert "http.query" not in event["spans"][0]["data"]
3 changes: 2 additions & 1 deletion tests/integrations/stdlib/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from sentry_sdk import capture_message, start_transaction
from sentry_sdk._compat import PY2
from sentry_sdk.integrations.stdlib import StdlibIntegration
from tests.conftest import ApproxDict


if PY2:
Expand Down Expand Up @@ -125,7 +126,7 @@ def test_subprocess_basic(

assert message_event["message"] == "hi"

data = {"subprocess.cwd": os.getcwd()} if with_cwd else {}
data = ApproxDict({"subprocess.cwd": os.getcwd()} if with_cwd else {})

(crumb,) = message_event["breadcrumbs"]["values"]
assert crumb == {
Expand Down
46 changes: 0 additions & 46 deletions tests/test_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,52 +555,6 @@ def test_extract_stack_with_cache(frame, depth):
assert frame1 is frame2, i


@requires_python_version(3, 3)
@requires_gevent
def test_get_current_thread_id_gevent_in_thread():
results = Queue(maxsize=1)

def target():
job = gevent.spawn(get_current_thread_id)
job.join()
results.put(job.value)

thread = threading.Thread(target=target)
thread.start()
thread.join()
assert thread.ident == results.get(timeout=1)


@requires_python_version(3, 3)
def test_get_current_thread_id_running_thread():
results = Queue(maxsize=1)

def target():
results.put(get_current_thread_id())

thread = threading.Thread(target=target)
thread.start()
thread.join()
assert thread.ident == results.get(timeout=1)


@requires_python_version(3, 3)
def test_get_current_thread_id_main_thread():
results = Queue(maxsize=1)

def target():
# mock that somehow the current thread doesn't exist
with mock.patch("threading.current_thread", side_effect=[None]):
results.put(get_current_thread_id())

thread_id = threading.main_thread().ident if sys.version_info >= (3, 4) else None

thread = threading.Thread(target=target)
thread.start()
thread.join()
assert thread_id == results.get(timeout=1)


def get_scheduler_threads(scheduler):
return [thread for thread in threading.enumerate() if thread.name == scheduler.name]

Expand Down
8 changes: 4 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ def test_duration_in_milliseconds(timedelta, expected_milliseconds):
assert duration_in_milliseconds(timedelta) == expected_milliseconds


def test_get_current_thread_id_explicit_thread():
def test_get_current_thread_meta_explicit_thread():
results = Queue(maxsize=1)

def target1():
Expand All @@ -639,7 +639,7 @@ def target2():


@pytest.mark.skipif(gevent is None, reason="gevent not enabled")
def test_get_current_thread_id_gevent_in_thread():
def test_get_current_thread_meta_gevent_in_thread():
results = Queue(maxsize=1)

def target():
Expand All @@ -653,7 +653,7 @@ def target():
assert (thread.ident, thread.name) == results.get(timeout=1)


def test_get_current_thread_id_running_thread():
def test_get_current_thread_meta_running_thread():
results = Queue(maxsize=1)

def target():
Expand All @@ -666,7 +666,7 @@ def target():


@pytest.mark.skipif(sys.version_info < (3, 4), reason="threading.main_thread() Not available")
def test_get_current_thread_id_main_thread():
def test_get_current_thread_meta_main_thread():
results = Queue(maxsize=1)

def target():
Expand Down

0 comments on commit 95331f5

Please sign in to comment.