Skip to content

Commit ae16bae

Browse files
U117293U117293
U117293
authored and
U117293
committed
feat: added tests for #385
1 parent 14dff68 commit ae16bae

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

Diff for: java/src/main/java/io/cucumber/gherkin/Locations.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ class Locations {
1212
* We can't use Long.valueOf() because it caches only the first 128
1313
* values, and typical feature files have much more lines.
1414
*/
15-
private final static Long[] longs = new Long[4000];
15+
private static final Long[] longs = new Long[4096];
1616
static {
1717
for (int i = 0; i < longs.length; i++) {
1818
longs[i] = (long) i;
1919
}
2020
}
2121

22-
private static Long getLong(int i) {
22+
static Long getLong(int i) {
2323
if (i>=longs.length) return (long) i;
2424
return longs[i];
2525
}
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)