kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | baf47da1c0749cd18d577882e3cacb6cee5665c026132a6836a422c88e272fe7 |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-10-27 15:12 -05:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-11-05 18:37 +01:00 |
| Parents | 112b6d6f2201 |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/AbstractReconnectionHandler.java +3 −1
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | import java.util.concurrent.*; |
| 19 | 19 | import java.util.concurrent.atomic.AtomicReference; |
| 20 | 20 | |
| 21 | +import com.google.common.annotations.VisibleForTesting; | |
| 21 | 22 | import com.google.common.util.concurrent.ListenableFuture; |
| 22 | 23 | import com.google.common.util.concurrent.AbstractFuture; |
| 23 | 24 | import org.slf4j.Logger; |
| @@ -183,7 +184,8 @@ | ||
| 183 | 184 | } |
| 184 | 185 | |
| 185 | 186 | // 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> { | |
| 187 | 189 | // A future representing completion of the next task submitted to the executor |
| 188 | 190 | volatile ScheduledFuture<?> nextTry; |
| 189 | 191 |
modified driver-core/src/test/java/com/datastax/driver/core/AbstractReconnectionHandlerTest.java +26 −5
| @@ -25,6 +25,7 @@ | ||
| 25 | 25 | import com.google.common.util.concurrent.Futures; |
| 26 | 26 | import com.google.common.util.concurrent.ListenableFuture; |
| 27 | 27 | import com.google.common.util.concurrent.SettableFuture; |
| 28 | +import com.google.common.util.concurrent.Uninterruptibles; | |
| 28 | 29 | import org.slf4j.Logger; |
| 29 | 30 | import org.slf4j.LoggerFactory; |
| 30 | 31 | import org.testng.annotations.AfterMethod; |
| @@ -32,8 +33,12 @@ | ||
| 32 | 33 | import org.testng.annotations.Test; |
| 33 | 34 | |
| 34 | 35 | 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; | |
| 35 | 39 | import static org.testng.Assert.fail; |
| 36 | 40 | |
| 41 | +import com.datastax.driver.core.AbstractReconnectionHandler.HandlerFuture; | |
| 37 | 42 | import com.datastax.driver.core.AbstractReconnectionHandlerTest.MockReconnectionWork.ReconnectBehavior; |
| 38 | 43 | import com.datastax.driver.core.policies.ReconnectionPolicy.ReconnectionSchedule; |
| 39 | 44 | |
| @@ -48,7 +53,7 @@ | ||
| 48 | 53 | |
| 49 | 54 | @BeforeMethod(groups = {"unit", "short"}) |
| 50 | 55 | public void setup() { |
| 51 | - executor = Executors.newScheduledThreadPool(2); | |
| 56 | + executor = spy(Executors.newScheduledThreadPool(2)); | |
| 52 | 57 | schedule = new MockReconnectionSchedule(); |
| 53 | 58 | work = new MockReconnectionWork(); |
| 54 | 59 | future.set(null); |
| @@ -133,6 +138,9 @@ | ||
| 133 | 138 | public void should_stop_if_cancelled_between_attempts() { |
| 134 | 139 | handler.start(); |
| 135 | 140 | |
| 141 | + // Wait for the initial schedule of a reconnect. | |
| 142 | + verify(executor, timeout(1000)).schedule(handler, 0, TimeUnit.MILLISECONDS); | |
| 143 | + | |
| 136 | 144 | // Force a failed reconnect. |
| 137 | 145 | schedule.tick(); |
| 138 | 146 | work.nextReconnect = ReconnectBehavior.THROW_EXCEPTION; |
| @@ -143,17 +151,30 @@ | ||
| 143 | 151 | schedule.delay = 3000; |
| 144 | 152 | schedule.tick(); |
| 145 | 153 | |
| 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); | |
| 147 | 160 | future.get().cancel(false); |
| 148 | 161 | |
| 149 | - // Should block until scheduler runs, detects cancel and exits. | |
| 162 | + // Should immediately return as the future was cancelled while the task was scheduled. | |
| 150 | 163 | waitForCompletion(); |
| 151 | 164 | |
| 152 | 165 | assertThat(work.success).isFalse(); |
| 153 | 166 | // Should have had 1 failed attempt, no second attempt since cancelled. |
| 154 | 167 | 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()); | |
| 157 | 178 | } |
| 158 | 179 | |
| 159 | 180 | @Test(groups = "unit") |