|
| 1 | +package io.cucumber.gherkin; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import java.util.concurrent.atomic.AtomicInteger; |
| 6 | + |
| 7 | +import static org.junit.jupiter.api.Assertions.*; |
| 8 | + |
| 9 | +class LocationsTest { |
| 10 | + |
| 11 | + @Test |
| 12 | + void getLong() { |
| 13 | + // random integer conversion that is far after the cache upper bound works |
| 14 | + assertEquals(Long.valueOf(12000L), Locations.getLong(12000)); |
| 15 | + |
| 16 | + // sequential integer conversion works (the cache has no hole) |
| 17 | + for (int i=0; i<12000; i++) { |
| 18 | + Long expectedLong = (long)i; |
| 19 | + assertEquals(expectedLong, Locations.getLong(i)); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + void getLong_negative_number_not_supported() { |
| 25 | + assertThrows(ArrayIndexOutOfBoundsException.class, () -> Locations.getLong(-12000)); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void getLong_multithread_does_not_raise_exception() { |
| 30 | + // Given |
| 31 | + AtomicInteger successCounter = new AtomicInteger(0); |
| 32 | + int numberOfThreads = 1000; |
| 33 | + int numberOfIterations = 20000; |
| 34 | + Thread[] threads = new Thread[numberOfThreads]; |
| 35 | + for (int i = 0; i < numberOfThreads; i++) { |
| 36 | + threads[i] = new Thread(() -> { |
| 37 | + for (int j = 500; j < numberOfIterations; j *= 2) { |
| 38 | + Long expectedLong = (long)j; |
| 39 | + assertEquals(expectedLong, Locations.getLong(j)); |
| 40 | + } |
| 41 | + successCounter.incrementAndGet(); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + // When |
| 46 | + for (Thread thread : threads) { |
| 47 | + thread.start(); |
| 48 | + } |
| 49 | + |
| 50 | + // Then |
| 51 | + for (Thread thread : threads) { |
| 52 | + assertDoesNotThrow(() -> thread.join()); |
| 53 | + } |
| 54 | + assertEquals(numberOfThreads, successCounter.get()); |
| 55 | + } |
| 56 | + |
| 57 | +} |
0 commit comments