Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,24 @@ class KeepingToARateCommandTest {
get: {operationId: listOwners, responses: {'200': {description: every owner}}}
""";

/**
* The time a run kept to the rate is first given, which pays for starting up as well as for
* sending: on a machine at ease, room for well over a dozen turns once it has started.
*/
private static final Duration BUDGET = Duration.ofSeconds(2);

/**
* The most time a run kept to the rate is given. A run that had too little time left once it
* had started up is run again with twice as much, up to this.
*/
private static final Duration LONGEST = BUDGET.multipliedBy(4);

/**
* The time given to a run that its number of requests ends: only the ceiling a run that went
* wrong could not get past, as for the runs {@link ARun} starts.
*/
private static final Duration CEILING = Duration.ofSeconds(30);

/**
* Ends a run with no rate after this many requests rather than at its budget: enough to show
* it went far faster than a rate would let it, without loading a small machine for the whole
Expand Down Expand Up @@ -106,10 +122,21 @@ void answerEverything() {
@DisplayName("a rate named in one line is kept to: never two requests closer than a turn, none "
+ "after the run's time, and the quiet it made is said apart from the tool's own")
void a_rate_named_in_one_line_is_kept_to() throws IOException {
int answer = run("--set", "engine.mostRequestsPerMinute=600", "--store");
// The budget pays for reading the document and starting up as well as for sending, and a
// busy Windows runner once spent four and a half seconds starting up, sending nothing in
// a budget of two. A run with no room for three turns between its first request and the
// end of its time says nothing about the rate either way, so it is run again with twice
// the time: a busy machine costs this test time, never its verdict, and everything below
// is checked on a run that had room.
Duration budget = BUDGET;
int answer = keepingToTheRate(budget);
while (turnsLeft(budget) < 3 && budget.compareTo(LONGEST) < 0) {
budget = budget.multipliedBy(2);
answer = keepingToTheRate(budget);
}

assertThat(answer).describedAs("%s%s", screen, problems).isZero();
assertThat(arrivals()).hasSizeBetween(3, (int) BUDGET.dividedBy(BETWEEN) + 2);
assertThat(arrivals()).hasSizeBetween(3, (int) budget.dividedBy(BETWEEN) + 2);
// Where RESTest sent them, which is where the rate is kept: at the API the first arrives
// late, behind the connection it opens, and the next, sent a turn later, does not.
List<Instant> sent = sentAt();
Expand All @@ -119,9 +146,7 @@ void a_rate_named_in_one_line_is_kept_to() throws IOException {
+ "between giving a request its turn and noting the moment it went")
.isGreaterThan(BETWEEN.dividedBy(2));
}
JsonValue.JsonObject timeline =
(JsonValue.JsonObject) ARun.report(out()).member("timeline").orElseThrow();
Instant timeIsUp = Instant.parse(text(timeline, "startedAt")).plus(BUDGET);
Instant timeIsUp = timeIsUp(budget);
// A request whose turn came just before the time was up may still leave a little after
// it, as late as a busy machine is in building it and sending it: a Windows runner once took
// 55 ms. One still waiting for its turn is dropped; kept, it would go a turn or more late.
Expand All @@ -136,7 +161,7 @@ void a_rate_named_in_one_line_is_kept_to() throws IOException {
@DisplayName("with no rate named, the run goes as fast as the API answers and says nothing of a "
+ "rate")
void no_rate_no_limit() throws IOException {
int answer = run(A_FEW_HUNDRED.toArray(String[]::new));
int answer = run(CEILING, A_FEW_HUNDRED.toArray(String[]::new));

assertThat(answer).describedAs("%s%s", screen, problems).isZero();
assertFasterThanTheRate(arrivals());
Expand All @@ -155,7 +180,7 @@ void a_rate_over_a_file_with_the_switch_off_is_said() throws IOException {
List<String> arguments = new ArrayList<>(List.of("--settings", printed.toString(),
"--set", "engine.mostRequestsPerMinute=600"));
arguments.addAll(A_FEW_HUNDRED);
int answer = run(arguments.toArray(String[]::new));
int answer = run(CEILING, arguments.toArray(String[]::new));

assertThat(answer).describedAs("%s%s", screen, problems).isZero();
assertThat(screen.toString()).contains(" engine.mostRequestsPerMinute is 600, but "
Expand All @@ -168,13 +193,38 @@ private static void assertFasterThanTheRate(List<Instant> arrivals) {
assertThat(arrivals).hasSize(200);
assertThat(Duration.between(arrivals.getFirst(), arrivals.getLast()))
.describedAs("at ten a second, two hundred requests take twenty seconds")
.isLessThan(BUDGET);
.isLessThan(BETWEEN.multipliedBy(20));
}

/** A run kept to ten requests a second for this long, the only one the stand-in has seen. */
private int keepingToTheRate(Duration budget) throws IOException {
api.resetRequests();
screen.getBuffer().setLength(0);
problems.getBuffer().setLength(0);
return run(budget, "--set", "engine.mostRequestsPerMinute=600", "--store");
}

/**
* How many turns of the rate the last run had between its first request and the end of its
* time; none when it sent nothing.
*/
private long turnsLeft(Duration budget) {
List<Instant> sent = sentAt();
return sent.isEmpty() ? 0
: Duration.between(sent.getFirst(), timeIsUp(budget)).dividedBy(BETWEEN);
}

/** When the last run's time was up: the moment it says it started, and its budget after. */
private Instant timeIsUp(Duration budget) {
JsonValue.JsonObject timeline =
(JsonValue.JsonObject) ARun.report(out()).member("timeline").orElseThrow();
return Instant.parse(text(timeline, "startedAt")).plus(budget);
}

private int run(String... more) throws IOException {
private int run(Duration budget, String... more) throws IOException {
Path file = Files.writeString(directory.resolve("openapi.yaml"), SHELTER);
List<String> arguments = new ArrayList<>(List.of("run", file.toString(), "--url",
api.baseUrl(), "--budget", BUDGET.toMillis() + "ms", "--seed", "1", "--out",
api.baseUrl(), "--budget", budget.toMillis() + "ms", "--seed", "1", "--out",
out().toString()));
arguments.addAll(List.of(more));
PrintWriter outWriter = new PrintWriter(screen);
Expand Down
31 changes: 29 additions & 2 deletions restest-cli/src/test/java/io/restest/cli/RunLoopTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -934,14 +934,22 @@ void a_slow_401_to_the_lap_stops_the_run_when_it_arrives() {
long began = System.nanoTime();
RunLoop.Outcome outcome = run(Duration.ofSeconds(30), withPatience(patience),
listening -> { });
Duration took = Duration.ofNanos(System.nanoTime() - began);
long ended = System.nanoTime();

assertThat(outcome.refusedLap())
.describedAs("waiting %s for each step: %s", patience, outcome).isPresent();
assertThat(outcome.sent())
.describedAs("the premise: it went on sending while it waited for the answer")
.isGreaterThan(model.operations().size());
assertThat(took)
// From the 401, not from the start of the run. Once it is in, the run waits only for
// the lists of shelters it sent before it, the last answered at most two seconds later
// and usually within a fraction of one; a slot not given back would hold it for the
// whole wait for stragglers. Measured from the start, the bound also had to cover the
// two seconds the answer takes and however long the run took to get going, and a
// Windows runner once needed more than what that left.
Duration afterThe401 =
Duration.ofNanos(ended - engine.firstAnsweredSince("listShelters", began));
assertThat(afterThe401)
.describedAs("it stopped once the answer was in, and waited for nothing it "
+ "was not owed")
.isLessThan(PATIENT.minusSeconds(1));
Expand Down Expand Up @@ -1346,6 +1354,10 @@ private static final class ApiEngine implements HttpEngine {
private final java.util.Queue<Map.Entry<String, Long>> everySend =
new java.util.concurrent.ConcurrentLinkedQueue<>();

/** Every request answered, by operation and by the monotonic clock, in order. */
private final java.util.Queue<Map.Entry<String, Long>> everyAnswer =
new java.util.concurrent.ConcurrentLinkedQueue<>();

/** Every hold the loop asked for. */
private final List<Duration> holds = new java.util.concurrent.CopyOnWriteArrayList<>();

Expand Down Expand Up @@ -1420,6 +1432,20 @@ void answersWithHeaders(Function<TestCase, List<Header>> perTestCase) {
this.headers = perTestCase;
}

/**
* When a request for this operation was first answered at or after the given moment, by
* the monotonic clock: the first answer of a run that began then, when one engine serves
* several runs one after the other.
*/
long firstAnsweredSince(String operation, long since) {
return everyAnswer.stream()
.filter(answer -> answer.getKey().equals(operation))
.mapToLong(Map.Entry::getValue)
.filter(at -> at >= since)
.min()
.orElseThrow();
}

/** How many requests were asked for this operation. */
long askedFor(String operation) {
return everySend.stream().filter(send -> send.getKey().equals(operation)).count();
Expand Down Expand Up @@ -1467,6 +1493,7 @@ public CompletableFuture<Interaction> sendAsync(TestCase testCase,
return;
}
firstAnswered.putIfAbsent(operation, System.nanoTime());
everyAnswer.add(Map.entry(operation, System.nanoTime()));
Interaction replied = reply(testCase, request, sentAt);
if (testCase.sequence().isPresent()) {
answeredAt.put(testCase, System.nanoTime());
Expand Down
Loading