[GitHub] [geode] dschneider-pivotal commented on a change in pull request #4973: improvement/GEODE-8002 Refactor: Extract class to encapsulate concurrent execution

2020-04-21 Thread GitBox


dschneider-pivotal commented on a change in pull request #4973:
URL: https://github.com/apache/geode/pull/4973#discussion_r412318830



##
File path: 
geode-redis/src/integrationTest/java/org/apache/geode/redis/general/LoopingThreads.java
##
@@ -0,0 +1,88 @@
+/*
+ * 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.general;
+
+import java.util.Arrays;
+import java.util.concurrent.CountDownLatch;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+public class LoopingThreads {
+  private final int iterationCount;
+  private final Function[] functions;
+
+  @SuppressWarnings("unchecked")
+  public LoopingThreads(int iterationCount,
+  Function... functions) {
+this.iterationCount = iterationCount;
+this.functions = (Function[]) functions;
+  }
+
+  public void run() {
+CountDownLatch latch = new CountDownLatch(1);
+Stream loopingThreadStream = Arrays
+.stream(functions)
+.map((r) -> new LoopingThread(r, iterationCount, latch))
+.map((t) -> {
+  t.start();
+  return t;
+});
+
+latch.countDown();
+
+loopingThreadStream.forEach(loopingThread -> {
+  try {
+loopingThread.join();
+  } catch (InterruptedException e) {
+throw new RuntimeException(e);
+  }
+});
+
+  }
+
+  private class LoopingRunnable implements Runnable {
+private final Function runnable;
+private final int iterationCount;
+private CountDownLatch startLatch;
+
+public LoopingRunnable(Function runnable, int 
iterationCount,
+CountDownLatch startLatch) {
+  this.runnable = runnable;
+  this.iterationCount = iterationCount;
+  this.startLatch = startLatch;
+}
+
+@Override
+public void run() {
+  try {
+startLatch.await();
+  } catch (InterruptedException e) {
+throw new RuntimeException(e);
+  }
+  for (int i = 0; i < iterationCount; i++) {
+runnable.apply(i);
+Thread.yield();

Review comment:
   not a deal breaker. I just merged in a new dunit test for concurrent 
testing of SADD. It would be nice to have this encapsulated concurrency code in 
that test before it becomes the pattern for subsequent tests





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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [geode] dschneider-pivotal commented on a change in pull request #4973: improvement/GEODE-8002 Refactor: Extract class to encapsulate concurrent execution

2020-04-20 Thread GitBox


dschneider-pivotal commented on a change in pull request #4973:
URL: https://github.com/apache/geode/pull/4973#discussion_r411592696



##
File path: 
geode-redis/src/integrationTest/java/org/apache/geode/redis/general/LoopingThreads.java
##
@@ -0,0 +1,88 @@
+/*
+ * 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.general;
+
+import java.util.Arrays;
+import java.util.concurrent.CountDownLatch;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+public class LoopingThreads {
+  private final int iterationCount;
+  private final Function[] functions;
+
+  @SuppressWarnings("unchecked")
+  public LoopingThreads(int iterationCount,
+  Function... functions) {
+this.iterationCount = iterationCount;
+this.functions = (Function[]) functions;
+  }
+
+  public void run() {
+CountDownLatch latch = new CountDownLatch(1);
+Stream loopingThreadStream = Arrays
+.stream(functions)
+.map((r) -> new LoopingThread(r, iterationCount, latch))
+.map((t) -> {
+  t.start();
+  return t;
+});
+
+latch.countDown();
+
+loopingThreadStream.forEach(loopingThread -> {
+  try {
+loopingThread.join();
+  } catch (InterruptedException e) {
+throw new RuntimeException(e);
+  }
+});
+
+  }
+
+  private class LoopingRunnable implements Runnable {
+private final Function runnable;
+private final int iterationCount;
+private CountDownLatch startLatch;
+
+public LoopingRunnable(Function runnable, int 
iterationCount,
+CountDownLatch startLatch) {
+  this.runnable = runnable;
+  this.iterationCount = iterationCount;
+  this.startLatch = startLatch;
+}
+
+@Override
+public void run() {
+  try {
+startLatch.await();
+  } catch (InterruptedException e) {
+throw new RuntimeException(e);
+  }
+  for (int i = 0; i < iterationCount; i++) {
+runnable.apply(i);
+Thread.yield();

Review comment:
   Should the call of Thread.yield be optional? I'm not sure you would 
always want this





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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org