[ 
https://issues.apache.org/jira/browse/GEODE-8595?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17214821#comment-17214821
 ] 

ASF GitHub Bot commented on GEODE-8595:
---------------------------------------

demery-pivotal commented on a change in pull request #5612:
URL: https://github.com/apache/geode/pull/5612#discussion_r505675165



##########
File path: 
geode-junit/src/main/java/org/apache/geode/test/junit/rules/RandomRule.java
##########
@@ -0,0 +1,271 @@
+/*
+ * 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.test.junit.rules;
+
+import static java.util.Arrays.asList;
+import static java.util.Objects.requireNonNull;
+import static java.util.stream.Collectors.toList;
+import static java.util.stream.StreamSupport.stream;
+
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+import org.apache.geode.test.junit.rules.serializable.SerializableStatement;
+import org.apache.geode.test.junit.rules.serializable.SerializableTestRule;
+
+@SuppressWarnings({"serial", "unused", "WeakerAccess", 
"NumericCastThatLosesPrecision"})
+public class RandomRule extends Random implements GsRandom, 
SerializableTestRule {
+
+  private final AtomicReference<Random> random = new AtomicReference<>();
+  private final long seed;
+

Review comment:
       Would it be useful to have an additional constructor that takes a 
`Random`, or a `Supplier<Random>`?

##########
File path: 
geode-junit/src/main/java/org/apache/geode/test/junit/rules/RandomRule.java
##########
@@ -0,0 +1,271 @@
+/*
+ * 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.test.junit.rules;
+
+import static java.util.Arrays.asList;
+import static java.util.Objects.requireNonNull;
+import static java.util.stream.Collectors.toList;
+import static java.util.stream.StreamSupport.stream;
+
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+import org.apache.geode.test.junit.rules.serializable.SerializableStatement;
+import org.apache.geode.test.junit.rules.serializable.SerializableTestRule;
+
+@SuppressWarnings({"serial", "unused", "WeakerAccess", 
"NumericCastThatLosesPrecision"})
+public class RandomRule extends Random implements GsRandom, 
SerializableTestRule {
+
+  private final AtomicReference<Random> random = new AtomicReference<>();
+  private final long seed;
+
+  public RandomRule() {
+    this(0);
+  }
+
+  public RandomRule(long seed) {
+    this.seed = seed;
+  }
+
+  @Override
+  public Statement apply(final Statement base, final Description description) {
+    return statement(base);
+  }
+
+  private Statement statement(final Statement base) {
+    return new SerializableStatement() {
+      @Override
+      public void evaluate() throws Throwable {
+        before();
+        try {
+          base.evaluate();
+        } finally {
+          // nothing
+        }
+      }
+    };
+  }
+
+  private void before() {
+    random.set(newRandom());
+  }
+
+  /**
+   * Returns the next pseudorandom, uniformly distributed {@code boolean} 
value from this random
+   * number generator's sequence.
+   *
+   * @return the next pseudorandom, uniformly distributed {@code boolean} 
value from this random
+   *         number generator's sequence.
+   */
+  @Override
+  public boolean nextBoolean() {
+    return next(1) == 0;
+  }
+
+  /**
+   * Returns the next pseudorandom, uniformly distributed {@code char} value 
from this random number
+   * generator's sequence There is a hack here to prevent '}' so as to 
eliminate the possibility of
+   * generating a sequence which would falsely get marked as a suspect string 
while we are matching
+   * the pattern {@code {[0-9]+}}.
+   *
+   * @return the next pseudorandom, uniformly distributed {@code char} value 
from this random number
+   *         generator's sequence.
+   */
+  @Override
+  public char nextChar() {
+    char c = (char) next(16);
+    if (c == '}') {
+      c = nextChar(); // prevent right bracket, try again
+    }
+    return c;
+  }
+
+  /**
+   * Returns the next pseudorandom, uniformly distributed {@code byte} value 
from this random number
+   * generator's sequence.
+   *
+   * @return the next pseudorandom, uniformly distributed {@code byte} value 
from this random
+   *         number generator's sequence.
+   */
+  @Override
+  public byte nextByte() {
+    return (byte) next(8);
+  }
+
+  /**
+   * Returns the next pseudorandom, uniformly distributed {@code double} value 
from this random
+   * number generator's sequence within a range from 0 to max.
+   *
+   * @param max the maximum range (inclusive) for the pseudorandom.
+   * @return the next pseudorandom, uniformly distributed {@code double} value 
from this random
+   *         number generator's sequence.
+   */
+  @Override
+  public double nextDouble(double max) {
+    return nextDouble(0.0, max);
+  }
+
+  /**
+   * Returns the next pseudorandom, uniformly distributed {@code double} value 
from this random
+   * number generator's sequence within a range from min to max.
+   *
+   * @param min the minimum range (inclusive) for the pseudorandom.
+   * @param max the maximum range (inclusive) for the pseudorandom.
+   * @return the next pseudorandom, uniformly distributed {@code double} value 
from this random
+   *         number generator's sequence.
+   */
+  @Override
+  public double nextDouble(double min, double max) {
+    return nextDouble() * (max - min) + min;
+  }
+
+  /**
+   * Returns the next pseudorandom, uniformly distributed {@code short} value 
from this random
+   * number generator's sequence.
+   *
+   * @return the next pseudorandom, uniformly distributed {@code short} value 
from this random
+   *         number generator's sequence.
+   */
+  @Override
+  public short nextShort() {
+    return (short) nextChar();
+  }
+
+  /**
+   * Returns the next pseudorandom, uniformly distributed {@code long} value 
from this random number
+   * generator's sequence within a range from 0 to max.
+   *
+   * @param max the maximum range (inclusive) for the pseudorandom.
+   * @return the next pseudorandom, uniformly distributed {@code long} value 
from this random number
+   *         generator's sequence.
+   */
+  @Override
+  public long nextLong(long max) {
+    if (max == Long.MAX_VALUE) {
+      max--;
+    }
+    return Math.abs(nextLong()) % (max + 1);
+  }
+
+  /**
+   * Returns the next pseudorandom, uniformly distributed {@code long} value 
from this random number
+   * generator's sequence within a range from min to max.
+   *
+   * @param min the minimum range (inclusive) for the pseudorandom.
+   * @param max the maximum range (inclusive) for the pseudorandom.
+   * @return the next pseudorandom, uniformly distributed {@code long} value 
from this random number
+   *         generator's sequence.
+   */
+  @Override
+  public long nextLong(long min, long max) {
+    return nextLong(max - min) + min;
+  }
+
+  /**
+   * Returns the next pseudorandom, uniformly distributed {@code int} value 
from this random number
+   * generator's sequence within a range from 0 to max (inclusive -- which is 
different from
+   * {@link Random#nextInt}).
+   *
+   * @param max the maximum range (inclusive) for the pseudorandom.
+   * @return the next pseudorandom, uniformly distributed {@code int} value 
from this random number
+   *         generator's sequence.
+   */
+  @Override
+  public int nextInt(int max) {
+    if (max == Integer.MAX_VALUE) {
+      max--;
+    }
+
+    int theNext = nextInt();
+    // Math.abs behaves badly when given min int, so avoid
+    if (theNext == Integer.MIN_VALUE) {
+      theNext = Integer.MIN_VALUE + 1;
+    }
+    return Math.abs(theNext) % (max + 1);
+  }
+
+  /**
+   * Returns the next pseudorandom, uniformly distributed {@code int} value 
from this random number
+   * generator's sequence within a range from min to max. If max < min, 
returns 0.
+   *
+   * @param min the minimum range (inclusive) for the pseudorandom.
+   * @param max the maximum range (inclusive) for the pseudorandom.
+   * @return the next pseudorandom, uniformly distributed {@code int} value 
from this random number
+   *         generator's sequence.
+   */
+  @Override
+  public int nextInt(int min, int max) {
+    if (max < min) {
+      return 0; // handle max == 0 and avoid divide-by-zero exceptions
+    }
+
+    return nextInt(max - min) + min;
+  }
+
+  /**
+   * Returns the next pseudorandom, uniformly distributed element from the 
specified iterable as
+   * indexed by this random number generator's sequence.
+   *
+   * @param iterable the range of values (inclusive) for the pseudorandom.
+   * @return the next pseudorandom, uniformly distributed element from the 
specified iterable as
+   *         indexed by this random number generator's sequence.
+   * @throws NullPointerException if iterable is null.
+   * @throws IllegalArgumentException if iterable is empty.
+   */
+  public <T> T next(Iterable<T> iterable) {
+    List<T> list = 
requireNonNulls(requireNonEmpty(stream(iterable.spliterator(), false)
+        .collect(toList())));
+    return list.get(nextInt(0, list.size() - 1));
+  }
+
+  /**
+   * Returns the next pseudorandom, uniformly distributed element from the 
specified iterable as

Review comment:
       "… from the specified var args …" or "… from the specified values …"

##########
File path: 
geode-junit/src/test/java/org/apache/geode/test/junit/rules/RandomRuleTest.java
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.test.junit.rules;
+
+import static java.util.Arrays.asList;
+import static java.util.Collections.emptyList;
+import static java.util.Collections.singleton;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.catchThrowable;
+
+import org.junit.Rule;
+import org.junit.Test;
+
+public class RandomRuleTest {
+
+  @Rule
+  public RandomRule randomRule = new RandomRule();
+
+  @Test
+  public void iterableWithOneElementReturnsThatElement() {
+    String value = randomRule.next(singleton("item"));
+
+    assertThat(value).isEqualTo("item");
+  }
+
+  @Test
+  public void nullNullIterableThrowsNullPointerException() {
+    Iterable<String> iterable = null;
+
+    Throwable thrown = catchThrowable(() -> randomRule.next(iterable));
+
+    assertThat(thrown).isInstanceOf(NullPointerException.class);
+  }
+
+  @Test
+  public void emptyIterableThrowsIllegalArgumentException() {
+    Throwable thrown = catchThrowable(() -> randomRule.next(emptyList()));
+
+    assertThat(thrown).isInstanceOf(IllegalArgumentException.class);
+  }
+
+  @Test
+  public void iterableWithTwoElementsReturnsRandomElement() {
+    String value = randomRule.next(asList("one", "two"));
+
+    assertThat(value).isIn("one", "two");
+  }
+
+  @Test
+  public void iterableWithManyElementsReturnsRandomElement() {
+    String value = randomRule.next(asList("one", "two", "three"));
+
+    assertThat(value).isIn("one", "two", "three");
+  }
+
+  @Test
+  public void varArgsWithOneElementReturnsThatElement() {
+    String value = randomRule.next("item");
+
+    assertThat(value).isEqualTo("item");
+  }
+

Review comment:
       Test for empty varargs?




----------------------------------------------------------------
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:
[email protected]


> RandomRule to provide GsRandom utilities for dunit tests
> --------------------------------------------------------
>
>                 Key: GEODE-8595
>                 URL: https://issues.apache.org/jira/browse/GEODE-8595
>             Project: Geode
>          Issue Type: Wish
>          Components: tests
>            Reporter: Kirk Lund
>            Assignee: Kirk Lund
>            Priority: Major
>              Labels: pull-request-available
>
> RandomRule to provide GsRandom utilities for dunit tests.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to