-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathDDSpanIdTest.java
More file actions
145 lines (130 loc) · 6.08 KB
/
DDSpanIdTest.java
File metadata and controls
145 lines (130 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package datadog.trace.api;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import datadog.trace.test.util.TableTestTypeConverters;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.tabletest.junit.TableTest;
import org.tabletest.junit.TypeConverterSources;
@TypeConverterSources(TableTestTypeConverters.class)
class DDSpanIdTest {
@TableTest({
"scenario | stringId | expectedId ",
"zero | '0' | 0 ",
"one | '1' | 1 ",
"max | '18446744073709551615' | DDSpanId.MAX ",
"long max | '9223372036854775807' | Long.MAX_VALUE ",
"long max plus one | '9223372036854775808' | Long.MIN_VALUE "
})
@ParameterizedTest
void convertIdsFromToString(String stringId, long expectedId) {
long ddid = DDSpanId.from(stringId);
assertEquals(expectedId, ddid);
assertEquals(stringId, DDSpanId.toString(ddid));
}
@ParameterizedTest
@NullSource
@ValueSource(
strings = {
"",
"-1",
"18446744073709551616",
"18446744073709551625",
"184467440737095516150",
"18446744073709551a1",
"184467440737095511a"
})
void failOnIllegalString(String stringId) {
assertThrows(NumberFormatException.class, () -> DDSpanId.from(stringId));
}
@TableTest({
"scenario | hexId | expectedId ",
"zero | '0' | 0 ",
"one | '1' | 1 ",
"max | 'ffffffffffffffff' | DDSpanId.MAX ",
"long max | '7fffffffffffffff' | Long.MAX_VALUE ",
"long min | '8000000000000000' | Long.MIN_VALUE ",
"long min with leading zeros | '00008000000000000000' | Long.MIN_VALUE ",
"cafebabe | 'cafebabe' | 3405691582 ",
"fifteen hex digits | '123456789abcdef' | 81985529216486895 "
})
@ParameterizedTest
void convertIdsFromToHexString(String hexId, long expectedId) {
long ddid = DDSpanId.fromHex(hexId);
String padded16 =
hexId.length() <= 16 ? leftPadWithZeros(hexId, 16) : hexId.substring(hexId.length() - 16);
String normalizedHexId = hexId.length() > 1 ? hexId.replaceFirst("^0+", "") : hexId;
if (normalizedHexId.isEmpty()) {
normalizedHexId = "0";
}
assertEquals(expectedId, ddid);
assertEquals(normalizedHexId, DDSpanId.toHexString(ddid));
assertEquals(padded16, DDSpanId.toHexStringPadded(ddid));
}
@TableTest({
"scenario | hexId | start | length | lowerCaseOnly | expectedId ",
"null input | | 1 | 1 | false | ",
"empty input | '' | 1 | 1 | false | ",
"negative start | '00' | -1 | 1 | false | ",
"zero length | '00' | 0 | 0 | false | ",
"single zero at index 0 | '00' | 0 | 1 | false | DDSpanId.ZERO",
"single zero at index 1 | '00' | 1 | 1 | false | DDSpanId.ZERO",
"single zero at index 1 duplicate | '00' | 1 | 1 | false | DDSpanId.ZERO",
"max lower-case | 'ffffffffffffffff' | 0 | 16 | true | DDSpanId.MAX ",
"upper-case rejected when lower-case only| 'ffffffffffffFfff' | 0 | 16 | true | ",
"upper-case accepted when lower disabled | 'ffffffffffffFfff' | 0 | 16 | false | DDSpanId.MAX "
})
@ParameterizedTest
void convertIdsFromPartOfHexString(
String hexId, int start, int length, boolean lowerCaseOnly, Long expectedId) {
Long parsedId = null;
try {
parsedId = DDSpanId.fromHex(hexId, start, length, lowerCaseOnly);
} catch (NumberFormatException ignored) {
}
if (expectedId == null) {
assertNull(parsedId);
} else {
assertNotNull(parsedId);
assertEquals(expectedId.longValue(), parsedId.longValue());
}
}
@ParameterizedTest
@NullSource
@ValueSource(strings = {"", "-1", "10000000000000000", "ffffffffffffffzf", "fffffffffffffffz"})
void failOnIllegalHexString(String hexId) {
assertThrows(NumberFormatException.class, () -> DDSpanId.fromHex(hexId));
}
@ParameterizedTest
@ValueSource(strings = {"RANDOM", "SEQUENTIAL", "SECURE_RANDOM"})
void generateIdWithStrategy(String strategyName) {
IdGenerationStrategy strategy = IdGenerationStrategy.fromName(strategyName);
Set<Long> checked = new HashSet<Long>();
for (int index = 0; index <= 32768; index++) {
long spanId = strategy.generateSpanId();
assertNotEquals(0L, spanId);
assertFalse(checked.contains(spanId));
checked.add(spanId);
}
}
private static String leftPadWithZeros(String value, int size) {
if (value.length() >= size) {
return value;
}
return repeat("0", size - value.length()) + value;
}
private static String repeat(String value, int count) {
StringBuilder builder = new StringBuilder(value.length() * count);
for (int index = 0; index < count; index++) {
builder.append(value);
}
return builder.toString();
}
}