|
| 1 | +import instana.util |
| 2 | +import string |
| 3 | +from nose.tools import assert_equals |
| 4 | + |
| 5 | + |
| 6 | +def test_id_generation(): |
| 7 | + count = 0 |
| 8 | + while count <= 1000: |
| 9 | + id = instana.util.generate_id() |
| 10 | + assert id >= -9223372036854775808 |
| 11 | + assert id <= 9223372036854775807 |
| 12 | + count += 1 |
| 13 | + |
| 14 | + |
| 15 | +def test_id_max_value_and_conversion(): |
| 16 | + max_id = 9223372036854775807 |
| 17 | + min_id = -9223372036854775808 |
| 18 | + max_hex = "7fffffffffffffff" |
| 19 | + min_hex = "8000000000000000" |
| 20 | + |
| 21 | + assert_equals(max_hex, instana.util.id_to_header(max_id)) |
| 22 | + assert_equals(min_hex, instana.util.id_to_header(min_id)) |
| 23 | + |
| 24 | + assert_equals(max_id, instana.util.header_to_id(max_hex)) |
| 25 | + assert_equals(min_id, instana.util.header_to_id(min_hex)) |
| 26 | + |
| 27 | + |
| 28 | +def test_id_conversion_back_and_forth(): |
| 29 | + # id --> header --> id |
| 30 | + original_id = instana.util.generate_id() |
| 31 | + header_id = instana.util.id_to_header(original_id) |
| 32 | + converted_back_id = instana.util.header_to_id(header_id) |
| 33 | + assert original_id == converted_back_id |
| 34 | + |
| 35 | + # header --> id --> header |
| 36 | + original_header_id = "c025ee93b1aeda7b" |
| 37 | + id = instana.util.header_to_id(original_header_id) |
| 38 | + converted_back_header_id = instana.util.id_to_header(id) |
| 39 | + assert_equals(original_header_id, converted_back_header_id) |
| 40 | + |
| 41 | + # Test a random value |
| 42 | + id = -7815363404733516491 |
| 43 | + header = "938a406416457535" |
| 44 | + |
| 45 | + result = instana.util.header_to_id(header) |
| 46 | + assert_equals(id, result) |
| 47 | + |
| 48 | + result = instana.util.id_to_header(id) |
| 49 | + assert_equals(header, result) |
| 50 | + |
| 51 | + |
| 52 | +def test_that_leading_zeros_handled_correctly(): |
| 53 | + header = instana.util.id_to_header(16) |
| 54 | + assert_equals("10", header) |
| 55 | + |
| 56 | + id = instana.util.header_to_id("10") |
| 57 | + assert_equals(16, id) |
| 58 | + |
| 59 | + id = instana.util.header_to_id("0000000000000010") |
| 60 | + assert_equals(16, id) |
| 61 | + |
| 62 | + id = instana.util.header_to_id("88b6c735206ca42") |
| 63 | + assert_equals(615705016619420226, id) |
| 64 | + |
| 65 | + id = instana.util.header_to_id("088b6c735206ca42") |
| 66 | + assert_equals(615705016619420226, id) |
| 67 | + |
| 68 | + |
| 69 | +def test_id_to_header_conversion(): |
| 70 | + # Test passing a standard Integer ID |
| 71 | + original_id = instana.util.generate_id() |
| 72 | + converted_id = instana.util.id_to_header(original_id) |
| 73 | + |
| 74 | + # Assert that it is a string and there are no non-hex characters |
| 75 | + assert isinstance(converted_id, basestring) |
| 76 | + assert all(c in string.hexdigits for c in converted_id) |
| 77 | + |
| 78 | + # Test passing a standard Integer ID as a String |
| 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, basestring) |
| 84 | + assert all(c in string.hexdigits for c in converted_id) |
| 85 | + |
| 86 | + |
| 87 | +def test_id_to_header_conversion_with_bogus_id(): |
| 88 | + # Test passing an empty String |
| 89 | + converted_id = instana.util.id_to_header('') |
| 90 | + |
| 91 | + # Assert that it is a string and there are no non-hex characters |
| 92 | + assert isinstance(converted_id, basestring) |
| 93 | + assert converted_id == '' |
| 94 | + |
| 95 | + # Test passing a nil |
| 96 | + converted_id = instana.util.id_to_header(None) |
| 97 | + |
| 98 | + # Assert that it is a string and there are no non-hex characters |
| 99 | + assert isinstance(converted_id, basestring) |
| 100 | + assert converted_id == '' |
| 101 | + |
| 102 | + # Test passing an Array |
| 103 | + converted_id = instana.util.id_to_header([]) |
| 104 | + |
| 105 | + # Assert that it is a string and there are no non-hex characters |
| 106 | + assert isinstance(converted_id, basestring) |
| 107 | + assert converted_id == '' |
| 108 | + |
| 109 | + |
| 110 | +def test_header_to_id_conversion(): |
| 111 | + # Get a hex string to test against & convert |
| 112 | + header_id = instana.util.id_to_header(instana.util.generate_id) |
| 113 | + converted_id = instana.util.header_to_id(header_id) |
| 114 | + |
| 115 | + # Assert that it is an Integer |
| 116 | + assert isinstance(converted_id, int) |
| 117 | + |
| 118 | + |
| 119 | +def test_header_to_id_conversion_with_bogus_header(): |
| 120 | + # Bogus nil arg |
| 121 | + bogus_result = instana.util.header_to_id(None) |
| 122 | + assert_equals(0, bogus_result) |
| 123 | + |
| 124 | + # Bogus Integer arg |
| 125 | + bogus_result = instana.util.header_to_id(1234) |
| 126 | + assert_equals(0, bogus_result) |
| 127 | + |
| 128 | + # Bogus Array arg |
| 129 | + bogus_result = instana.util.header_to_id([1234]) |
| 130 | + assert_equals(0, bogus_result) |
0 commit comments