-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathIdGenerationStrategyTest.java
More file actions
120 lines (102 loc) · 3.83 KB
/
IdGenerationStrategyTest.java
File metadata and controls
120 lines (102 loc) · 3.83 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
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 java.security.SecureRandom;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.tabletest.junit.TableTest;
class IdGenerationStrategyTest {
@TableTest({
"scenario | traceId128BitGenerationEnabled | strategyName ",
"strategies-64-bit | false | {RANDOM, SEQUENTIAL, SECURE_RANDOM}",
"strategies-128-bit | true | {RANDOM, SEQUENTIAL, SECURE_RANDOM}"
})
@ParameterizedTest(name = "generate id with {1} and {0} bits")
void generateIdWithStrategyAndBitSize(
boolean traceId128BitGenerationEnabled, String strategyName) {
IdGenerationStrategy strategy =
IdGenerationStrategy.fromName(strategyName, traceId128BitGenerationEnabled);
Set<DDTraceId> checked = new HashSet<DDTraceId>();
for (int index = 0; index <= 32768; index++) {
DDTraceId traceId = strategy.generateTraceId();
assertNotNull(traceId);
assertNotEquals(null, traceId);
assertNotEquals("foo", traceId);
assertNotEquals(DDTraceId.ZERO, traceId);
int expectedHash =
(int)
(traceId.toHighOrderLong()
^ (traceId.toHighOrderLong() >>> 32)
^ traceId.toLong()
^ (traceId.toLong() >>> 32));
assertEquals(expectedHash, traceId.hashCode());
assertFalse(checked.contains(traceId));
checked.add(traceId);
}
}
@ParameterizedTest(name = "return null for non existing strategy {0}")
@ValueSource(strings = {"SOME", "UNKNOWN", "STRATEGIES"})
void returnNullForNonExistingStrategy(String strategyName) {
assertNull(IdGenerationStrategy.fromName(strategyName));
}
@Test
void exceptionCreatedOnSecureRandomStrategy() {
ExceptionInInitializerError error =
assertThrows(
ExceptionInInitializerError.class,
() ->
new IdGenerationStrategy.SRandom(
false,
() -> {
throw new IllegalArgumentException("SecureRandom init exception");
}));
assertNotNull(error.getCause());
assertEquals("SecureRandom init exception", error.getCause().getMessage());
}
@Test
void secureRandomIdsWillAlwaysBeNonZero() {
ScriptedSecureRandom random = new ScriptedSecureRandom(new long[] {0L, 47L, 0L, 11L});
CallCounter providerCallCounter = new CallCounter();
IdGenerationStrategy strategy =
new IdGenerationStrategy.SRandom(
false,
() -> {
providerCallCounter.count++;
return random;
});
long traceId = strategy.generateTraceId().toLong();
long spanId = strategy.generateSpanId();
assertEquals(1, providerCallCounter.count);
assertEquals(47L, traceId);
assertEquals(11L, spanId);
assertEquals(4, random.calls);
}
private static class CallCounter {
private int count;
}
private static class ScriptedSecureRandom extends SecureRandom {
private final long[] values;
private int index;
private int calls;
private ScriptedSecureRandom(long[] values) {
this.values = values;
}
@Override
public long nextLong() {
calls++;
if (index >= values.length) {
return 0L;
}
long value = values[index];
index++;
return value;
}
}
}