|
13 | 13 |
|
14 | 14 | def test_id_generation(): |
15 | 15 | count = 0 |
16 | | - while count <= 1000: |
| 16 | + while count <= 10000: |
17 | 17 | id = instana.util.generate_id() |
18 | | - assert id >= -9223372036854775808 |
19 | | - assert id <= 9223372036854775807 |
| 18 | + base10_id = int(id, 16) |
| 19 | + assert base10_id >= 0 |
| 20 | + assert base10_id <= 18446744073709551615 |
20 | 21 | count += 1 |
21 | 22 |
|
22 | 23 |
|
23 | | -def test_id_max_value_and_conversion(): |
24 | | - max_id = 9223372036854775807 |
25 | | - min_id = -9223372036854775808 |
26 | | - max_hex = "7fffffffffffffff" |
27 | | - min_hex = "8000000000000000" |
28 | | - |
29 | | - assert_equals(max_hex, instana.util.id_to_header(max_id)) |
30 | | - assert_equals(min_hex, instana.util.id_to_header(min_id)) |
31 | | - |
32 | | - assert_equals(max_id, instana.util.header_to_id(max_hex)) |
33 | | - assert_equals(min_id, instana.util.header_to_id(min_hex)) |
34 | | - |
35 | | - |
36 | | -def test_id_conversion_back_and_forth(): |
37 | | - # id --> header --> id |
38 | | - original_id = instana.util.generate_id() |
39 | | - header_id = instana.util.id_to_header(original_id) |
40 | | - converted_back_id = instana.util.header_to_id(header_id) |
41 | | - assert original_id == converted_back_id |
42 | | - |
43 | | - # header --> id --> header |
44 | | - original_header_id = "c025ee93b1aeda7b" |
45 | | - id = instana.util.header_to_id(original_header_id) |
46 | | - converted_back_header_id = instana.util.id_to_header(id) |
47 | | - assert_equals(original_header_id, converted_back_header_id) |
48 | | - |
49 | | - # Test a random value |
50 | | - id = -7815363404733516491 |
51 | | - header = "938a406416457535" |
52 | | - |
53 | | - result = instana.util.header_to_id(header) |
54 | | - assert_equals(id, result) |
55 | | - |
56 | | - result = instana.util.id_to_header(id) |
57 | | - assert_equals(header, result) |
58 | | - |
59 | | - |
60 | | -def test_that_leading_zeros_handled_correctly(): |
61 | | - header = instana.util.id_to_header(16) |
62 | | - assert_equals("10", header) |
63 | | - |
64 | | - id = instana.util.header_to_id("10") |
65 | | - assert_equals(16, id) |
66 | | - |
67 | | - id = instana.util.header_to_id("0000000000000010") |
68 | | - assert_equals(16, id) |
69 | | - |
70 | | - id = instana.util.header_to_id("88b6c735206ca42") |
71 | | - assert_equals(615705016619420226, id) |
72 | | - |
73 | | - id = instana.util.header_to_id("088b6c735206ca42") |
74 | | - assert_equals(615705016619420226, id) |
75 | | - |
76 | | - |
77 | | -def test_id_to_header_conversion(): |
78 | | - # Test passing a standard Integer ID |
79 | | - original_id = instana.util.generate_id() |
80 | | - converted_id = instana.util.id_to_header(original_id) |
81 | | - |
82 | | - # Assert that it is a string and there are no non-hex characters |
83 | | - assert isinstance(converted_id, string_types) |
84 | | - assert all(c in string.hexdigits for c in converted_id) |
85 | | - |
86 | | - # Test passing a standard Integer ID as a String |
87 | | - original_id = instana.util.generate_id() |
88 | | - converted_id = instana.util.id_to_header(original_id) |
89 | | - |
90 | | - # Assert that it is a string and there are no non-hex characters |
91 | | - assert isinstance(converted_id, string_types) |
92 | | - assert all(c in string.hexdigits for c in converted_id) |
93 | | - |
94 | | - |
95 | | -def test_id_to_header_conversion_with_bogus_id(): |
96 | | - # Test passing an empty String |
97 | | - converted_id = instana.util.id_to_header('') |
98 | | - |
99 | | - # Assert that it is a string and there are no non-hex characters |
100 | | - assert isinstance(converted_id, string_types) |
101 | | - assert converted_id == instana.util.BAD_ID_HEADER |
102 | | - |
103 | | - # Test passing a nil |
104 | | - converted_id = instana.util.id_to_header(None) |
105 | | - |
106 | | - # Assert that it is a string and there are no non-hex characters |
107 | | - assert isinstance(converted_id, string_types) |
108 | | - assert converted_id == instana.util.BAD_ID_HEADER |
109 | | - |
110 | | - # Test passing an Array |
111 | | - converted_id = instana.util.id_to_header([]) |
112 | | - |
113 | | - # Assert that it is a string and there are no non-hex characters |
114 | | - assert isinstance(converted_id, string_types) |
115 | | - assert converted_id == instana.util.BAD_ID_HEADER |
116 | | - |
117 | | - |
118 | | -def test_header_to_id_conversion(): |
| 24 | +def test_various_header_to_id_conversion(): |
119 | 25 | # Get a hex string to test against & convert |
120 | | - header_id = instana.util.id_to_header(instana.util.generate_id) |
| 26 | + header_id = instana.util.generate_id() |
121 | 27 | converted_id = instana.util.header_to_id(header_id) |
| 28 | + assert_equals(header_id, converted_id) |
122 | 29 |
|
123 | | - # Assert that it is an Integer |
124 | | - assert isinstance(converted_id, int) |
| 30 | + # Hex value - result should be left padded |
| 31 | + result = instana.util.header_to_id('abcdef') |
| 32 | + assert_equals('0000000000abcdef', result) |
| 33 | + |
| 34 | + # Hex value |
| 35 | + result = instana.util.header_to_id('0123456789abcdef') |
| 36 | + assert_equals('0123456789abcdef', result) |
| 37 | + |
| 38 | + # Very long incoming header should just return the rightmost 16 bytes |
| 39 | + result = instana.util.header_to_id('0x0123456789abcdef0123456789abcdef') |
| 40 | + assert_equals('0123456789abcdef', result) |
125 | 41 |
|
126 | 42 |
|
127 | 43 | def test_header_to_id_conversion_with_bogus_header(): |
128 | 44 | # Bogus nil arg |
129 | 45 | bogus_result = instana.util.header_to_id(None) |
130 | | - assert_equals(instana.util.BAD_ID_LONG, bogus_result) |
| 46 | + assert_equals(instana.util.BAD_ID, bogus_result) |
131 | 47 |
|
132 | 48 | # Bogus Integer arg |
133 | 49 | bogus_result = instana.util.header_to_id(1234) |
134 | | - assert_equals(instana.util.BAD_ID_LONG, bogus_result) |
| 50 | + assert_equals(instana.util.BAD_ID, bogus_result) |
135 | 51 |
|
136 | 52 | # Bogus Array arg |
137 | 53 | bogus_result = instana.util.header_to_id([1234]) |
138 | | - assert_equals(instana.util.BAD_ID_LONG, bogus_result) |
| 54 | + assert_equals(instana.util.BAD_ID, bogus_result) |
| 55 | + |
| 56 | + # Bogus Hex Values in String |
| 57 | + bogus_result = instana.util.header_to_id('0xZZZZZZ') |
| 58 | + assert_equals(instana.util.BAD_ID, bogus_result) |
| 59 | + |
| 60 | + bogus_result = instana.util.header_to_id('ZZZZZZ') |
| 61 | + assert_equals(instana.util.BAD_ID, bogus_result) |
0 commit comments