Elffuss BraidHub

kikocis/java-driver public

Update tests to account for retry behavior change (#714)

As of JAVA-1212, non-idempotent queries are no longer retried for writes
and request errors.  Tests that expected this have been updated to
account for this.
Changeset87dc28d80ecc720f07062707295ce32a8c7bdcfcaba89e22a6017567dcde52bb
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2016-07-19 12:53 -05:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-07-19 10:53 -07:00
Parents465ac6b8b47f

Browse the files at this changeset

5 files changed

modified driver-core/src/test/java/com/datastax/driver/core/QueryLoggerErrorsTest.java +28 −24
@@ -16,6 +16,7 @@
1616 package com.datastax.driver.core;
1717
1818 import com.datastax.driver.core.exceptions.*;
19+import com.datastax.driver.core.policies.FallthroughRetryPolicy;
1920 import com.google.common.util.concurrent.Uninterruptibles;
2021 import org.apache.log4j.Level;
2122 import org.apache.log4j.Logger;
@@ -66,7 +67,7 @@
6667
6768 queryLogger = null;
6869
69- cluster = createClusterBuilder().build();
70+ cluster = createClusterBuilder().withRetryPolicy(FallthroughRetryPolicy.INSTANCE).build();
7071 session = cluster.connect();
7172 }
7273
@@ -175,8 +176,8 @@
175176 // when
176177 try {
177178 session.execute(query);
178- fail("Should have thrown NoHostAvailableException");
179- } catch (NoHostAvailableException e) {
179+ fail("Should have thrown OperationTimedOutException");
180+ } catch (OperationTimedOutException e) {
180181 // ok
181182 }
182183 // then
@@ -192,25 +193,25 @@
192193 @DataProvider(name = "errors")
193194 public static Object[][] createErrors() {
194195 return new Object[][]{
195- {unavailable, NoHostAvailableException.class, UnavailableException.class},
196- {write_request_timeout, WriteTimeoutException.class, WriteTimeoutException.class},
197- {read_request_timeout, ReadTimeoutException.class, ReadTimeoutException.class},
198- {server_error, NoHostAvailableException.class, ServerError.class},
199- {protocol_error, ProtocolError.class, ProtocolError.class},
200- {bad_credentials, AuthenticationException.class, AuthenticationException.class},
201- {overloaded, NoHostAvailableException.class, OverloadedException.class},
202- {is_bootstrapping, NoHostAvailableException.class, BootstrappingException.class},
203- {truncate_error, TruncateException.class, TruncateException.class},
204- {syntax_error, SyntaxError.class, SyntaxError.class},
205- {invalid, InvalidQueryException.class, InvalidQueryException.class},
206- {config_error, InvalidConfigurationInQueryException.class, InvalidConfigurationInQueryException.class},
207- {already_exists, AlreadyExistsException.class, AlreadyExistsException.class},
208- {unprepared, DriverInternalError.class, UnpreparedException.class}
196+ {unavailable, UnavailableException.class},
197+ {write_request_timeout, WriteTimeoutException.class},
198+ {read_request_timeout, ReadTimeoutException.class},
199+ {server_error, ServerError.class},
200+ {protocol_error, ProtocolError.class},
201+ {bad_credentials, AuthenticationException.class},
202+ {overloaded, OverloadedException.class},
203+ {is_bootstrapping, BootstrappingException.class},
204+ {truncate_error, TruncateException.class},
205+ {syntax_error, SyntaxError.class},
206+ {invalid, InvalidQueryException.class},
207+ {config_error, InvalidConfigurationInQueryException.class},
208+ {already_exists, AlreadyExistsException.class},
209+ {unprepared, UnpreparedException.class}
209210 };
210211 }
211212
212213 @Test(groups = "short", dataProvider = "errors")
213- public void should_log_exception_from_the_given_result(Result result, Class<? extends Exception> expectedException, Class<? extends Exception> loggedException) throws Exception {
214+ public void should_log_exception_from_the_given_result(Result result, Class<? extends Exception> expectedException) throws Exception {
214215 // given
215216 error.setLevel(DEBUG);
216217 queryLogger = builder().build();
@@ -225,12 +226,15 @@
225226 // when
226227 try {
227228 session.execute(query);
228- fail("Should have thrown NoHostAvailableException");
229+ fail("Should have thrown Exception");
229230 } catch (Exception e) {
230- if (e instanceof NoHostAvailableException) {
231- assertThat(expectedException).isEqualTo(NoHostAvailableException.class);
232- Throwable error = ((NoHostAvailableException) e).getErrors().get(hostAddress);
233- assertThat(error).isNotNull().isOfAnyClassIn(loggedException);
231+ if (expectedException == UnpreparedException.class) {
232+ // Special case UnpreparedException, it raises DriverInternalError instead.
233+ assertThat(e).isInstanceOf(DriverInternalError.class);
234+ } else if (expectedException == BootstrappingException.class) {
235+ // Special case BootstrappingException, it's wrapped in NHAE since it's always retried.
236+ assertThat(e).isInstanceOf(NoHostAvailableException.class);
237+ assertThat(((NoHostAvailableException) e).getErrors().get(hostAddress)).isInstanceOf(expectedException);
234238 } else {
235239 assertThat(e).isInstanceOf(expectedException);
236240 }
@@ -242,7 +246,7 @@
242246 .contains(ip)
243247 .contains(Integer.toString(scassandra.getBinaryPort()))
244248 .contains(query)
245- .contains(loggedException.getName());
249+ .contains(expectedException.getName());
246250 }
247251
248252 }
modified driver-core/src/test/java/com/datastax/driver/core/ReadTimeoutTest.java +2 −11
@@ -15,13 +15,10 @@
1515 */
1616 package com.datastax.driver.core;
1717
18-import com.datastax.driver.core.exceptions.NoHostAvailableException;
1918 import com.datastax.driver.core.exceptions.OperationTimedOutException;
2019 import org.testng.annotations.BeforeMethod;
2120 import org.testng.annotations.Test;
2221
23-import static org.assertj.core.api.Assertions.assertThat;
24-import static org.assertj.core.api.Assertions.fail;
2522 import static org.scassandra.http.client.PrimingRequest.queryBuilder;
2623 import static org.scassandra.http.client.PrimingRequest.then;
2724
@@ -42,15 +39,9 @@
4239 cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(10);
4340 }
4441
45- @Test(groups = "short")
42+ @Test(groups = "short", expectedExceptions = OperationTimedOutException.class)
4643 public void should_use_default_timeout_if_not_overridden_by_statement() {
47- try {
48- session.execute(query);
49- fail("expected a timeout");
50- } catch (NoHostAvailableException e) {
51- Throwable t = e.getErrors().values().iterator().next();
52- assertThat(t).isInstanceOf(OperationTimedOutException.class);
53- }
44+ session.execute(query);
5445 }
5546
5647 @Test(groups = "short")
modified driver-core/src/test/java/com/datastax/driver/core/ReusedStreamIdTest.java +3 −3
@@ -15,7 +15,7 @@
1515 */
1616 package com.datastax.driver.core;
1717
18-import com.datastax.driver.core.exceptions.NoHostAvailableException;
18+import com.datastax.driver.core.exceptions.OperationTimedOutException;
1919 import com.google.common.util.concurrent.FutureCallback;
2020 import com.google.common.util.concurrent.Futures;
2121 import com.google.common.util.concurrent.Uninterruptibles;
@@ -116,8 +116,8 @@
116116 @Override
117117 public void onFailure(Throwable t) {
118118 semaphore.release();
119- // NHAEs are inevitable because of low query timeouts and blocked threads.
120- if (!(t instanceof NoHostAvailableException)) {
119+ // Timeouts are inevitable because of low query timeouts and blocked threads.
120+ if (!(t instanceof OperationTimedOutException)) {
121121 logger.error("Unexpected error encountered.", t);
122122 errorTrigger.countDown();
123123 }
modified driver-core/src/test/java/com/datastax/driver/core/policies/AbstractRetryPolicyIntegrationTest.java +2 −0
@@ -89,6 +89,8 @@
8989 .setMaxConnectionsPerHost(HostDistance.LOCAL, 1)
9090 .setHeartbeatIntervalSeconds(0))
9191 .withNettyOptions(nonQuietClusterCloseOptions)
92+ // Mark everything as idempotent by default so RetryPolicy is exercised.
93+ .withQueryOptions(new QueryOptions().setDefaultIdempotence(true))
9294 .build();
9395
9496 session = cluster.connect();
modified driver-core/src/test/java/com/datastax/driver/core/policies/IdempotenceAwareRetryPolicyIntegrationTest.java +8 −4
@@ -52,7 +52,8 @@
5252 query();
5353 fail("expected an WriteTimeoutException");
5454 } catch (WriteTimeoutException e) {/* expected */}
55- assertOnWriteTimeoutWasCalled(1);
55+ // Should not have even been called as statement was not idempotent.
56+ assertOnWriteTimeoutWasCalled(0);
5657 assertThat(errors.getWriteTimeouts().getCount()).isEqualTo(1);
5758 assertThat(errors.getRetries().getCount()).isEqualTo(0);
5859 assertThat(errors.getRetriesOnWriteTimeout().getCount()).isEqualTo(0);
@@ -91,7 +92,8 @@
9192 String.format("[%s] Timed out waiting for server response", host1.getSocketAddress())
9293 );
9394 }
94- assertOnRequestErrorWasCalled(1, OperationTimedOutException.class);
95+ // Should not have even been called as statement was not idempotent.
96+ assertOnRequestErrorWasCalled(0, OperationTimedOutException.class);
9597 assertThat(errors.getClientTimeouts().getCount()).isEqualTo(1);
9698 assertThat(errors.getRetries().getCount()).isEqualTo(0);
9799 assertThat(errors.getRetriesOnClientTimeout().getCount()).isEqualTo(0);
@@ -135,7 +137,8 @@
135137 } catch (DriverException e) {
136138 assertThat(e).isInstanceOf(exception);
137139 }
138- assertOnRequestErrorWasCalled(1, ServerError.class);
140+ // Should not have even been called as statement was not idempotent.
141+ assertOnRequestErrorWasCalled(0, ServerError.class);
139142 assertThat(errors.getOthers().getCount()).isEqualTo(1);
140143 assertThat(errors.getRetries().getCount()).isEqualTo(0);
141144 assertThat(errors.getRetriesOnOtherErrors().getCount()).isEqualTo(0);
@@ -183,7 +186,8 @@
183186 String.format("[%s] Connection has been closed", host1.getSocketAddress())
184187 );
185188 }
186- assertOnRequestErrorWasCalled(1, TransportException.class);
189+ // Should not have even been called as statement was not idempotent.
190+ assertOnRequestErrorWasCalled(0, TransportException.class);
187191 assertThat(errors.getRetries().getCount()).isEqualTo(0);
188192 assertThat(errors.getConnectionErrors().getCount()).isEqualTo(1);
189193 assertThat(errors.getIgnoresOnConnectionError().getCount()).isEqualTo(0);