|
| 1 | +package resilience.connection; |
| 2 | + |
| 3 | +import ch.qos.logback.classic.Level; |
| 4 | +import com.arangodb.*; |
| 5 | +import org.junit.jupiter.params.ParameterizedTest; |
| 6 | +import org.junit.jupiter.params.provider.MethodSource; |
| 7 | +import resilience.ClusterTest; |
| 8 | + |
| 9 | +import java.net.ConnectException; |
| 10 | +import java.net.UnknownHostException; |
| 11 | +import java.util.concurrent.ExecutionException; |
| 12 | +import java.util.stream.Stream; |
| 13 | + |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | +import static org.assertj.core.api.Assertions.catchThrowable; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Michele Rastelli |
| 19 | + */ |
| 20 | +class ConnectionClusterTest extends ClusterTest { |
| 21 | + |
| 22 | + static Stream<Protocol> protocolProvider() { |
| 23 | + return Stream.of( |
| 24 | + Protocol.VST, |
| 25 | + Protocol.HTTP_VPACK, |
| 26 | + Protocol.HTTP2_VPACK |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + static Stream<ArangoDB> arangoProvider() { |
| 31 | + return Stream.of( |
| 32 | + dbBuilder().protocol(Protocol.VST).build(), |
| 33 | + dbBuilder().protocol(Protocol.HTTP_VPACK).build(), |
| 34 | + dbBuilder().protocol(Protocol.HTTP2_JSON).build() |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + static Stream<ArangoDBAsync> asyncArangoProvider() { |
| 39 | + return arangoProvider().map(ArangoDB::async); |
| 40 | + } |
| 41 | + |
| 42 | + @ParameterizedTest |
| 43 | + @MethodSource("protocolProvider") |
| 44 | + void nameResolutionFail(Protocol protocol) { |
| 45 | + ArangoDB arangoDB = new ArangoDB.Builder() |
| 46 | + .host("wrongHost", 8529) |
| 47 | + .protocol(protocol) |
| 48 | + .build(); |
| 49 | + |
| 50 | + Throwable thrown = catchThrowable(arangoDB::getVersion); |
| 51 | + assertThat(thrown).isInstanceOf(ArangoDBException.class); |
| 52 | + assertThat(thrown.getMessage()).contains("Cannot contact any host!"); |
| 53 | + assertThat(thrown.getCause()).isNotNull(); |
| 54 | + assertThat(thrown.getCause()).isInstanceOf(ArangoDBMultipleException.class); |
| 55 | + ((ArangoDBMultipleException) thrown.getCause()).getExceptions().forEach(e -> { |
| 56 | + assertThat(e).isInstanceOf(UnknownHostException.class); |
| 57 | + assertThat(e.getMessage()).contains("wrongHost"); |
| 58 | + }); |
| 59 | + arangoDB.shutdown(); |
| 60 | + } |
| 61 | + |
| 62 | + @ParameterizedTest |
| 63 | + @MethodSource("protocolProvider") |
| 64 | + void nameResolutionFailAsync(Protocol protocol) { |
| 65 | + ArangoDBAsync arangoDB = new ArangoDB.Builder() |
| 66 | + .host("wrongHost", 8529) |
| 67 | + .protocol(protocol) |
| 68 | + .build() |
| 69 | + .async(); |
| 70 | + |
| 71 | + Throwable thrown = catchThrowable(() -> arangoDB.getVersion().get()).getCause(); |
| 72 | + assertThat(thrown).isInstanceOf(ArangoDBException.class); |
| 73 | + assertThat(thrown.getMessage()).contains("Cannot contact any host!"); |
| 74 | + assertThat(thrown.getCause()).isNotNull(); |
| 75 | + assertThat(thrown.getCause()).isInstanceOf(ArangoDBMultipleException.class); |
| 76 | + ((ArangoDBMultipleException) thrown.getCause()).getExceptions().forEach(e -> { |
| 77 | + assertThat(e).isInstanceOf(UnknownHostException.class); |
| 78 | + assertThat(e.getMessage()).contains("wrongHost"); |
| 79 | + }); |
| 80 | + arangoDB.shutdown(); |
| 81 | + } |
| 82 | + |
| 83 | + @ParameterizedTest |
| 84 | + @MethodSource("protocolProvider") |
| 85 | + void nameResolutionFailover(Protocol protocol) { |
| 86 | + ArangoDB arangoDB = new ArangoDB.Builder() |
| 87 | + .password("test") |
| 88 | + .host("wrongHost", 8529) |
| 89 | + .host("127.0.0.1", 8529) |
| 90 | + .protocol(protocol) |
| 91 | + .build(); |
| 92 | + |
| 93 | + arangoDB.getVersion(); |
| 94 | + |
| 95 | + assertThat(logs.getLogs()) |
| 96 | + .filteredOn(e -> e.getLevel().equals(Level.WARN)) |
| 97 | + .anyMatch(e -> e.getFormattedMessage().contains("Could not connect to host")); |
| 98 | + |
| 99 | + arangoDB.shutdown(); |
| 100 | + } |
| 101 | + |
| 102 | + @ParameterizedTest |
| 103 | + @MethodSource("protocolProvider") |
| 104 | + void nameResolutionFailoverAsync(Protocol protocol) throws ExecutionException, InterruptedException { |
| 105 | + ArangoDBAsync arangoDB = new ArangoDB.Builder() |
| 106 | + .password("test") |
| 107 | + .host("wrongHost", 8529) |
| 108 | + .host("127.0.0.1", 8529) |
| 109 | + .protocol(protocol) |
| 110 | + .build() |
| 111 | + .async(); |
| 112 | + |
| 113 | + arangoDB.getVersion().get(); |
| 114 | + |
| 115 | + assertThat(logs.getLogs()) |
| 116 | + .filteredOn(e -> e.getLevel().equals(Level.WARN)) |
| 117 | + .anyMatch(e -> e.getFormattedMessage().contains("Could not connect to host")); |
| 118 | + |
| 119 | + arangoDB.shutdown(); |
| 120 | + } |
| 121 | + |
| 122 | + @ParameterizedTest(name = "{index}") |
| 123 | + @MethodSource("arangoProvider") |
| 124 | + void connectionFail(ArangoDB arangoDB) { |
| 125 | + disableAllEndpoints(); |
| 126 | + |
| 127 | + Throwable thrown = catchThrowable(arangoDB::getVersion); |
| 128 | + assertThat(thrown).isInstanceOf(ArangoDBException.class); |
| 129 | + assertThat(thrown.getMessage()).contains("Cannot contact any host"); |
| 130 | + assertThat(thrown.getCause()).isNotNull(); |
| 131 | + assertThat(thrown.getCause()).isInstanceOf(ArangoDBMultipleException.class); |
| 132 | + ((ArangoDBMultipleException) thrown.getCause()).getExceptions().forEach(e -> |
| 133 | + assertThat(e).isInstanceOf(ConnectException.class)); |
| 134 | + |
| 135 | + arangoDB.shutdown(); |
| 136 | + enableAllEndpoints(); |
| 137 | + } |
| 138 | + |
| 139 | + @ParameterizedTest(name = "{index}") |
| 140 | + @MethodSource("asyncArangoProvider") |
| 141 | + void connectionFailAsync(ArangoDBAsync arangoDB) { |
| 142 | + disableAllEndpoints(); |
| 143 | + |
| 144 | + Throwable thrown = catchThrowable(() -> arangoDB.getVersion().get()).getCause(); |
| 145 | + assertThat(thrown).isInstanceOf(ArangoDBException.class); |
| 146 | + assertThat(thrown.getMessage()).contains("Cannot contact any host"); |
| 147 | + assertThat(thrown.getCause()).isNotNull(); |
| 148 | + assertThat(thrown.getCause()).isInstanceOf(ArangoDBMultipleException.class); |
| 149 | + ((ArangoDBMultipleException) thrown.getCause()).getExceptions().forEach(e -> |
| 150 | + assertThat(e).isInstanceOf(ConnectException.class)); |
| 151 | + arangoDB.shutdown(); |
| 152 | + enableAllEndpoints(); |
| 153 | + } |
| 154 | + |
| 155 | + @ParameterizedTest(name = "{index}") |
| 156 | + @MethodSource("arangoProvider") |
| 157 | + void connectionFailover(ArangoDB arangoDB) { |
| 158 | + getEndpoints().get(0).disable(); |
| 159 | + getEndpoints().get(1).disable(); |
| 160 | + |
| 161 | + arangoDB.getVersion(); |
| 162 | + |
| 163 | + assertThat(logs.getLogs()) |
| 164 | + .filteredOn(e -> e.getLevel().equals(Level.WARN)) |
| 165 | + .anyMatch(e -> e.getFormattedMessage().contains("Could not connect to host")); |
| 166 | + |
| 167 | + arangoDB.shutdown(); |
| 168 | + enableAllEndpoints(); |
| 169 | + } |
| 170 | + |
| 171 | + @ParameterizedTest(name = "{index}") |
| 172 | + @MethodSource("asyncArangoProvider") |
| 173 | + void connectionFailoverAsync(ArangoDBAsync arangoDB) throws ExecutionException, InterruptedException { |
| 174 | + getEndpoints().get(0).disable(); |
| 175 | + getEndpoints().get(1).disable(); |
| 176 | + |
| 177 | + arangoDB.getVersion().get(); |
| 178 | + |
| 179 | + assertThat(logs.getLogs()) |
| 180 | + .filteredOn(e -> e.getLevel().equals(Level.WARN)) |
| 181 | + .anyMatch(e -> e.getFormattedMessage().contains("Could not connect to host")); |
| 182 | + |
| 183 | + arangoDB.shutdown(); |
| 184 | + enableAllEndpoints(); |
| 185 | + } |
| 186 | + |
| 187 | + @ParameterizedTest(name = "{index}") |
| 188 | + @MethodSource("arangoProvider") |
| 189 | + void connectionFailoverPost(ArangoDB arangoDB) { |
| 190 | + getEndpoints().get(0).disable(); |
| 191 | + getEndpoints().get(1).disable(); |
| 192 | + |
| 193 | + arangoDB.db().query("RETURN 1", Integer.class); |
| 194 | + |
| 195 | + assertThat(logs.getLogs()) |
| 196 | + .filteredOn(e -> e.getLevel().equals(Level.WARN)) |
| 197 | + .anyMatch(e -> e.getFormattedMessage().contains("Could not connect to host")); |
| 198 | + |
| 199 | + arangoDB.shutdown(); |
| 200 | + enableAllEndpoints(); |
| 201 | + } |
| 202 | + |
| 203 | + @ParameterizedTest(name = "{index}") |
| 204 | + @MethodSource("asyncArangoProvider") |
| 205 | + void connectionFailoverPostAsync(ArangoDBAsync arangoDB) throws ExecutionException, InterruptedException { |
| 206 | + getEndpoints().get(0).disable(); |
| 207 | + getEndpoints().get(1).disable(); |
| 208 | + |
| 209 | + arangoDB.db().query("RETURN 1", Integer.class).get(); |
| 210 | + |
| 211 | + assertThat(logs.getLogs()) |
| 212 | + .filteredOn(e -> e.getLevel().equals(Level.WARN)) |
| 213 | + .anyMatch(e -> e.getFormattedMessage().contains("Could not connect to host")); |
| 214 | + |
| 215 | + arangoDB.shutdown(); |
| 216 | + enableAllEndpoints(); |
| 217 | + } |
| 218 | + |
| 219 | +} |
0 commit comments