Skip to content

Commit f81c8f4

Browse files
committed
adapt tests
1 parent 4516e8e commit f81c8f4

File tree

10 files changed

+24
-54
lines changed

10 files changed

+24
-54
lines changed

tests/integrations/aiohttp/test_aiohttp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ async def handler(request):
618618

619619
raw_server = await aiohttp_raw_server(handler)
620620

621-
with mock.patch("sentry_sdk.tracing_utils.Random.uniform", return_value=0.5):
621+
with mock.patch("sentry_sdk.tracing_utils.Random.randrange", return_value=500000):
622622
with start_transaction(
623623
name="/interactions/other-dogs/new-dog",
624624
op="greeting.sniff",

tests/integrations/celery/test_celery.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,8 @@ def test_baggage_propagation(init_celery):
518518
def dummy_task(self, x, y):
519519
return _get_headers(self)
520520

521-
# patch random.uniform to return a predictable sample_rand value
522-
with mock.patch("sentry_sdk.tracing_utils.Random.uniform", return_value=0.5):
521+
# patch random.randrange to return a predictable sample_rand value
522+
with mock.patch("sentry_sdk.tracing_utils.Random.randrange", return_value=500000):
523523
with start_transaction() as transaction:
524524
result = dummy_task.apply_async(
525525
args=(1, 0),

tests/integrations/httpx/test_httpx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ def test_outgoing_trace_headers_append_to_baggage(
170170

171171
url = "http://example.com/"
172172

173-
# patch random.uniform to return a predictable sample_rand value
174-
with mock.patch("sentry_sdk.tracing_utils.Random.uniform", return_value=0.5):
173+
# patch random.randrange to return a predictable sample_rand value
174+
with mock.patch("sentry_sdk.tracing_utils.Random.randrange", return_value=500000):
175175
with start_transaction(
176176
name="/interactions/other-dogs/new-dog",
177177
op="greeting.sniff",

tests/integrations/stdlib/test_httplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def test_outgoing_trace_headers_head_sdk(sentry_init, monkeypatch):
236236
monkeypatch.setattr(HTTPSConnection, "send", mock_send)
237237

238238
sentry_init(traces_sample_rate=0.5, release="foo")
239-
with mock.patch("sentry_sdk.tracing_utils.Random.uniform", return_value=0.25):
239+
with mock.patch("sentry_sdk.tracing_utils.Random.randrange", return_value=250000):
240240
transaction = Transaction.continue_from_headers({})
241241

242242
with start_transaction(transaction=transaction, name="Head SDK tx") as transaction:

tests/test_dsc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def my_traces_sampler(sampling_context):
175175
}
176176

177177
# We continue the incoming trace and start a new transaction
178-
with mock.patch("sentry_sdk.tracing_utils.Random.uniform", return_value=0.125):
178+
with mock.patch("sentry_sdk.tracing_utils.Random.randrange", return_value=125000):
179179
transaction = sentry_sdk.continue_trace(incoming_http_headers)
180180
with sentry_sdk.start_transaction(transaction, name="foo"):
181181
pass

tests/test_monitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_transaction_uses_downsampled_rate(
7373
assert monitor.downsample_factor == 1
7474

7575
# make sure we don't sample the transaction
76-
with mock.patch("sentry_sdk.tracing_utils.Random.uniform", return_value=0.75):
76+
with mock.patch("sentry_sdk.tracing_utils.Random.randrange", return_value=750000):
7777
with sentry_sdk.start_transaction(name="foobar") as transaction:
7878
assert transaction.sampled is False
7979
assert transaction.sample_rate == 0.5

tests/test_propagationcontext.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ def test_sample_rand_filled(parent_sampled, sample_rate, expected_interval):
136136
else:
137137
sample_rate_str = ""
138138

139-
# for convenience, we'll just return the lower bound of the interval
140-
mock_uniform = mock.Mock(return_value=expected_interval[0])
139+
# for convenience, we'll just return the lower bound of the interval as an integer
140+
mock_randrange = mock.Mock(return_value=int(expected_interval[0] * 1000000))
141141

142142
def mock_random_class(seed):
143143
assert seed == "00000000000000000000000000000000", "seed should be the trace_id"
144144
rv = Mock()
145-
rv.uniform = mock_uniform
145+
rv.randrange = mock_randrange
146146
return rv
147147

148148
with mock.patch("sentry_sdk.tracing_utils.Random", mock_random_class):
@@ -158,17 +158,20 @@ def mock_random_class(seed):
158158
ctx.dynamic_sampling_context["sample_rand"]
159159
== f"{expected_interval[0]:.6f}" # noqa: E231
160160
)
161-
assert mock_uniform.call_count == 1
162-
assert mock_uniform.call_args[0] == expected_interval
161+
assert mock_randrange.call_count == 1
162+
assert mock_randrange.call_args[0] == (
163+
int(expected_interval[0] * 1000000),
164+
int(expected_interval[1] * 1000000),
165+
)
163166

164167

165168
def test_sample_rand_rounds_down():
166169
# Mock value that should round down to 0.999_999
167-
mock_uniform = mock.Mock(return_value=0.999_999_9)
170+
mock_randrange = mock.Mock(return_value=999999)
168171

169172
def mock_random_class(_):
170173
rv = Mock()
171-
rv.uniform = mock_uniform
174+
rv.randrange = mock_randrange
172175
return rv
173176

174177
with mock.patch("sentry_sdk.tracing_utils.Random", mock_random_class):

tests/tracing/test_integration_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def test_dynamic_sampling_head_sdk_creates_dsc(
169169
envelopes = capture_envelopes()
170170

171171
# make sure transaction is sampled for both cases
172-
with mock.patch("sentry_sdk.tracing_utils.Random.uniform", return_value=0.25):
172+
with mock.patch("sentry_sdk.tracing_utils.Random.randrange", return_value=250000):
173173
transaction = Transaction.continue_from_headers({}, name="Head SDK tx")
174174

175175
# will create empty mutable baggage

tests/tracing/test_sample_rand.py

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import decimal
2-
from decimal import Inexact, FloatOperation
31
from unittest import mock
42

53
import pytest
@@ -20,7 +18,8 @@ def test_deterministic_sampled(sentry_init, capture_events, sample_rate, sample_
2018
events = capture_events()
2119

2220
with mock.patch(
23-
"sentry_sdk.tracing_utils.Random.uniform", return_value=sample_rand
21+
"sentry_sdk.tracing_utils.Random.randrange",
22+
return_value=int(sample_rand * 1000000),
2423
):
2524
with sentry_sdk.start_transaction() as transaction:
2625
assert (
@@ -55,35 +54,3 @@ def test_transaction_uses_incoming_sample_rand(
5554
# Transaction event captured if sample_rand < sample_rate, indicating that
5655
# sample_rand is used to make the sampling decision.
5756
assert len(events) == int(sample_rand < sample_rate)
58-
59-
60-
def test_decimal_context(sentry_init, capture_events):
61-
"""
62-
Ensure that having a user altered decimal context with a precision below 6
63-
does not cause an InvalidOperation exception.
64-
"""
65-
sentry_init(traces_sample_rate=1.0)
66-
events = capture_events()
67-
68-
old_prec = decimal.getcontext().prec
69-
old_inexact = decimal.getcontext().traps[Inexact]
70-
old_float_operation = decimal.getcontext().traps[FloatOperation]
71-
72-
decimal.getcontext().prec = 2
73-
decimal.getcontext().traps[Inexact] = True
74-
decimal.getcontext().traps[FloatOperation] = True
75-
76-
try:
77-
with mock.patch(
78-
"sentry_sdk.tracing_utils.Random.uniform", return_value=0.123456789
79-
):
80-
with sentry_sdk.start_transaction() as transaction:
81-
assert (
82-
transaction.get_baggage().sentry_items["sample_rand"] == "0.123456"
83-
)
84-
finally:
85-
decimal.getcontext().prec = old_prec
86-
decimal.getcontext().traps[Inexact] = old_inexact
87-
decimal.getcontext().traps[FloatOperation] = old_float_operation
88-
89-
assert len(events) == 1

tests/tracing/test_sample_rand_propagation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ def test_continue_trace_missing_sample_rand():
3535
"baggage": "sentry-placeholder=asdf",
3636
}
3737

38-
mock_uniform = Mock(return_value=0.5)
39-
40-
with mock.patch("sentry_sdk.tracing_utils.Random.uniform", mock_uniform):
38+
with mock.patch(
39+
"sentry_sdk.tracing_utils.Random.randrange", Mock(return_value=500000)
40+
):
4141
transaction = sentry_sdk.continue_trace(headers)
4242

4343
assert transaction.get_baggage().sentry_items["sample_rand"] == "0.500000"

0 commit comments

Comments
 (0)