|
5 | 5 | import org.junit.jupiter.api.Test;
|
6 | 6 |
|
7 | 7 | import java.io.IOException;
|
| 8 | +import java.lang.reflect.Field; |
| 9 | +import java.lang.reflect.InvocationTargetException; |
| 10 | +import java.lang.reflect.Method; |
8 | 11 | import java.net.URISyntaxException;
|
9 | 12 | import java.nio.file.Files;
|
10 | 13 | import java.nio.file.Path;
|
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.HashSet; |
11 | 16 | import java.util.List;
|
12 | 17 | import java.util.Objects;
|
| 18 | +import java.util.Set; |
13 | 19 | import java.util.UUID;
|
14 | 20 | import java.util.stream.Collectors;
|
15 | 21 | import java.util.stream.IntStream;
|
16 | 22 |
|
17 | 23 | import static org.hamcrest.MatcherAssert.assertThat;
|
18 | 24 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 25 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
19 | 26 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
20 | 27 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
21 | 28 |
|
22 | 29 | class IncrementingUuidGeneratorTest {
|
23 | 30 |
|
| 31 | + public static final String CLASSLOADER_ID_FIELD_NAME = "classloaderId"; |
| 32 | + |
24 | 33 | /**
|
25 | 34 | * Example of generated values (same epochTime, same sessionId, same
|
26 | 35 | * classloaderId, different counter value):
|
@@ -92,7 +101,7 @@ void same_thread_generates_different_UuidGenerators() {
|
92 | 101 | void different_classloaders_generators() {
|
93 | 102 | // Given/When
|
94 | 103 | List<UUID> uuids = IntStream.rangeClosed(1, 10)
|
95 |
| - .mapToObj(i -> getUuidGeneratorFromOtherClassloader().generateId()) |
| 104 | + .mapToObj(i -> getUuidGeneratorFromOtherClassloader(i).generateId()) |
96 | 105 | .collect(Collectors.toList());
|
97 | 106 |
|
98 | 107 | // Then
|
@@ -160,12 +169,131 @@ private static UUID removeClassloaderId(UUID uuid) {
|
160 | 169 | return new UUID(uuid.getMostSignificantBits() & 0xfffffffffffff000L, uuid.getLeastSignificantBits());
|
161 | 170 | }
|
162 | 171 |
|
163 |
| - private static UuidGenerator getUuidGeneratorFromOtherClassloader() { |
| 172 | + /** |
| 173 | + * Create a copy of the UUID without the epoch-time part to allow |
| 174 | + * comparison. |
| 175 | + */ |
| 176 | + private static UUID removeEpochTime(UUID uuid) { |
| 177 | + return new UUID(uuid.getMostSignificantBits() & 0x0ffffffL, uuid.getLeastSignificantBits()); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Check that classloaderId collision rate is lower than a given threshold |
| 182 | + * when using multiple classloaders. This should not be mistaken with the |
| 183 | + * UUID collision rate. Note: this test takes about 20 seconds. |
| 184 | + */ |
| 185 | + @Test |
| 186 | + void classloaderid_collision_rate_lower_than_two_percents_with_ten_classloaders() |
| 187 | + throws NoSuchFieldException, IllegalAccessException { |
| 188 | + double collisionRateWhenUsingTenClassloaders; |
| 189 | + List<Double> collisionRatesWhenUsingTenClassloaders = new ArrayList<>(); |
| 190 | + do { |
| 191 | + // When I compute the classloaderId collision rate with multiple |
| 192 | + // classloaders |
| 193 | + Set<Long> classloaderIds = new HashSet<>(); |
| 194 | + List<Integer> stats = new ArrayList<>(); |
| 195 | + while (stats.size() < 100) { |
| 196 | + if (!classloaderIds |
| 197 | + .add(getStaticFieldValue(getUuidGeneratorFromOtherClassloader(null), |
| 198 | + CLASSLOADER_ID_FIELD_NAME))) { |
| 199 | + stats.add(classloaderIds.size() + 1); |
| 200 | + classloaderIds.clear(); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + // Then the classloaderId collision rate for 10 classloaders is less |
| 205 | + // than 2% |
| 206 | + collisionRateWhenUsingTenClassloaders = stats.stream() |
| 207 | + .filter(x -> x < 10).count() * 100 / (double) stats.size(); |
| 208 | + collisionRatesWhenUsingTenClassloaders.add(collisionRateWhenUsingTenClassloaders); |
| 209 | + } while (collisionRateWhenUsingTenClassloaders > 2 && collisionRatesWhenUsingTenClassloaders.size() < 10); |
| 210 | + assertTrue(collisionRateWhenUsingTenClassloaders <= 2, |
| 211 | + "all retries exceed the expected collision rate : " + collisionRatesWhenUsingTenClassloaders); |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + void same_classloaderId_leads_to_same_uuid_when_ignoring_epoch_time() { |
| 216 | + // Given the two generator have the same classloaderId |
| 217 | + UuidGenerator generator1 = getUuidGeneratorFromOtherClassloader(255); |
| 218 | + UuidGenerator generator2 = getUuidGeneratorFromOtherClassloader(255); |
| 219 | + |
| 220 | + // When the UUID are generated |
| 221 | + UUID uuid1 = generator1.generateId(); |
| 222 | + UUID uuid2 = generator2.generateId(); |
| 223 | + |
| 224 | + // Then the UUID are the same |
| 225 | + assertEquals(removeEpochTime(uuid1), removeEpochTime(uuid2)); |
| 226 | + } |
| 227 | + |
| 228 | + @Test |
| 229 | + void different_classloaderId_leads_to_different_uuid_when_ignoring_epoch_time() { |
| 230 | + // Given the two generator have the different classloaderId |
| 231 | + UuidGenerator generator1 = getUuidGeneratorFromOtherClassloader(1); |
| 232 | + UuidGenerator generator2 = getUuidGeneratorFromOtherClassloader(2); |
| 233 | + |
| 234 | + // When the UUID are generated |
| 235 | + UUID uuid1 = generator1.generateId(); |
| 236 | + UUID uuid2 = generator2.generateId(); |
| 237 | + |
| 238 | + // Then the UUID are the same |
| 239 | + assertNotEquals(removeEpochTime(uuid1), removeEpochTime(uuid2)); |
| 240 | + } |
| 241 | + |
| 242 | + @Test |
| 243 | + void setClassloaderId_keeps_only_12_bits() throws NoSuchFieldException, IllegalAccessException { |
| 244 | + // When the classloaderId is defined with a value higher than 0xfff (12 |
| 245 | + // bits) |
| 246 | + IncrementingUuidGenerator.setClassloaderId(0xfffffABC); |
| 247 | + |
| 248 | + // Then the classloaderId is truncated to 12 bits |
| 249 | + assertEquals(0x0ABC, getStaticFieldValue(new IncrementingUuidGenerator(), CLASSLOADER_ID_FIELD_NAME)); |
| 250 | + } |
| 251 | + |
| 252 | + @Test |
| 253 | + void setClassloaderId_keeps_values_under_12_bits_unmodified() throws NoSuchFieldException, IllegalAccessException { |
| 254 | + // When the classloaderId is defined with a value lower than 0xfff (12 |
| 255 | + // bits) |
| 256 | + IncrementingUuidGenerator.setClassloaderId(0x0123); |
| 257 | + |
| 258 | + // Then the classloaderId value is left unmodified |
| 259 | + assertEquals(0x0123, getStaticFieldValue(new IncrementingUuidGenerator(), CLASSLOADER_ID_FIELD_NAME)); |
| 260 | + } |
| 261 | + |
| 262 | + private Long getStaticFieldValue(UuidGenerator generator, String fieldName) |
| 263 | + throws NoSuchFieldException, IllegalAccessException { |
| 264 | + // The Field cannot be cached because the IncrementingUuidGenerator |
| 265 | + // class is different at each call (because it was loaded by a |
| 266 | + // different classloader). |
| 267 | + Field declaredField = generator.getClass().getDeclaredField(fieldName); |
| 268 | + declaredField.setAccessible(true); |
| 269 | + return (Long) declaredField.get(null); |
| 270 | + } |
| 271 | + |
| 272 | + private static void setClassloaderId(Class<?> generatorClass, int value) |
| 273 | + throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { |
| 274 | + // The Method cannot be cached because the IncrementingUuidGenerator |
| 275 | + // class is different at each call (because it was loaded by a |
| 276 | + // different classloader). |
| 277 | + Method method = generatorClass.getDeclaredMethod("setClassloaderId", int.class); |
| 278 | + method.setAccessible(true); |
| 279 | + method.invoke(null, value); |
| 280 | + } |
| 281 | + |
| 282 | + /** |
| 283 | + * Create a fresh new IncrementingUuidGenerator from a fresh new |
| 284 | + * classloader, and return a new instance. |
| 285 | + * |
| 286 | + * @param classloaderId the classloader unique identifier, or null if the |
| 287 | + * default classloader id generator must be used |
| 288 | + * @return a new IncrementingUuidGenerator instance |
| 289 | + */ |
| 290 | + private static UuidGenerator getUuidGeneratorFromOtherClassloader(Integer classloaderId) { |
164 | 291 | try {
|
165 |
| - return (UuidGenerator) (new NonCachingClassLoader() |
166 |
| - .findClass(IncrementingUuidGenerator.class.getName()) |
167 |
| - .getConstructor() |
168 |
| - .newInstance()); |
| 292 | + Class<?> aClass = new NonCachingClassLoader().findClass(IncrementingUuidGenerator.class.getName()); |
| 293 | + if (classloaderId != null) { |
| 294 | + setClassloaderId(aClass, classloaderId); |
| 295 | + } |
| 296 | + return (UuidGenerator) aClass.getConstructor().newInstance(); |
169 | 297 | } catch (Exception e) {
|
170 | 298 | throw new RuntimeException("could not instantiate " + IncrementingUuidGenerator.class.getSimpleName(), e);
|
171 | 299 | }
|
|
0 commit comments