Java Code Examples for com.google.common.util.concurrent.Runnables#doNothing()

The following examples show how to use com.google.common.util.concurrent.Runnables#doNothing() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemoryStateUpdaterTests.java    From pravega with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the ability of the MemoryStateUpdater to delegate Enter/Exit recovery mode to the read index.
 */
@Test
public void testRecoveryMode() throws Exception {
    // Check it's properly delegated to Read index.
    SequencedItemList<Operation> opLog = new SequencedItemList<>();
    ArrayList<TestReadIndex.MethodInvocation> methodInvocations = new ArrayList<>();
    TestReadIndex readIndex = new TestReadIndex(methodInvocations::add);
    MemoryStateUpdater updater = new MemoryStateUpdater(opLog, readIndex, Runnables.doNothing());

    UpdateableContainerMetadata metadata1 = new MetadataBuilder(1).build();
    updater.enterRecoveryMode(metadata1);
    updater.exitRecoveryMode(true);

    Assert.assertEquals("Unexpected number of method invocations.", 2, methodInvocations.size());
    TestReadIndex.MethodInvocation enterRecovery = methodInvocations.get(0);
    Assert.assertEquals("ReadIndex.enterRecoveryMode was not called when expected.", TestReadIndex.ENTER_RECOVERY_MODE, enterRecovery.methodName);
    Assert.assertEquals("ReadIndex.enterRecoveryMode was called with the wrong arguments.", metadata1, enterRecovery.args.get("recoveryMetadataSource"));

    TestReadIndex.MethodInvocation exitRecovery = methodInvocations.get(1);
    Assert.assertEquals("ReadIndex.exitRecoveryMode was not called when expected.", TestReadIndex.EXIT_RECOVERY_MODE, exitRecovery.methodName);
    Assert.assertEquals("ReadIndex.exitRecoveryMode was called with the wrong arguments.", true, exitRecovery.args.get("successfulRecovery"));
}
 
Example 2
Source File: LambdaStreamObserver.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public LambdaStreamObserver(Consumer<T> onNext, Consumer<Throwable> onError) {
    this(
        onNext,
        onError,
        Runnables.doNothing()
    );
}
 
Example 3
Source File: LambdaStreamObserver.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public LambdaStreamObserver(Consumer<T> onNext) {
    this(
        onNext,
        t -> {
            throw new OnErrorNotImplementedException(t);
        },
        Runnables.doNothing()
    );
}
 
Example 4
Source File: DynamicScheduledThreadTest.java    From artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testShutdown() throws InterruptedException {
    DynamicScheduledThreadConfig dynamicScheduledThreadConfig = new DynamicScheduledThreadConfig(ArtemisClientConstants.Properties,
            new RangePropertyConfig<Integer>(20, 0, 200), new RangePropertyConfig<Integer>(500, 500, 5 * 1000));
    DynamicScheduledThread t = new DynamicScheduledThread("client", Runnables.doNothing(), dynamicScheduledThreadConfig);
    t.setDaemon(true);
    t.start();
    t.shutdown();
    Thread.sleep(500);
    Assert.assertFalse(t.isAlive());
}
 
Example 5
Source File: OperationProcessorTests.java    From pravega with Apache License 2.0 5 votes vote down vote up
private MetadataCheckpointPolicy getNoOpCheckpointPolicy() {
    // Turn off any MetadataCheckpointing. In these tests, we are doing that manually.
    DurableLogConfig dlConfig = DurableLogConfig
            .builder()
            .with(DurableLogConfig.CHECKPOINT_COMMIT_COUNT, Integer.MAX_VALUE)
            .with(DurableLogConfig.CHECKPOINT_TOTAL_COMMIT_LENGTH, Long.MAX_VALUE)
            .build();

    return new MetadataCheckpointPolicy(dlConfig, Runnables.doNothing(), executorService());
}
 
Example 6
Source File: OperationProcessorTests.java    From pravega with Apache License 2.0 5 votes vote down vote up
TestContext() {
    this.storage = InMemoryStorageFactory.newStorage(executorService());
    this.storage.initialize(1);
    this.metadata = new MetadataBuilder(CONTAINER_ID).build();
    ReadIndexConfig readIndexConfig = ReadIndexConfig.builder().with(ReadIndexConfig.STORAGE_READ_ALIGNMENT, 1024).build();
    this.cacheStorage = new DirectMemoryCache(Integer.MAX_VALUE);
    this.cacheManager = new CacheManager(CachePolicy.INFINITE, this.cacheStorage, executorService());
    this.readIndex = new ContainerReadIndex(readIndexConfig, this.metadata, this.storage, this.cacheManager, executorService());
    this.memoryLog = new SequencedItemList<>();
    this.stateUpdater = new MemoryStateUpdater(this.memoryLog, this.readIndex, Runnables.doNothing());
}
 
Example 7
Source File: InterruptiblesTest.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
@Test
void shouldWaitUntilThreadIsDead() {
  final Thread thread = new Thread(Runnables.doNothing());
  thread.start();

  assertTimeoutPreemptively(
      Duration.ofSeconds(5L), () -> assertThat(Interruptibles.join(thread), is(true)));
}
 
Example 8
Source File: ThreadsTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testNamedThread() {
  String name = "test";
  Runnable runnable = Runnables.doNothing();

  Thread thread = Threads.namedThread(name, runnable);

  assertNotNull(thread);
  assertFalse(thread.isDaemon());
  assertEquals(State.NEW, thread.getState());
  assertEquals(name, thread.getName());
}
 
Example 9
Source File: JimfsInputStreamTest.java    From jimfs with Apache License 2.0 5 votes vote down vote up
private static JimfsInputStream newInputStream(int... bytes) throws IOException {
  byte[] b = new byte[bytes.length];
  for (int i = 0; i < bytes.length; i++) {
    b[i] = (byte) bytes[i];
  }

  RegularFile file = regularFile(0);
  file.write(0, b, 0, b.length);
  return new JimfsInputStream(file, new FileSystemState(Runnables.doNothing()));
}
 
Example 10
Source File: PollingWatchServiceTest.java    From jimfs with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  fs = (JimfsFileSystem) Jimfs.newFileSystem(Configuration.unix());
  watcher =
      new PollingWatchService(
          fs.getDefaultView(),
          fs.getPathService(),
          new FileSystemState(Runnables.doNothing()),
          4,
          MILLISECONDS);
}
 
Example 11
Source File: OperationProcessorTests.java    From pravega with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the ability of the OperationProcessor to process Operations when a simulated DataCorruptionException
 * is generated.
 */
@Test
public void testWithDataCorruptionFailures() throws Exception {
    // If a DataCorruptionException is thrown for a particular Operation, the OperationQueueProcessor should
    // immediately shut down and stop accepting other ops.
    int streamSegmentCount = 10;
    int appendsPerStreamSegment = 80;
    int failAtOperationIndex = 123; // Fail Operation at index X.

    @Cleanup
    TestContext context = new TestContext();

    // Create a different state updater and Memory log - and use these throughout this test.
    CorruptedMemoryOperationLog corruptedMemoryLog = new CorruptedMemoryOperationLog(failAtOperationIndex);
    MemoryStateUpdater stateUpdater = new MemoryStateUpdater(corruptedMemoryLog, context.readIndex, Runnables.doNothing());

    // Generate some test data (no need to complicate ourselves with Transactions here; that is tested in the no-failure test).
    HashSet<Long> streamSegmentIds = createStreamSegmentsInMetadata(streamSegmentCount, context.metadata);
    List<Operation> operations = generateOperations(streamSegmentIds, new HashMap<>(), appendsPerStreamSegment,
            METADATA_CHECKPOINT_EVERY, false, false);

    // Setup an OperationProcessor and start it.
    @Cleanup
    TestDurableDataLog dataLog = TestDurableDataLog.create(CONTAINER_ID, MAX_DATA_LOG_APPEND_SIZE, executorService());
    dataLog.initialize(TIMEOUT);
    @Cleanup
    OperationProcessor operationProcessor = new OperationProcessor(context.metadata, stateUpdater,
            dataLog, getNoOpCheckpointPolicy(), executorService());
    operationProcessor.startAsync().awaitRunning();

    // Process all generated operations.
    List<OperationWithCompletion> completionFutures = processOperations(operations, operationProcessor);

    // Wait for the store to fail (and make sure it failed).
    AssertExtensions.assertThrows(
            "Operation Processor did not shut down with failure.",
            () -> ServiceListeners.awaitShutdown(operationProcessor, true),
            ex -> ex instanceof IllegalStateException);
    Assert.assertEquals("Unexpected service state after encountering DataCorruptionException.", Service.State.FAILED, operationProcessor.state());

    // Verify that the "right" operations failed, while the others succeeded.
    int successCount = 0;
    boolean encounteredFirstFailure = false;
    for (int i = 0; i < completionFutures.size(); i++) {
        OperationWithCompletion oc = completionFutures.get(i);

        // Once an operation failed (in our scenario), no other operation can succeed.
        if (encounteredFirstFailure) {
            Assert.assertTrue("Encountered successful operation after a failed operation: " + oc.operation, oc.completion.isCompletedExceptionally());
        }
        // The operation that failed may have inadvertently failed other operations that were aggregated together
        // with it, which is why it's hard to determine precisely what the first expected failed operation is.
        if (oc.completion.isCompletedExceptionally()) {
            // If we do find a failed one in this area, make sure it is failed with DataCorruptionException.
            AssertExtensions.assertThrows(
                    "Unexpected exception for failed Operation in the same DataFrame as intentionally failed operation.",
                    oc.completion::join,
                    super::isExpectedExceptionForDataCorruption);
            encounteredFirstFailure = true;
        } else {
            successCount++;
        }
    }

    AssertExtensions.assertGreaterThan("No operation succeeded.", 0, successCount);
    performLogOperationChecks(completionFutures, corruptedMemoryLog, dataLog, context.metadata, failAtOperationIndex - 1);

    // There is no point in performing metadata checks. A DataCorruptionException means the Metadata (and the general
    // state of the Container) is in an undefined state.
}
 
Example 12
Source File: ConcurrentBattleCalculator.java    From triplea with GNU General Public License v3.0 4 votes vote down vote up
public ConcurrentBattleCalculator() {
  this(Runnables.doNothing());
}
 
Example 13
Source File: JimfsOutputStreamTest.java    From jimfs with Apache License 2.0 4 votes vote down vote up
private static JimfsOutputStream newOutputStream(boolean append) {
  RegularFile file = regularFile(0);
  return new JimfsOutputStream(file, append, new FileSystemState(Runnables.doNothing()));
}
 
Example 14
Source File: JimfsFileChannelTest.java    From jimfs with Apache License 2.0 4 votes vote down vote up
private static FileChannel channel(RegularFile file, OpenOption... options) throws IOException {
  return new JimfsFileChannel(
      file,
      Options.getOptionsForChannel(ImmutableSet.copyOf(options)),
      new FileSystemState(Runnables.doNothing()));
}
 
Example 15
Source File: JimfsFileChannelTest.java    From jimfs with Apache License 2.0 4 votes vote down vote up
@Test
public void testFileTimeUpdates() throws IOException {
  RegularFile file = regularFile(10);
  FileChannel channel =
      new JimfsFileChannel(
          file,
          ImmutableSet.<OpenOption>of(READ, WRITE),
          new FileSystemState(Runnables.doNothing()));

  // accessed
  long accessTime = file.getLastAccessTime();
  Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);

  channel.read(ByteBuffer.allocate(10));
  assertNotEquals(accessTime, file.getLastAccessTime());

  accessTime = file.getLastAccessTime();
  Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);

  channel.read(ByteBuffer.allocate(10), 0);
  assertNotEquals(accessTime, file.getLastAccessTime());

  accessTime = file.getLastAccessTime();
  Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);

  channel.read(new ByteBuffer[] {ByteBuffer.allocate(10)});
  assertNotEquals(accessTime, file.getLastAccessTime());

  accessTime = file.getLastAccessTime();
  Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);

  channel.read(new ByteBuffer[] {ByteBuffer.allocate(10)}, 0, 1);
  assertNotEquals(accessTime, file.getLastAccessTime());

  accessTime = file.getLastAccessTime();
  Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);

  channel.transferTo(0, 10, new ByteBufferChannel(10));
  assertNotEquals(accessTime, file.getLastAccessTime());

  // modified
  long modifiedTime = file.getLastModifiedTime();
  Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);

  channel.write(ByteBuffer.allocate(10));
  assertNotEquals(modifiedTime, file.getLastModifiedTime());

  modifiedTime = file.getLastModifiedTime();
  Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);

  channel.write(ByteBuffer.allocate(10), 0);
  assertNotEquals(modifiedTime, file.getLastModifiedTime());

  modifiedTime = file.getLastModifiedTime();
  Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);

  channel.write(new ByteBuffer[] {ByteBuffer.allocate(10)});
  assertNotEquals(modifiedTime, file.getLastModifiedTime());

  modifiedTime = file.getLastModifiedTime();
  Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);

  channel.write(new ByteBuffer[] {ByteBuffer.allocate(10)}, 0, 1);
  assertNotEquals(modifiedTime, file.getLastModifiedTime());

  modifiedTime = file.getLastModifiedTime();
  Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);

  channel.truncate(0);
  assertNotEquals(modifiedTime, file.getLastModifiedTime());

  modifiedTime = file.getLastModifiedTime();
  Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);

  channel.transferFrom(new ByteBufferChannel(10), 0, 10);
  assertNotEquals(modifiedTime, file.getLastModifiedTime());
}