|
31 | 31 | import static org.junit.jupiter.api.Assertions.assertNull; |
32 | 32 | import static org.junit.jupiter.api.Assertions.assertThrows; |
33 | 33 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 34 | +import static org.junit.jupiter.api.Assertions.fail; |
34 | 35 | import static org.junit.jupiter.api.Assumptions.assumeTrue; |
35 | 36 |
|
36 | 37 | import java.io.File; |
@@ -257,5 +258,72 @@ public void testGetActiveNonListenerOMNodeIdsFiltering() { |
257 | 258 | assertEquals(expected.size(), result.size()); |
258 | 259 | assertTrue(result.containsAll(expected)); |
259 | 260 | } |
260 | | -} |
261 | 261 |
|
| 262 | + @Test |
| 263 | + void testGetOMEpoch() { |
| 264 | + assertEquals(2, OmUtils.getOMEpoch()); |
| 265 | + assertEquals(OmUtils.EPOCH_WHEN_RATIS_ENABLED, OmUtils.getOMEpoch()); |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + void testAddEpochToTxId() { |
| 270 | + assertEquals(0L, OmUtils.addEpochToTxId(0, 0)); |
| 271 | + assertEquals(1L << 62, OmUtils.addEpochToTxId(1, 0)); |
| 272 | + assertEquals(2L << 62, OmUtils.addEpochToTxId(2, 0)); |
| 273 | + assertEquals(3L << 62, OmUtils.addEpochToTxId(3, 0)); |
| 274 | + |
| 275 | + long txId = 12345L; |
| 276 | + long expected = (2L << 62) | (txId << 8); |
| 277 | + assertEquals(expected, OmUtils.addEpochToTxId(2, txId)); |
| 278 | + |
| 279 | + long maxTxId = OmUtils.MAX_TRXN_ID; |
| 280 | + long maxExpected = (2L << 62) | (maxTxId << 8); |
| 281 | + assertEquals(maxExpected, OmUtils.addEpochToTxId(2, maxTxId)); |
| 282 | + |
| 283 | + // Verify bit structure |
| 284 | + long result = OmUtils.addEpochToTxId(2, 0x123456789ABCDL); |
| 285 | + assertEquals(2L, result >>> 62); |
| 286 | + assertEquals(0x123456789ABCDL, (result & 0x3FFFFFFFFFFFFFFFL) >>> 8); |
| 287 | + } |
| 288 | + |
| 289 | + // Intentionally no tests for getTxIdFromObjectId(); this helper is not |
| 290 | + // used in production paths and may be removed in the future. |
| 291 | + |
| 292 | + @Test |
| 293 | + void testGetObjectIdFromTxId() { |
| 294 | + long txId = 12345L; |
| 295 | + long epoch = 2L; |
| 296 | + long expected = OmUtils.addEpochToTxId(epoch, txId); |
| 297 | + assertEquals(expected, OmUtils.getObjectIdFromTxId(epoch, txId)); |
| 298 | + |
| 299 | + for (long e = 0; e <= 3; e++) { |
| 300 | + long result = OmUtils.getObjectIdFromTxId(e, txId); |
| 301 | + assertEquals(e, result >>> 62); |
| 302 | + assertEquals(txId, (result & 0x3FFFFFFFFFFFFFFFL) >>> 8); |
| 303 | + } |
| 304 | + |
| 305 | + long maxTxId = OmUtils.MAX_TRXN_ID; |
| 306 | + long maxResult = OmUtils.getObjectIdFromTxId(epoch, maxTxId); |
| 307 | + assertEquals(epoch, maxResult >>> 62); |
| 308 | + assertEquals(maxTxId, (maxResult & 0x3FFFFFFFFFFFFFFFL) >>> 8); |
| 309 | + } |
| 310 | + |
| 311 | + @Test |
| 312 | + void testGetObjectIdFromTxIdValidation() { |
| 313 | + long validTxId = OmUtils.MAX_TRXN_ID; |
| 314 | + // Test valid case - should not throw exception |
| 315 | + try { |
| 316 | + OmUtils.getObjectIdFromTxId(2, validTxId); |
| 317 | + } catch (Exception e) { |
| 318 | + fail("Valid txId should not throw exception: " + e.getMessage()); |
| 319 | + } |
| 320 | + |
| 321 | + long invalidTxId = (1L << 54) - 1; // MAX_TRXN_ID + 1 |
| 322 | + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, |
| 323 | + () -> OmUtils.getObjectIdFromTxId(2, invalidTxId)); |
| 324 | + assertTrue(exception.getMessage().contains("TransactionID exceeds max limit")); |
| 325 | + } |
| 326 | + |
| 327 | + // Consistency checks between epoch and txId are covered by |
| 328 | + // testAddEpochToTxId() and testGetObjectIdFromTxId(). |
| 329 | +} |
0 commit comments