Elffuss BraidHub

kikocis/java-driver public

Ensure cancel actually occurs between retry attempts.
Changesetbaf47da1c0749cd18d577882e3cacb6cee5665c026132a6836a422c88e272fe7
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-10-27 15:12 -05:00
Committerolim7t <omichallat+github@gmail.com> · 2015-11-05 18:37 +01:00
Parents112b6d6f2201

Browse the files at this changeset

2 files changed

modified driver-core/src/main/java/com/datastax/driver/core/AbstractReconnectionHandler.java +3 −1
@@ -18,6 +18,7 @@
1818 import java.util.concurrent.*;
1919 import java.util.concurrent.atomic.AtomicReference;
2020
21+import com.google.common.annotations.VisibleForTesting;
2122 import com.google.common.util.concurrent.ListenableFuture;
2223 import com.google.common.util.concurrent.AbstractFuture;
2324 import org.slf4j.Logger;
@@ -183,7 +184,8 @@
183184 }
184185
185186 // The future that the handler exposes to its clients via currentAttempt
186- private static class HandlerFuture extends AbstractFuture<Void> {
187+ @VisibleForTesting
188+ static class HandlerFuture extends AbstractFuture<Void> {
187189 // A future representing completion of the next task submitted to the executor
188190 volatile ScheduledFuture<?> nextTry;
189191
modified driver-core/src/test/java/com/datastax/driver/core/AbstractReconnectionHandlerTest.java +26 −5
@@ -25,6 +25,7 @@
2525 import com.google.common.util.concurrent.Futures;
2626 import com.google.common.util.concurrent.ListenableFuture;
2727 import com.google.common.util.concurrent.SettableFuture;
28+import com.google.common.util.concurrent.Uninterruptibles;
2829 import org.slf4j.Logger;
2930 import org.slf4j.LoggerFactory;
3031 import org.testng.annotations.AfterMethod;
@@ -32,8 +33,12 @@
3233 import org.testng.annotations.Test;
3334
3435 import static org.assertj.core.api.Assertions.assertThat;
36+import static org.mockito.Mockito.spy;
37+import static org.mockito.Mockito.timeout;
38+import static org.mockito.Mockito.verify;
3539 import static org.testng.Assert.fail;
3640
41+import com.datastax.driver.core.AbstractReconnectionHandler.HandlerFuture;
3742 import com.datastax.driver.core.AbstractReconnectionHandlerTest.MockReconnectionWork.ReconnectBehavior;
3843 import com.datastax.driver.core.policies.ReconnectionPolicy.ReconnectionSchedule;
3944
@@ -48,7 +53,7 @@
4853
4954 @BeforeMethod(groups = {"unit", "short"})
5055 public void setup() {
51- executor = Executors.newScheduledThreadPool(2);
56+ executor = spy(Executors.newScheduledThreadPool(2));
5257 schedule = new MockReconnectionSchedule();
5358 work = new MockReconnectionWork();
5459 future.set(null);
@@ -133,6 +138,9 @@
133138 public void should_stop_if_cancelled_between_attempts() {
134139 handler.start();
135140
141+ // Wait for the initial schedule of a reconnect.
142+ verify(executor, timeout(1000)).schedule(handler, 0, TimeUnit.MILLISECONDS);
143+
136144 // Force a failed reconnect.
137145 schedule.tick();
138146 work.nextReconnect = ReconnectBehavior.THROW_EXCEPTION;
@@ -143,17 +151,30 @@
143151 schedule.delay = 3000;
144152 schedule.tick();
145153
146- // At this point the reconnect should be scheduled but not executed yet, cancel the future.
154+ // Ensure reconnect is scheduled (slight timing window after handling failed reconnect
155+ // and scheduling next reconnect).
156+ verify(executor, timeout(1000)).schedule(handler, schedule.delay, TimeUnit.MILLISECONDS);
157+
158+ // Add a short delay to account for nextTry being assigned after schedule completes.
159+ Uninterruptibles.sleepUninterruptibly(5, TimeUnit.MILLISECONDS);
147160 future.get().cancel(false);
148161
149- // Should block until scheduler runs, detects cancel and exits.
162+ // Should immediately return as the future was cancelled while the task was scheduled.
150163 waitForCompletion();
151164
152165 assertThat(work.success).isFalse();
153166 // Should have had 1 failed attempt, no second attempt since cancelled.
154167 assertThat(work.tries).isEqualTo(1);
155- // The future will be marked null once it is detected as cancelled.
156- assertThat(future.get()).isNull();
168+
169+ // The future will be marked cancelled and thus not executed.
170+ ListenableFuture<?> currentAttempt = future.get();
171+ assertThat(currentAttempt).isInstanceOf(HandlerFuture.class);
172+ HandlerFuture handlerFuture = (HandlerFuture)currentAttempt;
173+ assertThat(handlerFuture.isCancelled());
174+
175+ // The next try should also be cancelled.
176+ assertThat(handlerFuture.nextTry).isNotNull();
177+ assertThat(handlerFuture.nextTry.isCancelled());
157178 }
158179
159180 @Test(groups = "unit")