|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import unittest |
| 5 | +from unittest import mock |
| 6 | + |
| 7 | +from azure.monitor.opentelemetry.exporter.export.trace._utils import ( |
| 8 | + _get_DJB2_sample_score, |
| 9 | +) |
| 10 | +# fixedint was removed as a source dependency. It is used as a dev requirement to test sample score |
| 11 | +from fixedint import Int32 |
| 12 | +from azure.monitor.opentelemetry.exporter._constants import ( |
| 13 | + _SAMPLING_HASH, |
| 14 | + _INT32_MAX, |
| 15 | + _INT32_MIN, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +class TestGetDJB2SampleScore(unittest.TestCase): |
| 20 | + """Test cases for _get_DJB2_sample_score function.""" |
| 21 | + |
| 22 | + def test_empty_string(self): |
| 23 | + """Test with empty string.""" |
| 24 | + result = _get_DJB2_sample_score("") |
| 25 | + # With empty string, hash should remain _SAMPLING_HASH (5381) |
| 26 | + # Result should be 5381 / _INT32_MAX |
| 27 | + expected = float(5381) / _INT32_MAX |
| 28 | + self.assertEqual(result, expected) |
| 29 | + |
| 30 | + def test_single_character(self): |
| 31 | + """Test with single character.""" |
| 32 | + result = _get_DJB2_sample_score("a") |
| 33 | + # hash = ((5381 << 5) + 5381) + ord('a') |
| 34 | + # hash = (172192 + 5381) + 97 = 177670 |
| 35 | + expected_hash = ((5381 << 5) + 5381) + ord('a') |
| 36 | + expected = float(expected_hash) / _INT32_MAX |
| 37 | + self.assertEqual(result, expected) |
| 38 | + |
| 39 | + def test_typical_trace_id(self): |
| 40 | + """Test with typical 32-character trace ID.""" |
| 41 | + trace_id = "12345678901234567890123456789012" |
| 42 | + result = _get_DJB2_sample_score(trace_id) |
| 43 | + |
| 44 | + # Manually calculate expected result |
| 45 | + hash_value = Int32(_SAMPLING_HASH) |
| 46 | + for char in trace_id: |
| 47 | + hash_value = ((hash_value << 5) + hash_value) + ord(char) |
| 48 | + |
| 49 | + if hash_value == _INT32_MIN: |
| 50 | + hash_value = int(_INT32_MAX) |
| 51 | + else: |
| 52 | + hash_value = abs(hash_value) |
| 53 | + |
| 54 | + expected = float(hash_value) / _INT32_MAX |
| 55 | + self.assertEqual(result, expected) |
| 56 | + |
| 57 | + def test_hex_characters(self): |
| 58 | + """Test with valid hex characters (0-9, a-f).""" |
| 59 | + test_cases = [ |
| 60 | + "0123456789abcdef", |
| 61 | + "fedcba9876543210", |
| 62 | + "aaaaaaaaaaaaaaaa", |
| 63 | + "0000000000000000", |
| 64 | + "ffffffffffffffff" |
| 65 | + ] |
| 66 | + |
| 67 | + for trace_id in test_cases: |
| 68 | + with self.subTest(trace_id=trace_id): |
| 69 | + result = _get_DJB2_sample_score(trace_id) |
| 70 | + self.assertIsInstance(result, float) |
| 71 | + self.assertGreaterEqual(result, 0.0) |
| 72 | + self.assertLessEqual(result, 1.0) |
| 73 | + |
| 74 | + def test_int32_overflow_handling(self): |
| 75 | + """Test that Int32 overflow is handled correctly.""" |
| 76 | + # Create a string that should cause overflow |
| 77 | + long_string = "f" * 100 # 100 'f' characters should cause overflow |
| 78 | + result = _get_DJB2_sample_score(long_string) |
| 79 | + |
| 80 | + self.assertIsInstance(result, float) |
| 81 | + self.assertGreaterEqual(result, 0.0) |
| 82 | + self.assertLessEqual(result, 1.0) |
| 83 | + |
| 84 | + def test_int32_minimum_value_handling(self): |
| 85 | + """Test handling when hash equals INTEGER_MIN.""" |
| 86 | + # This is tricky to test directly since we need to find a string |
| 87 | + # that results in exactly _INT32_MIN. Instead, let's test the logic. |
| 88 | + |
| 89 | + # We'll use a mock to simulate this condition |
| 90 | + |
| 91 | + |
| 92 | + def mock_djb2_with_min_value(trace_id_hex): |
| 93 | + # Call original to get the structure, then simulate _INT32_MIN case |
| 94 | + hash_value = Int32(_SAMPLING_HASH) |
| 95 | + for char in trace_id_hex: |
| 96 | + hash_value = ((hash_value << 5) + hash_value) + ord(char) |
| 97 | + |
| 98 | + # Simulate the case where we get _INT32_MIN |
| 99 | + if str(trace_id_hex) == "test_min": |
| 100 | + hash_value = Int32(_INT32_MIN) |
| 101 | + |
| 102 | + if hash_value == _INT32_MIN: |
| 103 | + hash_value = int(_INT32_MAX) |
| 104 | + else: |
| 105 | + hash_value = abs(hash_value) |
| 106 | + |
| 107 | + return float(hash_value) / _INT32_MAX |
| 108 | + |
| 109 | + # Test the _INT32_MIN case |
| 110 | + result = mock_djb2_with_min_value("test_min") |
| 111 | + expected = float(_INT32_MAX) / _INT32_MAX |
| 112 | + self.assertEqual(result, expected) |
| 113 | + |
| 114 | + def test_negative_hash_conversion(self): |
| 115 | + """Test that negative hash values are converted to positive.""" |
| 116 | + # Find a string that produces a negative hash |
| 117 | + test_string = "negative_test_case_string" |
| 118 | + result = _get_DJB2_sample_score(test_string) |
| 119 | + |
| 120 | + # Result should always be positive (between 0 and 1) |
| 121 | + self.assertGreaterEqual(result, 0.0) |
| 122 | + self.assertLessEqual(result, 1.0) |
| 123 | + |
| 124 | + def test_deterministic_output(self): |
| 125 | + """Test that same input always produces same output.""" |
| 126 | + trace_id = "abcdef1234567890abcdef1234567890" |
| 127 | + |
| 128 | + # Call multiple times with same input |
| 129 | + results = [_get_DJB2_sample_score(trace_id) for _ in range(5)] |
| 130 | + |
| 131 | + # All results should be identical |
| 132 | + self.assertTrue(all(r == results[0] for r in results)) |
| 133 | + |
| 134 | + def test_different_inputs_different_outputs(self): |
| 135 | + """Test that different inputs produce different outputs.""" |
| 136 | + trace_ids = [ |
| 137 | + "12345678901234567890123456789012", |
| 138 | + "12345678901234567890123456789013", # Last digit different |
| 139 | + "22345678901234567890123456789012", # First digit different |
| 140 | + "abcdef1234567890fedcba0987654321", # Completely different |
| 141 | + ] |
| 142 | + |
| 143 | + results = [_get_DJB2_sample_score(tid) for tid in trace_ids] |
| 144 | + |
| 145 | + # All results should be different |
| 146 | + self.assertEqual(len(results), len(set(results))) |
| 147 | + |
| 148 | + def test_boundary_values(self): |
| 149 | + """Test with boundary values and edge cases.""" |
| 150 | + test_cases = [ |
| 151 | + "0", # Single minimum hex digit |
| 152 | + "f", # Single maximum hex digit |
| 153 | + "00000000000000000000000000000000", # All zeros |
| 154 | + "ffffffffffffffffffffffffffffffff", # All f's (32 chars) |
| 155 | + ] |
| 156 | + |
| 157 | + for trace_id in test_cases: |
| 158 | + with self.subTest(trace_id=trace_id): |
| 159 | + result = _get_DJB2_sample_score(trace_id) |
| 160 | + self.assertIsInstance(result, float) |
| 161 | + self.assertGreaterEqual(result, 0.0) |
| 162 | + self.assertLessEqual(result, 1.0) |
| 163 | + |
| 164 | + def test_constants_used_correctly(self): |
| 165 | + """Test that the function uses the expected constants.""" |
| 166 | + # Verify that _SAMPLING_HASH is 5381 (standard DJB2 hash initial value) |
| 167 | + self.assertEqual(_SAMPLING_HASH, 5381) |
| 168 | + |
| 169 | + # Verify Int32 constants |
| 170 | + self.assertEqual(_INT32_MAX, 2147483647) # 2^31 - 1 |
| 171 | + self.assertEqual(_INT32_MIN, -2147483648) # -2^31 |
| 172 | + |
| 173 | + def test_algorithm_correctness(self): |
| 174 | + """Test that the DJB2 algorithm is implemented correctly.""" |
| 175 | + # Test with known input and manually calculated expected output |
| 176 | + trace_id = "abc" |
| 177 | + |
| 178 | + # Manual calculation: |
| 179 | + # Start with 5381 |
| 180 | + # For 'a' (97): hash = ((5381 << 5) + 5381) + 97 = 177670 |
| 181 | + # For 'b' (98): hash = ((177670 << 5) + 177670) + 98 = 5823168 |
| 182 | + # For 'c' (99): hash = ((5823168 << 5) + 5823168) + 99 = 191582563 |
| 183 | + |
| 184 | + expected_hash = _SAMPLING_HASH |
| 185 | + for char in trace_id: |
| 186 | + expected_hash = Int32(((expected_hash << 5) + expected_hash) + ord(char)) |
| 187 | + |
| 188 | + if expected_hash == _INT32_MIN: |
| 189 | + expected_hash = int(_INT32_MAX) |
| 190 | + else: |
| 191 | + expected_hash = abs(expected_hash) |
| 192 | + |
| 193 | + expected_result = float(expected_hash) / _INT32_MAX |
| 194 | + actual_result = _get_DJB2_sample_score(trace_id) |
| 195 | + |
| 196 | + self.assertEqual(actual_result, expected_result) |
0 commit comments