|
4 | 4 | import unittest |
5 | 5 | import importlib |
6 | 6 | import sys |
| 7 | +import types |
7 | 8 | import pytest |
8 | 9 |
|
9 | 10 | from unittest.mock import MagicMock, patch, call, ANY |
@@ -77,6 +78,58 @@ def setUp(self): |
77 | 78 | self.mock_dd_lambda_layer_tag = patcher.start() |
78 | 79 | self.addCleanup(patcher.stop) |
79 | 80 |
|
| 81 | + def test_real_decorator_calls_feature_flag_flush_contract(self): |
| 82 | + import ddtrace.internal.openfeature as openfeature_pkg |
| 83 | + from datadog_lambda import feature_flags |
| 84 | + |
| 85 | + calls = [] |
| 86 | + fake = types.ModuleType("ddtrace.internal.openfeature._telemetry") |
| 87 | + fake.FLUSH_API_VERSION = 1 |
| 88 | + fake.flush = lambda timeout: calls.append(timeout) or "completed" |
| 89 | + self.addCleanup(importlib.reload, feature_flags) |
| 90 | + with patch.dict(sys.modules, {fake.__name__: fake}), patch.object( |
| 91 | + openfeature_pkg, "_telemetry", fake, create=True |
| 92 | + ), patch.object(type(config), "feature_flags_enabled", True): |
| 93 | + importlib.reload(feature_flags) |
| 94 | + context = get_mock_context() |
| 95 | + context.get_remaining_time_in_millis = lambda: 30000 |
| 96 | + |
| 97 | + handler = wrapper.datadog_lambda_wrapper( |
| 98 | + lambda event, context: {"ok": True} |
| 99 | + ) |
| 100 | + self.assertEqual(handler({}, context), {"ok": True}) |
| 101 | + |
| 102 | + error = ValueError("handler error") |
| 103 | + |
| 104 | + def raising(event, context): |
| 105 | + raise error |
| 106 | + |
| 107 | + with self.assertRaises(ValueError) as caught: |
| 108 | + wrapper.datadog_lambda_wrapper(raising)({}, context) |
| 109 | + self.assertIs(caught.exception, error) |
| 110 | + self.assertEqual(calls, [0.5, 0.5]) |
| 111 | + |
| 112 | + def test_disabled_feature_flags_skip_flush(self): |
| 113 | + with patch.object(type(config), "feature_flags_enabled", False), patch.object( |
| 114 | + wrapper, "flush_feature_flag_telemetry" |
| 115 | + ) as flush: |
| 116 | + context = get_mock_context() |
| 117 | + context.get_remaining_time_in_millis = lambda: 30000 |
| 118 | + handler = wrapper.datadog_lambda_wrapper( |
| 119 | + lambda event, context: {"ok": True} |
| 120 | + ) |
| 121 | + self.assertEqual(handler({}, context), {"ok": True}) |
| 122 | + |
| 123 | + error = ValueError("handler error") |
| 124 | + |
| 125 | + def raising(event, context): |
| 126 | + raise error |
| 127 | + |
| 128 | + with self.assertRaises(ValueError) as caught: |
| 129 | + wrapper.datadog_lambda_wrapper(raising)({}, context) |
| 130 | + self.assertIs(caught.exception, error) |
| 131 | + flush.assert_not_called() |
| 132 | + |
80 | 133 | @patch("datadog_lambda.config.Config.trace_enabled", False) |
81 | 134 | def test_datadog_lambda_wrapper(self): |
82 | 135 | @wrapper.datadog_lambda_wrapper |
|
0 commit comments