[TEST] Fixes PageParamsTests to no underflow from and size

In `mutateInstance()` the from or size could become negative if the other one was pushed over the limit for `from + size`. This change fixes this case to make sure after the mutate method is called the from and size obey the limit but are also both `>= 0`

relates elastic/x-pack-elasticsearch#2344

Original commit: elastic/x-pack-elasticsearch@a8a7072fcc
This commit is contained in:
Colin Goodheart-Smithe 2017-08-24 09:29:36 +01:00
parent f05568e7b3
commit d0103d6cab
1 changed files with 19 additions and 6 deletions

View File

@ -60,21 +60,34 @@ public class PageParamsTests extends AbstractSerializingTestCase<PageParams> {
protected PageParams mutateInstance(PageParams instance) throws IOException {
int from = instance.getFrom();
int size = instance.getSize();
int amountToAdd = between(1, 20);
switch (between(0, 1)) {
case 0:
from += between(1, 20);
from += amountToAdd;
// If we have gone above the limit for max and size then we need to
// change size too
// adjust from and size to be valid
if ((from + size) > PageParams.MAX_FROM_SIZE_SUM) {
size = PageParams.MAX_FROM_SIZE_SUM - from;
if (from >= 2 * amountToAdd) {
// If from is large enough then just subtract the amount we added twice
from -= 2 * amountToAdd;
} else {
// Otherwise change size to obey the limit
size = PageParams.MAX_FROM_SIZE_SUM - from;
}
}
break;
case 1:
size += between(1, 20);
size += amountToAdd;
// If we have gone above the limit for max and size then we need to
// change from too
// adjust from and size to be valid
if ((from + size) > PageParams.MAX_FROM_SIZE_SUM) {
from = PageParams.MAX_FROM_SIZE_SUM - size;
if (size >= 2 * amountToAdd) {
// If from is large enough then just subtract the amount we added twice
size -= 2 * amountToAdd;
} else {
// Otherwise change size to obey the limit
from = PageParams.MAX_FROM_SIZE_SUM - size;
}
}
break;
default: