|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.polaris.service.tracing; |
| 21 | + |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 24 | + |
| 25 | +import java.util.HashSet; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.UUID; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +public class RequestIdGeneratorTest { |
| 32 | + |
| 33 | + private RequestIdGenerator requestIdGenerator; |
| 34 | + |
| 35 | + @BeforeEach |
| 36 | + void setUp() { |
| 37 | + requestIdGenerator = new RequestIdGenerator(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void testGenerateRequestId_ReturnsValidFormat() { |
| 42 | + String requestId = requestIdGenerator.generateRequestId(); |
| 43 | + |
| 44 | + assertThat(requestId).isNotNull(); |
| 45 | + assertThat(requestId).matches(this::isValidRequestIdFormat); |
| 46 | + assertThat(requestId).contains("_1"); // First call should increment counter to 1 |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void testGenerateRequestId_ReturnsUniqueIds() { |
| 51 | + Set<String> generatedIds = new HashSet<>(); |
| 52 | + |
| 53 | + // Generate multiple request IDs and verify they're all unique |
| 54 | + for (int i = 0; i < 1000; i++) { |
| 55 | + String requestId = requestIdGenerator.generateRequestId(); |
| 56 | + assertThat(generatedIds).doesNotContain(requestId); |
| 57 | + generatedIds.add(requestId); |
| 58 | + } |
| 59 | + |
| 60 | + assertThat(generatedIds).hasSize(1000); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testCounterIncrementsSequentially() { |
| 65 | + requestIdGenerator.setCounter(0); |
| 66 | + |
| 67 | + String firstId = requestIdGenerator.generateRequestId(); |
| 68 | + String secondId = requestIdGenerator.generateRequestId(); |
| 69 | + String thirdId = requestIdGenerator.generateRequestId(); |
| 70 | + |
| 71 | + assertThat(firstId).endsWith("_1"); |
| 72 | + assertThat(secondId).endsWith("_2"); |
| 73 | + assertThat(thirdId).endsWith("_3"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testCounterRotationAtSoftMax() { |
| 78 | + // Set counter close to soft max |
| 79 | + long softMax = RequestIdGenerator.COUNTER_SOFT_MAX; |
| 80 | + requestIdGenerator.setCounter(softMax - 1); |
| 81 | + |
| 82 | + String beforeRotation = requestIdGenerator.generateRequestId(); |
| 83 | + String afterRotation = requestIdGenerator.generateRequestId(); |
| 84 | + |
| 85 | + // The UUID part should be different after rotation |
| 86 | + String beforeUuidPart = beforeRotation.substring(0, beforeRotation.lastIndexOf('_')); |
| 87 | + String afterUuidPart = afterRotation.substring(0, afterRotation.lastIndexOf('_')); |
| 88 | + assertNotEquals(beforeUuidPart, afterUuidPart); |
| 89 | + |
| 90 | + // After rotation, counter should be reset and UUID should be different |
| 91 | + assertThat(beforeRotation).endsWith("_" + softMax); |
| 92 | + assertThat(afterRotation).endsWith("_1"); // Counter reset to 1 (after increment from 0) |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void testSetCounterChangesNextGeneratedId() { |
| 97 | + requestIdGenerator.setCounter(100); |
| 98 | + |
| 99 | + String requestId = requestIdGenerator.generateRequestId(); |
| 100 | + |
| 101 | + assertThat(requestId).endsWith("_101"); // Should increment from set value |
| 102 | + } |
| 103 | + |
| 104 | + private boolean isValidRequestIdFormat(String str) { |
| 105 | + try { |
| 106 | + String[] requestIdParts = str.split("_"); |
| 107 | + String uuid = requestIdParts[0]; |
| 108 | + String counter = requestIdParts[1]; |
| 109 | + UUID.fromString(uuid); |
| 110 | + Long.parseLong(counter); |
| 111 | + return true; |
| 112 | + } catch (IllegalArgumentException e) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments