Fix for IpRangeAggregatorTests#testRanges

Handle null from/to ranges.

Closes #23272
This commit is contained in:
Jim Ferenczi 2017-02-20 21:16:14 +01:00
parent 4c2bd5feab
commit 1ff5b318be
1 changed files with 45 additions and 19 deletions

View File

@ -38,13 +38,11 @@ import org.elasticsearch.search.aggregations.bucket.range.ip.IpRangeAggregationB
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Comparator;
public class IpRangeAggregatorTests extends AggregatorTestCase {
private static InetAddress randomIp(boolean v4, boolean withNull) {
if (withNull && rarely()) {
return null;
}
private static InetAddress randomIp(boolean v4) {
try {
if (v4) {
byte[] ipv4 = new byte[4];
@ -60,14 +58,28 @@ public class IpRangeAggregatorTests extends AggregatorTestCase {
}
}
private static boolean isInRange(BytesRef value, BytesRef from, BytesRef to) {
if (to == null || to.compareTo(value) > 0 && (from == null || from.compareTo(value) <= 0)) {
if ((to == null || to.compareTo(value) > 0) && (from == null || from.compareTo(value) <= 0)) {
return true;
}
return false;
}
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/23272")
private static final Comparator<Tuple<BytesRef, BytesRef> > RANGE_COMPARATOR = (a, b) -> {
int cmp = compare(a.v1(), b.v1(), 1);
if (cmp == 0) {
cmp = compare(a.v2(), b.v2(), -1);
}
return cmp;
};
private static int compare(BytesRef a, BytesRef b, int m) {
return a == null
? b == null ? 0 : -m
: b == null ? m : a.compareTo(b);
}
public void testRanges() throws Exception {
boolean v4 = randomBoolean();
IpRangeAggregationBuilder builder = new IpRangeAggregationBuilder("test_agg").field("field");
@ -76,28 +88,34 @@ public class IpRangeAggregatorTests extends AggregatorTestCase {
for (int i = 0; i < numRanges; i++) {
Tuple<InetAddress, BytesRef>[] arr = new Tuple[2];
for (int j = 0; j < 2; j++) {
InetAddress addr = randomIp(v4, true);
if (addr == null) {
arr[j] = new Tuple(null, null);
} else {
arr[j] = new Tuple(addr, new BytesRef(InetAddressPoint.encode(addr)));
}
InetAddress addr = randomIp(v4);
arr[j] = new Tuple(addr, new BytesRef(InetAddressPoint.encode(addr)));
}
Arrays.sort(arr, (t1, t2) -> t1.v2().compareTo(t2.v2()));
builder.addRange(NetworkAddress.format(arr[0].v1()), NetworkAddress.format(arr[1].v1()));
requestedRanges[i] = new Tuple(arr[0].v2(), arr[1].v2());
if (rarely()) {
if (randomBoolean()) {
builder.addRange(NetworkAddress.format(arr[0].v1()), null);
requestedRanges[i] = new Tuple(arr[0].v2(), null);
} else {
builder.addRange(null, NetworkAddress.format(arr[1].v1()));
requestedRanges[i] = new Tuple(null, arr[1].v2());
}
} else {
builder.addRange(NetworkAddress.format(arr[0].v1()), NetworkAddress.format(arr[1].v1()));
requestedRanges[i] = new Tuple(arr[0].v2(), arr[1].v2());
}
}
Arrays.sort(requestedRanges, (t1, t2) -> t1.v1().compareTo(t2.v1()));
Arrays.sort(requestedRanges, RANGE_COMPARATOR);
int[] expectedCounts = new int[numRanges];
try (Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
int numDocs = randomIntBetween(10, 100);
for (int i = 0; i < numDocs; i++) {
Document doc = new Document();
int numValues = randomIntBetween(1, 5);
BytesRef[] values = new BytesRef[numValues];
for (int j = 0; j < numValues; j++) {
values[j] = new BytesRef(InetAddressPoint.encode(randomIp(v4, false)));
values[j] = new BytesRef(InetAddressPoint.encode(randomIp(v4)));
doc.add(new SortedSetDocValuesField("field", values[j]));
}
Arrays.sort(values);
@ -120,8 +138,16 @@ public class IpRangeAggregatorTests extends AggregatorTestCase {
for (int i = 0; i < range.getBuckets().size(); i++) {
Tuple<BytesRef, BytesRef> expected = requestedRanges[i];
Range.Bucket bucket = range.getBuckets().get(i);
assertEquals(DocValueFormat.IP.format(expected.v1()), bucket.getFrom());
assertEquals(DocValueFormat.IP.format(expected.v2()), bucket.getTo());
if (expected.v1() == null) {
assertNull(bucket.getFrom());
} else {
assertEquals(DocValueFormat.IP.format(expected.v1()), bucket.getFrom());
}
if (expected.v2() == null) {
assertNull(bucket.getTo());
} else {
assertEquals(DocValueFormat.IP.format(expected.v2()), bucket.getTo());
}
assertEquals(expectedCounts[i], bucket.getDocCount());
}
}