Skip to content

Commit f6d6ec3

Browse files
committed
async resilience tests
1 parent 2129508 commit f6d6ec3

File tree

7 files changed

+383
-38
lines changed

7 files changed

+383
-38
lines changed

resilience-tests/src/test/java/resilience/connection/ConnectionTest.java

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package resilience.connection;
22

3-
import com.arangodb.ArangoDB;
4-
import com.arangodb.ArangoDBException;
5-
import com.arangodb.ArangoDBMultipleException;
6-
import com.arangodb.Protocol;
3+
import com.arangodb.*;
74
import resilience.SingleServerTest;
85
import org.junit.jupiter.params.ParameterizedTest;
96
import org.junit.jupiter.params.provider.MethodSource;
@@ -36,9 +33,13 @@ static Stream<ArangoDB> arangoProvider() {
3633
);
3734
}
3835

36+
static Stream<ArangoDBAsync> asyncArangoProvider() {
37+
return arangoProvider().map(ArangoDB::async);
38+
}
39+
3940
@ParameterizedTest
4041
@MethodSource("protocolProvider")
41-
void nameResolutionFailTest(Protocol protocol) {
42+
void nameResolutionFail(Protocol protocol) {
4243
ArangoDB arangoDB = new ArangoDB.Builder()
4344
.host("wrongHost", 8529)
4445
.protocol(protocol)
@@ -57,8 +58,29 @@ void nameResolutionFailTest(Protocol protocol) {
5758
}
5859

5960
@ParameterizedTest
61+
@MethodSource("protocolProvider")
62+
void nameResolutionFailAsync(Protocol protocol) {
63+
ArangoDBAsync arangoDB = new ArangoDB.Builder()
64+
.host("wrongHost", 8529)
65+
.protocol(protocol)
66+
.build()
67+
.async();
68+
69+
Throwable thrown = catchThrowable(() -> arangoDB.getVersion().get()).getCause();
70+
assertThat(thrown).isInstanceOf(ArangoDBException.class);
71+
assertThat(thrown.getMessage()).contains("Cannot contact any host!");
72+
assertThat(thrown.getCause()).isNotNull();
73+
assertThat(thrown.getCause()).isInstanceOf(ArangoDBMultipleException.class);
74+
((ArangoDBMultipleException) thrown.getCause()).getExceptions().forEach(e -> {
75+
assertThat(e).isInstanceOf(UnknownHostException.class);
76+
assertThat(e.getMessage()).contains("wrongHost");
77+
});
78+
arangoDB.shutdown();
79+
}
80+
81+
@ParameterizedTest(name = "{index}")
6082
@MethodSource("arangoProvider")
61-
void connectionFailTest(ArangoDB arangoDB) {
83+
void connectionFail(ArangoDB arangoDB) {
6284
disableEndpoint();
6385

6486
Throwable thrown = catchThrowable(arangoDB::getVersion);
@@ -72,4 +94,20 @@ void connectionFailTest(ArangoDB arangoDB) {
7294
enableEndpoint();
7395
}
7496

97+
@ParameterizedTest(name = "{index}")
98+
@MethodSource("asyncArangoProvider")
99+
void connectionFailAsync(ArangoDBAsync arangoDB) {
100+
disableEndpoint();
101+
102+
Throwable thrown = catchThrowable(() -> arangoDB.getVersion().get()).getCause();
103+
assertThat(thrown).isInstanceOf(ArangoDBException.class);
104+
assertThat(thrown.getMessage()).contains("Cannot contact any host");
105+
assertThat(thrown.getCause()).isNotNull();
106+
assertThat(thrown.getCause()).isInstanceOf(ArangoDBMultipleException.class);
107+
((ArangoDBMultipleException) thrown.getCause()).getExceptions().forEach(e ->
108+
assertThat(e).isInstanceOf(ConnectException.class));
109+
arangoDB.shutdown();
110+
enableEndpoint();
111+
}
112+
75113
}

resilience-tests/src/test/java/resilience/http/MockTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.junit.jupiter.api.Test;
1010
import org.mockserver.integration.ClientAndServer;
1111

12+
import java.util.concurrent.ExecutionException;
13+
1214
import static org.assertj.core.api.Assertions.assertThat;
1315
import static org.assertj.core.api.Assertions.catchThrowable;
1416
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
@@ -57,4 +59,26 @@ void doTest() {
5759
.isInstanceOf(ArangoDBException.class)
5860
.hasMessageContaining("boom");
5961
}
62+
63+
@Test
64+
void doTestAsync() throws ExecutionException, InterruptedException {
65+
arangoDB.async().getVersion().get();
66+
67+
mockServer
68+
.when(
69+
request()
70+
.withMethod("GET")
71+
.withPath("/.*/_api/version")
72+
)
73+
.respond(
74+
response()
75+
.withStatusCode(503)
76+
.withBody("{\"error\":true,\"errorNum\":503,\"errorMessage\":\"boom\",\"code\":503}")
77+
);
78+
79+
Throwable thrown = catchThrowable(() -> arangoDB.async().getVersion().get()).getCause();
80+
assertThat(thrown)
81+
.isInstanceOf(ArangoDBException.class)
82+
.hasMessageContaining("boom");
83+
}
6084
}

resilience-tests/src/test/java/resilience/retry/RetriableCursorTest.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package resilience.retry;
22

3-
import com.arangodb.ArangoCursor;
4-
import com.arangodb.ArangoDB;
5-
import com.arangodb.ArangoDBException;
6-
import com.arangodb.Protocol;
3+
import com.arangodb.*;
74
import com.arangodb.model.AqlQueryOptions;
85
import eu.rekawek.toxiproxy.model.ToxicDirection;
96
import eu.rekawek.toxiproxy.model.toxic.Latency;
@@ -12,6 +9,7 @@
129
import resilience.SingleServerTest;
1310

1411
import java.io.IOException;
12+
import java.util.concurrent.ExecutionException;
1513
import java.util.concurrent.TimeoutException;
1614
import java.util.stream.Stream;
1715

@@ -31,7 +29,11 @@ static Stream<ArangoDB> arangoProvider() {
3129
);
3230
}
3331

34-
@ParameterizedTest
32+
static Stream<ArangoDBAsync> asyncArangoProvider() {
33+
return arangoProvider().map(ArangoDB::async);
34+
}
35+
36+
@ParameterizedTest(name = "{index}")
3537
@MethodSource("arangoProvider")
3638
void retryCursor(ArangoDB arangoDB) throws IOException {
3739
try (ArangoCursor<String> cursor = arangoDB.db()
@@ -53,4 +55,26 @@ void retryCursor(ArangoDB arangoDB) throws IOException {
5355
arangoDB.shutdown();
5456
}
5557

58+
@ParameterizedTest(name = "{index}")
59+
@MethodSource("asyncArangoProvider")
60+
void retryCursorAsync(ArangoDBAsync arangoDB) throws IOException, ExecutionException, InterruptedException {
61+
ArangoCursorAsync<String> c1 = arangoDB.db()
62+
.query("for i in 1..2 return i",
63+
String.class,
64+
new AqlQueryOptions().batchSize(1).allowRetry(true)).get();
65+
66+
assertThat(c1.getResult()).containsExactly("1");
67+
assertThat(c1.hasMore()).isTrue();
68+
Latency toxic = getEndpoint().getProxy().toxics().latency("latency", ToxicDirection.DOWNSTREAM, 10_000);
69+
Throwable thrown = catchThrowable(() -> c1.nextBatch().get()).getCause();
70+
assertThat(thrown).isInstanceOf(ArangoDBException.class);
71+
assertThat(thrown.getCause()).isInstanceOfAny(TimeoutException.class);
72+
toxic.remove();
73+
ArangoCursorAsync<String> c2 = c1.nextBatch().get();
74+
assertThat(c2.getResult()).containsExactly("2");
75+
assertThat(c2.hasMore()).isFalse();
76+
c2.close();
77+
arangoDB.shutdown();
78+
}
79+
5680
}

0 commit comments

Comments
 (0)