|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright (C) 2018 The Guava Authors | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | 
|  | 5 | + * in compliance with the License. You may obtain a copy of the License at | 
|  | 6 | + * | 
|  | 7 | + * http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | + * | 
|  | 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License | 
|  | 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | 
|  | 11 | + * or implied. See the License for the specific language governing permissions and limitations under | 
|  | 12 | + * the License. | 
|  | 13 | + */ | 
|  | 14 | + | 
|  | 15 | +package com.google.common.util.concurrent; | 
|  | 16 | + | 
|  | 17 | +import static com.google.common.truth.Truth.assertThat; | 
|  | 18 | +import static com.google.common.util.concurrent.Futures.getDone; | 
|  | 19 | +import static com.google.common.util.concurrent.MoreExecutors.directExecutor; | 
|  | 20 | + | 
|  | 21 | +import java.util.concurrent.Callable; | 
|  | 22 | +import java.util.concurrent.CountDownLatch; | 
|  | 23 | +import java.util.concurrent.ExecutorService; | 
|  | 24 | +import java.util.concurrent.Executors; | 
|  | 25 | +import java.util.concurrent.Future; | 
|  | 26 | +import java.util.concurrent.TimeUnit; | 
|  | 27 | +import org.junit.After; | 
|  | 28 | +import org.junit.Before; | 
|  | 29 | +import org.junit.Test; | 
|  | 30 | +import org.junit.runner.RunWith; | 
|  | 31 | +import org.junit.runners.JUnit4; | 
|  | 32 | + | 
|  | 33 | +/** Tests for {@link ExecutionSequencer} */ | 
|  | 34 | +@RunWith(JUnit4.class) | 
|  | 35 | +public class ExecutionSequencerTest { | 
|  | 36 | + | 
|  | 37 | +  ExecutorService executor; | 
|  | 38 | + | 
|  | 39 | +  private ExecutionSequencer serializer; | 
|  | 40 | +  private SettableFuture<Void> firstFuture; | 
|  | 41 | +  private TestCallable firstCallable; | 
|  | 42 | + | 
|  | 43 | +  @Before | 
|  | 44 | +  public void setUp() throws Exception { | 
|  | 45 | +    executor = Executors.newCachedThreadPool(); | 
|  | 46 | +    serializer = ExecutionSequencer.create(); | 
|  | 47 | +    firstFuture = SettableFuture.create(); | 
|  | 48 | +    firstCallable = new TestCallable(firstFuture); | 
|  | 49 | +  } | 
|  | 50 | + | 
|  | 51 | +  @After | 
|  | 52 | +  public void tearDown() throws Exception { | 
|  | 53 | +    executor.shutdown(); | 
|  | 54 | +  } | 
|  | 55 | + | 
|  | 56 | +  @Test | 
|  | 57 | +  public void testCallableStartsAfterFirstFutureCompletes() { | 
|  | 58 | +    @SuppressWarnings({"unused", "nullness"}) | 
|  | 59 | +    Future<?> possiblyIgnoredError = serializer.submitAsync(firstCallable, directExecutor()); | 
|  | 60 | +    TestCallable secondCallable = new TestCallable(Futures.<Void>immediateFuture(null)); | 
|  | 61 | +    @SuppressWarnings({"unused", "nullness"}) | 
|  | 62 | +    Future<?> possiblyIgnoredError1 = serializer.submitAsync(secondCallable, directExecutor()); | 
|  | 63 | +    assertThat(firstCallable.called).isTrue(); | 
|  | 64 | +    assertThat(secondCallable.called).isFalse(); | 
|  | 65 | +    firstFuture.set(null); | 
|  | 66 | +    assertThat(secondCallable.called).isTrue(); | 
|  | 67 | +  } | 
|  | 68 | + | 
|  | 69 | +  @Test | 
|  | 70 | +  public void testCancellationNotPropagatedIfAlreadyStarted() { | 
|  | 71 | +    serializer.submitAsync(firstCallable, directExecutor()).cancel(true); | 
|  | 72 | +    assertThat(firstFuture.isCancelled()).isFalse(); | 
|  | 73 | +  } | 
|  | 74 | + | 
|  | 75 | +  @Test | 
|  | 76 | +  public void testCancellationDoesNotViolateSerialization() { | 
|  | 77 | +    @SuppressWarnings({"unused", "nullness"}) | 
|  | 78 | +    Future<?> possiblyIgnoredError = serializer.submitAsync(firstCallable, directExecutor()); | 
|  | 79 | +    TestCallable secondCallable = new TestCallable(Futures.<Void>immediateFuture(null)); | 
|  | 80 | +    ListenableFuture<Void> secondFuture = serializer.submitAsync(secondCallable, directExecutor()); | 
|  | 81 | +    TestCallable thirdCallable = new TestCallable(Futures.<Void>immediateFuture(null)); | 
|  | 82 | +    @SuppressWarnings({"unused", "nullness"}) | 
|  | 83 | +    Future<?> possiblyIgnoredError1 = serializer.submitAsync(thirdCallable, directExecutor()); | 
|  | 84 | +    secondFuture.cancel(true); | 
|  | 85 | +    assertThat(secondCallable.called).isFalse(); | 
|  | 86 | +    assertThat(thirdCallable.called).isFalse(); | 
|  | 87 | +    firstFuture.set(null); | 
|  | 88 | +    assertThat(secondCallable.called).isFalse(); | 
|  | 89 | +    assertThat(thirdCallable.called).isTrue(); | 
|  | 90 | +  } | 
|  | 91 | + | 
|  | 92 | +  @Test | 
|  | 93 | +  public void testCancellationMultipleThreads() throws Exception { | 
|  | 94 | +    final BlockingCallable blockingCallable = new BlockingCallable(); | 
|  | 95 | +    ListenableFuture<Void> unused = serializer.submit(blockingCallable, executor); | 
|  | 96 | +    ListenableFuture<Boolean> future2 = | 
|  | 97 | +        serializer.submit( | 
|  | 98 | +            new Callable<Boolean>() { | 
|  | 99 | +              @Override | 
|  | 100 | +              public Boolean call() { | 
|  | 101 | +                return blockingCallable.isRunning(); | 
|  | 102 | +              } | 
|  | 103 | +            }, | 
|  | 104 | +            directExecutor()); | 
|  | 105 | + | 
|  | 106 | +    // Wait for the first task to be started in the background. It will block until we explicitly | 
|  | 107 | +    // stop it. | 
|  | 108 | +    blockingCallable.waitForStart(); | 
|  | 109 | + | 
|  | 110 | +    // Give the second task a chance to (incorrectly) start up while the first task is running. | 
|  | 111 | +    assertThat(future2.isDone()).isFalse(); | 
|  | 112 | + | 
|  | 113 | +    // Stop the first task. The second task should then run. | 
|  | 114 | +    blockingCallable.stop(); | 
|  | 115 | +    executor.shutdown(); | 
|  | 116 | +    assertThat(executor.awaitTermination(10, TimeUnit.SECONDS)).isTrue(); | 
|  | 117 | +    assertThat(getDone(future2)).isFalse(); | 
|  | 118 | +  } | 
|  | 119 | + | 
|  | 120 | +  @Test | 
|  | 121 | +  public void secondTaskWaitsForFirstEvenIfCancelled() throws Exception { | 
|  | 122 | +    final BlockingCallable blockingCallable = new BlockingCallable(); | 
|  | 123 | +    ListenableFuture<Void> future1 = serializer.submit(blockingCallable, executor); | 
|  | 124 | +    ListenableFuture<Boolean> future2 = | 
|  | 125 | +        serializer.submit( | 
|  | 126 | +            new Callable<Boolean>() { | 
|  | 127 | +              @Override | 
|  | 128 | +              public Boolean call() { | 
|  | 129 | +                return blockingCallable.isRunning(); | 
|  | 130 | +              } | 
|  | 131 | +            }, | 
|  | 132 | +            directExecutor()); | 
|  | 133 | + | 
|  | 134 | +    // Wait for the first task to be started in the background. It will block until we explicitly | 
|  | 135 | +    // stop it. | 
|  | 136 | +    blockingCallable.waitForStart(); | 
|  | 137 | + | 
|  | 138 | +    // This time, cancel the future for the first task. The task remains running, only the future | 
|  | 139 | +    // is cancelled. | 
|  | 140 | +    future1.cancel(false); | 
|  | 141 | + | 
|  | 142 | +    // Give the second task a chance to (incorrectly) start up while the first task is running. | 
|  | 143 | +    // (This is the assertion that fails.) | 
|  | 144 | +    assertThat(future2.isDone()).isFalse(); | 
|  | 145 | + | 
|  | 146 | +    // Stop the first task. The second task should then run. | 
|  | 147 | +    blockingCallable.stop(); | 
|  | 148 | +    executor.shutdown(); | 
|  | 149 | +    assertThat(executor.awaitTermination(10, TimeUnit.SECONDS)).isTrue(); | 
|  | 150 | +    assertThat(getDone(future2)).isFalse(); | 
|  | 151 | +  } | 
|  | 152 | + | 
|  | 153 | +  private static class BlockingCallable implements Callable<Void> { | 
|  | 154 | +    private final CountDownLatch startLatch = new CountDownLatch(1); | 
|  | 155 | +    private final CountDownLatch stopLatch = new CountDownLatch(1); | 
|  | 156 | + | 
|  | 157 | +    private volatile boolean running = false; | 
|  | 158 | + | 
|  | 159 | +    @Override | 
|  | 160 | +    public Void call() throws InterruptedException { | 
|  | 161 | +      running = true; | 
|  | 162 | +      startLatch.countDown(); | 
|  | 163 | +      stopLatch.await(); | 
|  | 164 | +      running = false; | 
|  | 165 | +      return null; | 
|  | 166 | +    } | 
|  | 167 | + | 
|  | 168 | +    public void waitForStart() throws InterruptedException { | 
|  | 169 | +      startLatch.await(); | 
|  | 170 | +    } | 
|  | 171 | + | 
|  | 172 | +    public void stop() { | 
|  | 173 | +      stopLatch.countDown(); | 
|  | 174 | +    } | 
|  | 175 | + | 
|  | 176 | +    public boolean isRunning() { | 
|  | 177 | +      return running; | 
|  | 178 | +    } | 
|  | 179 | +  } | 
|  | 180 | + | 
|  | 181 | +  private static final class TestCallable implements AsyncCallable<Void> { | 
|  | 182 | + | 
|  | 183 | +    private final ListenableFuture<Void> future; | 
|  | 184 | +    private boolean called = false; | 
|  | 185 | + | 
|  | 186 | +    private TestCallable(ListenableFuture<Void> future) { | 
|  | 187 | +      this.future = future; | 
|  | 188 | +    } | 
|  | 189 | + | 
|  | 190 | +    @Override | 
|  | 191 | +    public ListenableFuture<Void> call() throws Exception { | 
|  | 192 | +      called = true; | 
|  | 193 | +      return future; | 
|  | 194 | +    } | 
|  | 195 | +  } | 
|  | 196 | +} | 
0 commit comments