|
| 1 | +import contextlib |
1 | 2 | import os
|
2 | 3 | import json
|
3 | 4 | import subprocess
|
@@ -1496,3 +1497,66 @@ def test_dropped_transaction(sentry_init, capture_record_lost_event_calls, test_
|
1496 | 1497 | def test_enable_tracing_deprecated(sentry_init, enable_tracing):
|
1497 | 1498 | with pytest.warns(DeprecationWarning):
|
1498 | 1499 | sentry_init(enable_tracing=enable_tracing)
|
| 1500 | + |
| 1501 | + |
| 1502 | +def make_options_transport_cls(): |
| 1503 | + """Make an options transport class that captures the options passed to it.""" |
| 1504 | + # We need a unique class for each test so that the options are not |
| 1505 | + # shared between tests. |
| 1506 | + |
| 1507 | + class OptionsTransport(Transport): |
| 1508 | + """Transport that captures the options passed to it.""" |
| 1509 | + |
| 1510 | + def __init__(self, options): |
| 1511 | + super().__init__(options) |
| 1512 | + type(self).options = options |
| 1513 | + |
| 1514 | + def capture_envelope(self, _): |
| 1515 | + pass |
| 1516 | + |
| 1517 | + return OptionsTransport |
| 1518 | + |
| 1519 | + |
| 1520 | +@contextlib.contextmanager |
| 1521 | +def clear_env_var(name): |
| 1522 | + """Helper to clear the a given environment variable, |
| 1523 | + and restore it to its original value on exit.""" |
| 1524 | + old_value = os.environ.pop(name, None) |
| 1525 | + |
| 1526 | + try: |
| 1527 | + yield |
| 1528 | + finally: |
| 1529 | + if old_value is not None: |
| 1530 | + os.environ[name] = old_value |
| 1531 | + elif name in os.environ: |
| 1532 | + del os.environ[name] |
| 1533 | + |
| 1534 | + |
| 1535 | +@pytest.mark.parametrize( |
| 1536 | + ("env_value", "arg_value", "expected_value"), |
| 1537 | + [ |
| 1538 | + (None, None, False), # default |
| 1539 | + ("0", None, False), # env var false |
| 1540 | + ("1", None, True), # env var true |
| 1541 | + (None, False, False), # arg false |
| 1542 | + (None, True, True), # arg true |
| 1543 | + # Argument overrides environment variable |
| 1544 | + ("0", True, True), # env false, arg true |
| 1545 | + ("1", False, False), # env true, arg false |
| 1546 | + ], |
| 1547 | +) |
| 1548 | +def test_keep_alive(env_value, arg_value, expected_value): |
| 1549 | + transport_cls = make_options_transport_cls() |
| 1550 | + keep_alive_kwarg = {} if arg_value is None else {"keep_alive": arg_value} |
| 1551 | + |
| 1552 | + with clear_env_var("SENTRY_KEEP_ALIVE"): |
| 1553 | + if env_value is not None: |
| 1554 | + os.environ["SENTRY_KEEP_ALIVE"] = env_value |
| 1555 | + |
| 1556 | + sentry_sdk.init( |
| 1557 | + dsn="http://[email protected]/123", |
| 1558 | + transport=transport_cls, |
| 1559 | + **keep_alive_kwarg, |
| 1560 | + ) |
| 1561 | + |
| 1562 | + assert transport_cls.options["keep_alive"] is expected_value |
0 commit comments