-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #498 from Kong/async429
Retries for Async Client
- Loading branch information
Showing
4 changed files
with
222 additions
and
3 deletions.
There are no files selected for viewing
174 changes: 174 additions & 0 deletions
174
unirest-bdd-tests/src/test/java/BehaviorTests/RetryAsyncTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/** | ||
* The MIT License | ||
* | ||
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package BehaviorTests; | ||
|
||
import kong.unirest.core.*; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardCopyOption; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Function; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class RetryAsyncTest extends BddTest { | ||
|
||
@Test | ||
void whenSettingIsOff() { | ||
MockServer.retryTimes(100, 429, 1d); | ||
assertEquals(429, Unirest.get(MockServer.GET).asEmpty().getStatus()); | ||
assertEquals(429, Unirest.get(MockServer.GET).asString().getStatus()); | ||
assertEquals(429, Unirest.get(MockServer.GET).asBytes().getStatus()); | ||
assertEquals(429, Unirest.get(MockServer.GET).asObject(RequestCapture.class).getStatus()); | ||
} | ||
|
||
@Test | ||
void dontRetry_whenHeaderIsMissing(){ | ||
MockServer.retryTimes(100, 429, (String)null); | ||
assertDidNotRetry(); | ||
} | ||
|
||
@Test | ||
void dontRetry_whenHeaderIsUnparseable(){ | ||
MockServer.retryTimes(100, 429, "David S Pumpkins"); | ||
assertDidNotRetry(); | ||
} | ||
|
||
@Test | ||
void dontRetry_whenHeaderIsNegative(){ | ||
MockServer.retryTimes(100, 429, -5.5); | ||
assertDidNotRetry(); | ||
} | ||
|
||
@Test | ||
void dontRetry_whenHeaderIsEmpty(){ | ||
MockServer.retryTimes(100, 429, ""); | ||
assertDidNotRetry(); | ||
} | ||
|
||
@Test | ||
void willRetryAfterSeconds_AsObject() { | ||
this.<RequestCapture>doWithRetry(r -> r.asObjectAsync(RequestCapture.class)) | ||
.assertMethod(HttpMethod.GET); | ||
} | ||
|
||
@Test | ||
void willRetryAfterSeconds_AsObjectGenerics() { | ||
List<Foo> o = Arrays.asList( | ||
new Foo("foo"), | ||
new Foo("bar"), | ||
new Foo("baz") | ||
); | ||
MockServer.setJsonAsResponse(o); | ||
List<Foo> cap = (List<Foo>)this.<RequestCapture>doWithRetry(r -> r.asObjectAsync(new GenericType<List<Foo>>(){})); | ||
assertEquals(o, cap); | ||
} | ||
|
||
@Test | ||
void willRetryAfterSeconds_AsString() { | ||
MockServer.setStringResponse("Hi Mom"); | ||
String cap = this.<String>doWithRetry(r -> r.asStringAsync()); | ||
assertEquals("Hi Mom", cap); | ||
} | ||
|
||
@Test | ||
void willRetryAfterSeconds_AsJson() { | ||
MockServer.setStringResponse("{\"message\": \"Hi Mom\"}"); | ||
JsonNode cap = this.<JsonNode>doWithRetry(r -> r.asJsonAsync()); | ||
assertEquals("Hi Mom", cap.getObject().getString("message")); | ||
} | ||
|
||
@Test | ||
void willRetryAfterSeconds_AsBytes() { | ||
MockServer.setStringResponse("Hi Mom"); | ||
byte[] cap = this.<byte[]>doWithRetry(r -> r.asBytesAsync()); | ||
assertEquals("Hi Mom", new String(cap)); | ||
} | ||
|
||
@Test | ||
void willRetryAfterSeconds_Empty() { | ||
doWithRetry(r -> r.asEmptyAsync()); | ||
} | ||
|
||
@Test | ||
void willRetryAfterSeconds_AsFile() { | ||
Path path = Paths.get("results.json"); | ||
clearFile(path); | ||
|
||
MockServer.setStringResponse("Hi Mom"); | ||
File cap = (File)this.doWithRetry(r -> r.asFileAsync(path.toString(), StandardCopyOption.REPLACE_EXISTING)); | ||
assertTrue(cap.exists()); | ||
|
||
clearFile(path); | ||
} | ||
|
||
@Test | ||
void willReturn429OnceItExceedsMaxAttempts() { | ||
MockServer.retryTimes(10, 429, .01); | ||
Unirest.config().retryAfter(true, 3); | ||
|
||
HttpResponse resp = Unirest.get(MockServer.GET).asEmpty(); | ||
assertEquals(429, resp.getStatus()); | ||
MockServer.assertRequestCount(3); | ||
} | ||
|
||
private void clearFile(Path path) { | ||
try { | ||
Files.delete(path); | ||
} catch (Exception ignored) { } | ||
} | ||
|
||
private void assertDidNotRetry() { | ||
Unirest.config().retryAfter(true); | ||
assertEquals(429, Unirest.get(MockServer.GET).asEmpty().getStatus()); | ||
MockServer.assertRequestCount(1); | ||
} | ||
|
||
private <R> R doWithRetry(Function<HttpRequest, CompletableFuture<HttpResponse<R>>> bodyExtractor) { | ||
try { | ||
MockServer.retryTimes(1, 429, 0.01); | ||
|
||
Unirest.config().retryAfter(true); | ||
|
||
HttpRequest request = Unirest.get(MockServer.GET); | ||
|
||
HttpResponse<R> response = bodyExtractor.apply(request).get(); | ||
assertEquals(200, response.getStatus()); | ||
MockServer.assertRequestCount(2); | ||
|
||
return response.getBody(); | ||
}catch (Exception e){ | ||
throw new AssertionError(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters