jdeppe-pivotal commented on a change in pull request #6704:
URL: https://github.com/apache/geode/pull/6704#discussion_r678395538



##########
File path: 
geode-apis-compatible-with-redis/src/test/java/org/apache/geode/redis/internal/services/StripedExecutorServiceJUnitTest.java
##########
@@ -0,0 +1,335 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional 
information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache 
License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the 
License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software 
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
KIND, either express
+ * or implied. See the License for the specific language governing permissions 
and limitations under
+ * the License.
+ */
+
+package org.apache.geode.redis.internal.services;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Collection;
+import java.util.concurrent.CompletionService;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorCompletionService;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Heinz Kabutz
+ */
+public class StripedExecutorServiceJUnitTest {
+  @Before
+  public void initialize() {
+    TestRunnable.outOfSequence = false;
+    TestUnstripedRunnable.outOfSequence = false;
+    TestFastRunnable.outOfSequence = false;
+  }
+
+  @Test
+  public void testSingleStripeRunnable() throws InterruptedException {
+    ExecutorService pool = new StripedExecutorService();
+    Object stripe = new Object();
+    AtomicInteger actual = new AtomicInteger(0);
+    for (int i = 0; i < 100; i++) {
+      pool.submit(new TestRunnable(stripe, actual, i));
+    }
+    assertThat(pool.isTerminated()).isFalse();
+    assertThat(pool.isShutdown()).isFalse();
+
+    pool.shutdown();
+
+    assertThat(pool.awaitTermination(1, TimeUnit.HOURS)).isTrue();
+    assertThat(TestRunnable.outOfSequence)
+        .as("Expected no out-of-sequence runnables to execute")
+        .isFalse();
+    assertThat(pool.isTerminated()).isTrue();
+  }
+
+  @Test
+  public void testShutdown() throws InterruptedException {
+    ThreadGroup group = new ThreadGroup("stripetestgroup");
+    Thread starter = new Thread(group, "starter") {
+      public void run() {
+        ExecutorService pool = new StripedExecutorService();
+        Object stripe = new Object();
+        AtomicInteger actual = new AtomicInteger(0);
+        for (int i = 0; i < 100; i++) {
+          pool.submit(new TestRunnable(stripe, actual, i));
+        }
+        pool.shutdown();
+      }
+    };
+    starter.start();
+    starter.join();
+
+    for (int i = 0; i < 100; i++) {
+      if (group.activeCount() == 0) {
+        return;
+      }
+      Thread.sleep(100);
+    }
+
+    assertThat(group.activeCount()).isEqualTo(0);
+  }
+
+  @Test
+  public void testShutdownNow() throws InterruptedException {
+    ExecutorService pool = new StripedExecutorService();
+    Object stripe = new Object();
+    AtomicInteger actual = new AtomicInteger(0);
+    for (int i = 0; i < 100; i++) {
+      pool.submit(new TestRunnable(stripe, actual, i));
+    }
+    Thread.sleep(500);
+
+    assertThat(pool.isTerminated()).isFalse();
+    Collection<Runnable> unfinishedJobs = pool.shutdownNow();
+
+    assertThat(pool.isShutdown()).isTrue();
+    assertThat(pool.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
+    assertThat(pool.isTerminated()).isTrue();
+
+    assertThat(unfinishedJobs.size() > 0).isTrue();
+
+    assertThat(unfinishedJobs.size() + actual.intValue()).isEqualTo(100);
+  }
+
+  @Test
+  public void testSingleStripeCallableWithCompletionService()
+      throws InterruptedException, ExecutionException {
+    ExecutorService pool = new StripedExecutorService();
+    final CompletionService<Integer> cs = new ExecutorCompletionService<>(
+        pool);
+
+    Thread testSubmitter = new Thread("TestSubmitter") {
+      public void run() {
+        Object stripe = new Object();
+        for (int i = 0; i < 50; i++) {
+          cs.submit(new TestCallable(stripe, i));
+        }
+        try {
+          Thread.sleep(2000);
+        } catch (InterruptedException e) {
+          interrupt();
+        }
+        for (int i = 50; i < 100; i++) {
+          cs.submit(new TestCallable(stripe, i));
+        }
+      }
+    };
+    testSubmitter.start();
+
+    for (int i = 0; i < 100; i++) {
+      int actual = cs.take().get().intValue();

Review comment:
       Done




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to