Repository: incubator-ratis
Updated Branches:
  refs/heads/master 830569ed2 -> e914e7afa


RATIS-142. Test ArithmeticStateMachine with the Gauss–Legendre algorithm. 
Contributed by Tsz Wo Nicholas Sze.


Project: http://git-wip-us.apache.org/repos/asf/incubator-ratis/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ratis/commit/e914e7af
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ratis/tree/e914e7af
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ratis/diff/e914e7af

Branch: refs/heads/master
Commit: e914e7afac36a40663b58b6d33e77900e9d1ec1b
Parents: 830569e
Author: Jing Zhao <[email protected]>
Authored: Fri Nov 17 12:19:10 2017 -0800
Committer: Jing Zhao <[email protected]>
Committed: Fri Nov 17 12:19:10 2017 -0800

----------------------------------------------------------------------
 .../arithmetic/ArithmeticStateMachine.java      |   5 +-
 .../arithmetic/expression/BinaryExpression.java |  20 ++-
 .../arithmetic/expression/DoubleValue.java      |   3 +-
 .../arithmetic/expression/UnaryExpression.java  |  33 ++++-
 .../arithmetic/expression/Variable.java         |   8 ++
 .../java/org/apache/ratis/TestBatchAppend.java  |   4 +-
 .../org/apache/ratis/TestMultiRaftGroup.java    |   4 +-
 .../org/apache/ratis/TestRestartRaftPeer.java   |   4 +-
 .../ratis/examples/ParameterizedBaseTest.java   | 120 ++++++++++++++++
 .../ratis/examples/RaftExamplesTestUtil.java    |  90 ------------
 .../examples/arithmetic/TestArithmetic.java     | 144 +++++++++++++------
 .../TestRaftStateMachineException.java          |   4 +-
 .../java/org/apache/ratis/MiniRaftCluster.java  |   7 +-
 13 files changed, 297 insertions(+), 149 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/e914e7af/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
----------------------------------------------------------------------
diff --git 
a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
 
b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
index cf61df1..f7488b5 100644
--- 
a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
+++ 
b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
@@ -171,7 +171,10 @@ public class ArithmeticStateMachine extends 
BaseStateMachine {
       updateLatestTermIndex(entry.getTerm(), index);
     }
     final Expression r = Expression.Utils.double2Expression(result);
-    LOG.debug("{}: {} = {}, variables={}", index, assignment, r, variables);
+    LOG.debug("{}-{}: {} = {}", getId(), index, assignment, r);
+    if (LOG.isTraceEnabled()) {
+      LOG.trace("{}-{}: variables={}", getId(), index, variables);
+    }
     return CompletableFuture.completedFuture(Expression.Utils.toMessage(r));
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/e914e7af/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/BinaryExpression.java
----------------------------------------------------------------------
diff --git 
a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/BinaryExpression.java
 
b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/BinaryExpression.java
index 121bb7a..ca2d88e 100644
--- 
a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/BinaryExpression.java
+++ 
b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/BinaryExpression.java
@@ -20,9 +20,10 @@ package org.apache.ratis.examples.arithmetic.expression;
 import org.apache.ratis.util.Preconditions;
 
 import java.util.Map;
+import java.util.function.BinaryOperator;
 
 public class BinaryExpression implements Expression {
-  public enum Op {
+  public enum Op implements BinaryOperator<Expression> {
     ADD("+"), SUBTRACT("-"), MULT("*"), DIV("/");
 
     final String symbol;
@@ -36,6 +37,23 @@ public class BinaryExpression implements Expression {
     }
 
     @Override
+    public BinaryExpression apply(Expression left, Expression right) {
+      return new BinaryExpression(this, left, right);
+    }
+
+    public BinaryExpression apply(double left, Expression right) {
+      return apply(new DoubleValue(left), right);
+    }
+
+    public BinaryExpression apply(Expression left, double right) {
+      return apply(left, new DoubleValue(right));
+    }
+
+    public BinaryExpression apply(double left, double right) {
+      return apply(new DoubleValue(left), new DoubleValue(right));
+    }
+
+    @Override
     public String toString() {
       return symbol;
     }

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/e914e7af/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/DoubleValue.java
----------------------------------------------------------------------
diff --git 
a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/DoubleValue.java
 
b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/DoubleValue.java
index 66862f0..0d58996 100644
--- 
a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/DoubleValue.java
+++ 
b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/DoubleValue.java
@@ -56,6 +56,7 @@ public class DoubleValue implements Expression {
 
   @Override
   public String toString() {
-    return String.valueOf(value);
+    final long n = (long)value;
+    return n == value? String.valueOf(n): String.valueOf(value);
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/e914e7af/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/UnaryExpression.java
----------------------------------------------------------------------
diff --git 
a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/UnaryExpression.java
 
b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/UnaryExpression.java
index abe329d..888c87b 100644
--- 
a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/UnaryExpression.java
+++ 
b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/UnaryExpression.java
@@ -20,15 +20,26 @@ package org.apache.ratis.examples.arithmetic.expression;
 import org.apache.ratis.util.Preconditions;
 
 import java.util.Map;
+import java.util.function.BiFunction;
+import java.util.function.DoubleFunction;
+import java.util.function.UnaryOperator;
 
 public class UnaryExpression implements Expression {
-  public enum Op {
-    NEG("~"), SQRT("√");
+  static final BiFunction<Op, Expression, String> PREFIX_OP_TO_STRING = (op, 
e) -> op + "" + e;
+  static final BiFunction<Op, Expression, String> POSTFIX_OP_TO_STRING = (op, 
e) -> e + "" + op;
+
+  public enum Op implements UnaryOperator<Expression>, 
DoubleFunction<Expression> {
+    NEG("~"), SQRT("√"), SQUARE("^2", POSTFIX_OP_TO_STRING);
 
     final String symbol;
+    final BiFunction<Op, Expression, String> stringFunction;
 
     Op(String symbol) {
+      this(symbol, PREFIX_OP_TO_STRING);
+    }
+    Op(String symbol, BiFunction<Op, Expression, String> stringFunction) {
       this.symbol = symbol;
+      this.stringFunction = stringFunction;
     }
 
     byte byteValue() {
@@ -36,10 +47,24 @@ public class UnaryExpression implements Expression {
     }
 
     @Override
+    public Expression apply(Expression e) {
+      return new UnaryExpression(this, e);
+    }
+
+    @Override
+    public Expression apply(double value) {
+      return new UnaryExpression(this, new DoubleValue(value));
+    }
+
+    @Override
     public String toString() {
       return symbol;
     }
 
+    public String toString(Expression e) {
+      return stringFunction.apply(this, e);
+    }
+
     static final Op[] VALUES = Op.values();
 
     static Op valueOf(byte b) {
@@ -83,6 +108,8 @@ public class UnaryExpression implements Expression {
         return -value;
       case SQRT:
         return Math.sqrt(value);
+      case SQUARE:
+        return value * value;
       default:
         throw new AssertionError("Unexpected op value: " + op);
     }
@@ -90,6 +117,6 @@ public class UnaryExpression implements Expression {
 
   @Override
   public String toString() {
-    return op + " " + expression;
+    return op.toString(expression);
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/e914e7af/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/Variable.java
----------------------------------------------------------------------
diff --git 
a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/Variable.java
 
b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/Variable.java
index 248b24a..ddc772a 100644
--- 
a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/Variable.java
+++ 
b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/Variable.java
@@ -82,6 +82,14 @@ public class Variable implements Expression {
     return name;
   }
 
+  public AssignmentMessage assign(double value) {
+    return assign(new DoubleValue(value));
+  }
+
+  public AssignmentMessage assign(Expression e) {
+    return new AssignmentMessage(this, e);
+  }
+
   @Override
   public int toBytes(byte[] buf, int offset) {
     System.arraycopy(encoded, 0, buf, offset, encoded.length);

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/e914e7af/ratis-examples/src/test/java/org/apache/ratis/TestBatchAppend.java
----------------------------------------------------------------------
diff --git a/ratis-examples/src/test/java/org/apache/ratis/TestBatchAppend.java 
b/ratis-examples/src/test/java/org/apache/ratis/TestBatchAppend.java
index d95a233..a8c9c08 100644
--- a/ratis-examples/src/test/java/org/apache/ratis/TestBatchAppend.java
+++ b/ratis-examples/src/test/java/org/apache/ratis/TestBatchAppend.java
@@ -21,7 +21,7 @@ import org.apache.log4j.Level;
 import org.apache.ratis.RaftTestUtil.SimpleMessage;
 import org.apache.ratis.client.RaftClient;
 import org.apache.ratis.conf.RaftProperties;
-import org.apache.ratis.examples.RaftExamplesTestUtil;
+import org.apache.ratis.examples.ParameterizedBaseTest;
 import org.apache.ratis.protocol.RaftPeerId;
 import org.apache.ratis.server.RaftServerConfigKeys;
 import org.apache.ratis.server.impl.RaftServerImpl;
@@ -65,7 +65,7 @@ public class TestBatchAppend extends BaseTest {
     // set batch appending buffer size to 4KB
     RaftServerConfigKeys.Log.Appender.setBufferCapacity(prop, 
SizeInBytes.valueOf("4KB"));
 
-    return RaftExamplesTestUtil.getMiniRaftClusters(prop, 3);
+    return ParameterizedBaseTest.getMiniRaftClusters(prop, 3);
   }
 
   @Parameterized.Parameter

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/e914e7af/ratis-examples/src/test/java/org/apache/ratis/TestMultiRaftGroup.java
----------------------------------------------------------------------
diff --git 
a/ratis-examples/src/test/java/org/apache/ratis/TestMultiRaftGroup.java 
b/ratis-examples/src/test/java/org/apache/ratis/TestMultiRaftGroup.java
index 9c76bfb..b3b0998 100644
--- a/ratis-examples/src/test/java/org/apache/ratis/TestMultiRaftGroup.java
+++ b/ratis-examples/src/test/java/org/apache/ratis/TestMultiRaftGroup.java
@@ -20,7 +20,7 @@ package org.apache.ratis;
 
 import org.apache.log4j.Level;
 import org.apache.ratis.client.RaftClient;
-import org.apache.ratis.examples.RaftExamplesTestUtil;
+import org.apache.ratis.examples.ParameterizedBaseTest;
 import org.apache.ratis.examples.arithmetic.ArithmeticStateMachine;
 import org.apache.ratis.examples.arithmetic.TestArithmetic;
 import org.apache.ratis.protocol.RaftGroup;
@@ -44,7 +44,7 @@ public class TestMultiRaftGroup extends BaseTest {
 
   @Parameterized.Parameters
   public static Collection<Object[]> data() throws IOException {
-    return 
RaftExamplesTestUtil.getMiniRaftClusters(ArithmeticStateMachine.class, 0);
+    return 
ParameterizedBaseTest.getMiniRaftClusters(ArithmeticStateMachine.class, 0);
   }
 
   @Parameterized.Parameter

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/e914e7af/ratis-examples/src/test/java/org/apache/ratis/TestRestartRaftPeer.java
----------------------------------------------------------------------
diff --git 
a/ratis-examples/src/test/java/org/apache/ratis/TestRestartRaftPeer.java 
b/ratis-examples/src/test/java/org/apache/ratis/TestRestartRaftPeer.java
index b6d9eab..4b7ed40 100644
--- a/ratis-examples/src/test/java/org/apache/ratis/TestRestartRaftPeer.java
+++ b/ratis-examples/src/test/java/org/apache/ratis/TestRestartRaftPeer.java
@@ -21,7 +21,7 @@ import org.apache.log4j.Level;
 import org.apache.ratis.RaftTestUtil.SimpleMessage;
 import org.apache.ratis.client.RaftClient;
 import org.apache.ratis.conf.RaftProperties;
-import org.apache.ratis.examples.RaftExamplesTestUtil;
+import org.apache.ratis.examples.ParameterizedBaseTest;
 import org.apache.ratis.protocol.RaftPeerId;
 import org.apache.ratis.server.RaftServerConfigKeys;
 import org.apache.ratis.server.impl.RaftServerImpl;
@@ -56,7 +56,7 @@ public class TestRestartRaftPeer extends BaseTest {
     prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY,
         SimpleStateMachine4Testing.class, StateMachine.class);
     RaftServerConfigKeys.Log.setSegmentSizeMax(prop, 
SizeInBytes.valueOf("8KB"));
-    return RaftExamplesTestUtil.getMiniRaftClusters(prop, 3);
+    return ParameterizedBaseTest.getMiniRaftClusters(prop, 3);
   }
 
   @Parameterized.Parameter

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/e914e7af/ratis-examples/src/test/java/org/apache/ratis/examples/ParameterizedBaseTest.java
----------------------------------------------------------------------
diff --git 
a/ratis-examples/src/test/java/org/apache/ratis/examples/ParameterizedBaseTest.java
 
b/ratis-examples/src/test/java/org/apache/ratis/examples/ParameterizedBaseTest.java
new file mode 100644
index 0000000..9e140ac
--- /dev/null
+++ 
b/ratis-examples/src/test/java/org/apache/ratis/examples/ParameterizedBaseTest.java
@@ -0,0 +1,120 @@
+/**
+ * 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.ratis.examples;
+
+import org.apache.ratis.BaseTest;
+import org.apache.ratis.MiniRaftCluster;
+import org.apache.ratis.RaftTestUtil;
+import org.apache.ratis.conf.RaftProperties;
+import org.apache.ratis.grpc.MiniRaftClusterWithGRpc;
+import org.apache.ratis.hadooprpc.MiniRaftClusterWithHadoopRpc;
+import org.apache.ratis.netty.MiniRaftClusterWithNetty;
+import org.apache.ratis.server.simulation.MiniRaftClusterWithSimulatedRpc;
+import org.apache.ratis.statemachine.StateMachine;
+import org.junit.AfterClass;
+import org.junit.Test;
+import org.junit.runners.Parameterized;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.*;
+import java.util.concurrent.atomic.AtomicReference;
+
+public class ParameterizedBaseTest extends BaseTest {
+  public static final Logger LOG = 
LoggerFactory.getLogger(ParameterizedBaseTest.class);
+
+  /** For {@link Parameterized} test so that a cluster can be shared by 
multiple {@link Test} */
+  private static final AtomicReference<MiniRaftCluster> currentCluster = new 
AtomicReference<>();
+
+  /** Set {@link #currentCluster} to the given cluster and start it if {@link 
#currentCluster} is changed. */
+  public static void setAndStart(MiniRaftCluster cluster) throws 
InterruptedException {
+    final MiniRaftCluster previous = currentCluster.getAndSet(cluster);
+    if (previous != cluster) {
+      if (previous != null) {
+        previous.shutdown();
+      }
+
+      cluster.start();
+      RaftTestUtil.waitForLeader(cluster);
+    }
+  }
+
+  @AfterClass
+  public static void shutdownCurrentCluster() {
+    final MiniRaftCluster cluster = currentCluster.getAndSet(null);
+    if (cluster != null) {
+      cluster.shutdown();
+    }
+  }
+
+  private static void add(
+      Collection<Object[]> clusters, MiniRaftCluster.Factory factory,
+      String[] ids, RaftProperties properties)
+      throws IOException {
+    clusters.add(new Object[]{factory.newCluster(ids, properties)});
+  }
+
+  public static Collection<Object[]> getMiniRaftClusters(
+      RaftProperties prop, int clusterSize, Class<?>... clusterClasses)
+      throws IOException {
+    final List<Class<?>> classes = Arrays.asList(clusterClasses);
+    final boolean isAll = classes.isEmpty(); //empty means all
+
+    final Iterator<String[]> ids = new Iterator<String[]>() {
+      private int i = 0;
+      @Override
+      public boolean hasNext() {
+        return true;
+      }
+      @Override
+      public String[] next() {
+        return MiniRaftCluster.generateIds(clusterSize, i++*clusterSize);
+      }
+    };
+
+    final List<Object[]> clusters = new ArrayList<>();
+
+    if (isAll || classes.contains(MiniRaftClusterWithSimulatedRpc.class)) {
+      add(clusters, MiniRaftClusterWithSimulatedRpc.FACTORY, ids.next(), prop);
+    }
+    if (isAll || classes.contains(MiniRaftClusterWithHadoopRpc.class)) {
+      add(clusters, MiniRaftClusterWithHadoopRpc.FACTORY, ids.next(), prop);
+    }
+    if (isAll || classes.contains(MiniRaftClusterWithNetty.class)) {
+      add(clusters, MiniRaftClusterWithNetty.FACTORY, ids.next(), prop);
+    }
+    if (isAll || classes.contains(MiniRaftClusterWithGRpc.class)) {
+      add(clusters, MiniRaftClusterWithGRpc.FACTORY, ids.next(), prop);
+    }
+    for(int i = 0; i < clusters.size(); i++) {
+      LOG.info(i + ": " + clusters.get(i)[0].getClass().getSimpleName());
+    }
+    LOG.info("#clusters = " + clusters.size());
+    return clusters;
+  }
+
+  public static <S extends StateMachine> Collection<Object[]> 
getMiniRaftClusters(
+      Class<S> stateMachineClass, int clusterSize, Class<?>... clusterClasses)
+      throws IOException {
+    final RaftProperties prop = new RaftProperties();
+    prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY,
+        stateMachineClass, StateMachine.class);
+    return getMiniRaftClusters(prop, clusterSize, clusterClasses);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/e914e7af/ratis-examples/src/test/java/org/apache/ratis/examples/RaftExamplesTestUtil.java
----------------------------------------------------------------------
diff --git 
a/ratis-examples/src/test/java/org/apache/ratis/examples/RaftExamplesTestUtil.java
 
b/ratis-examples/src/test/java/org/apache/ratis/examples/RaftExamplesTestUtil.java
deleted file mode 100644
index 786ac48..0000000
--- 
a/ratis-examples/src/test/java/org/apache/ratis/examples/RaftExamplesTestUtil.java
+++ /dev/null
@@ -1,90 +0,0 @@
-/**
- * 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.ratis.examples;
-
-import org.apache.ratis.MiniRaftCluster;
-import org.apache.ratis.conf.RaftProperties;
-import org.apache.ratis.grpc.MiniRaftClusterWithGRpc;
-import org.apache.ratis.hadooprpc.MiniRaftClusterWithHadoopRpc;
-import org.apache.ratis.netty.MiniRaftClusterWithNetty;
-import org.apache.ratis.server.simulation.MiniRaftClusterWithSimulatedRpc;
-import org.apache.ratis.statemachine.StateMachine;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.IOException;
-import java.util.*;
-
-public class RaftExamplesTestUtil {
-  public static final Logger LOG = 
LoggerFactory.getLogger(RaftExamplesTestUtil.class);
-
-  private static void add(
-      Collection<Object[]> clusters, MiniRaftCluster.Factory factory,
-      String[] ids, RaftProperties properties)
-      throws IOException {
-    clusters.add(new Object[]{factory.newCluster(ids, properties)});
-  }
-
-  public static Collection<Object[]> getMiniRaftClusters(
-      RaftProperties prop, int clusterSize, Class<?>... clusterClasses)
-      throws IOException {
-    final List<Class<?>> classes = Arrays.asList(clusterClasses);
-    final boolean isAll = classes.isEmpty(); //empty means all
-
-    final Iterator<String[]> ids = new Iterator<String[]>() {
-      private int i = 0;
-      @Override
-      public boolean hasNext() {
-        return true;
-      }
-      @Override
-      public String[] next() {
-        return MiniRaftCluster.generateIds(clusterSize, i++*clusterSize);
-      }
-    };
-
-    final List<Object[]> clusters = new ArrayList<>();
-
-    if (isAll || classes.contains(MiniRaftClusterWithSimulatedRpc.class)) {
-      add(clusters, MiniRaftClusterWithSimulatedRpc.FACTORY, ids.next(), prop);
-    }
-    if (isAll || classes.contains(MiniRaftClusterWithHadoopRpc.class)) {
-      add(clusters, MiniRaftClusterWithHadoopRpc.FACTORY, ids.next(), prop);
-    }
-    if (isAll || classes.contains(MiniRaftClusterWithNetty.class)) {
-      add(clusters, MiniRaftClusterWithNetty.FACTORY, ids.next(), prop);
-    }
-    if (isAll || classes.contains(MiniRaftClusterWithGRpc.class)) {
-      add(clusters, MiniRaftClusterWithGRpc.FACTORY, ids.next(), prop);
-    }
-    for(int i = 0; i < clusters.size(); i++) {
-      LOG.info(i + ": " + clusters.get(i)[0].getClass().getSimpleName());
-    }
-    LOG.info("#clusters = " + clusters.size());
-    return clusters;
-  }
-
-  public static <S extends StateMachine> Collection<Object[]> 
getMiniRaftClusters(
-      Class<S> stateMachineClass, int clusterSize, Class<?>... clusterClasses)
-      throws IOException {
-    final RaftProperties prop = new RaftProperties();
-    prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY,
-        stateMachineClass, StateMachine.class);
-    return getMiniRaftClusters(prop, clusterSize, clusterClasses);
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/e914e7af/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/TestArithmetic.java
----------------------------------------------------------------------
diff --git 
a/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/TestArithmetic.java
 
b/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/TestArithmetic.java
index 45211d9..0694f6d 100644
--- 
a/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/TestArithmetic.java
+++ 
b/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/TestArithmetic.java
@@ -18,12 +18,13 @@
 package org.apache.ratis.examples.arithmetic;
 
 import org.apache.log4j.Level;
-import org.apache.ratis.BaseTest;
 import org.apache.ratis.MiniRaftCluster;
-import org.apache.ratis.RaftTestUtil;
 import org.apache.ratis.client.RaftClient;
-import org.apache.ratis.examples.RaftExamplesTestUtil;
-import org.apache.ratis.examples.arithmetic.expression.*;
+import org.apache.ratis.examples.ParameterizedBaseTest;
+import org.apache.ratis.examples.arithmetic.expression.DoubleValue;
+import org.apache.ratis.examples.arithmetic.expression.Expression;
+import org.apache.ratis.examples.arithmetic.expression.NullValue;
+import org.apache.ratis.examples.arithmetic.expression.Variable;
 import org.apache.ratis.protocol.RaftClientReply;
 import org.apache.ratis.util.LogUtils;
 import org.apache.ratis.util.Preconditions;
@@ -35,15 +36,19 @@ import org.junit.runners.Parameterized;
 import java.io.IOException;
 import java.util.Collection;
 
+import static 
org.apache.ratis.examples.arithmetic.expression.BinaryExpression.Op.*;
+import static 
org.apache.ratis.examples.arithmetic.expression.UnaryExpression.Op.SQRT;
+import static 
org.apache.ratis.examples.arithmetic.expression.UnaryExpression.Op.SQUARE;
+
 @RunWith(Parameterized.class)
-public class TestArithmetic extends BaseTest {
+public class TestArithmetic extends ParameterizedBaseTest {
   {
-    LogUtils.setLogLevel(ArithmeticStateMachine.LOG, Level.ALL);
+    LogUtils.setLogLevel(ArithmeticStateMachine.LOG, Level.DEBUG);
   }
 
   @Parameterized.Parameters
   public static Collection<Object[]> data() throws IOException {
-    return 
RaftExamplesTestUtil.getMiniRaftClusters(ArithmeticStateMachine.class, 3);
+    return getMiniRaftClusters(ArithmeticStateMachine.class, 3);
   }
 
   @Parameterized.Parameter
@@ -51,14 +56,9 @@ public class TestArithmetic extends BaseTest {
 
   @Test
   public void testPythagorean() throws Exception {
-    cluster.start();
-    try {
-      RaftTestUtil.waitForLeader(cluster);
-      try (final RaftClient client = cluster.createClient()) {
-        runTestPythagorean(client, 3, 10);
-      }
-    } finally {
-      cluster.shutdown();
+    setAndStart(cluster);
+    try (final RaftClient client = cluster.createClient()) {
+      runTestPythagorean(client, 3, 10);
     }
   }
 
@@ -70,46 +70,102 @@ public class TestArithmetic extends BaseTest {
     final Variable a = new Variable("a");
     final Variable b = new Variable("b");
     final Variable c = new Variable("c");
-    final BinaryExpression a2 = new BinaryExpression(BinaryExpression.Op.MULT, 
a, a);
-    final BinaryExpression b2 = new BinaryExpression(BinaryExpression.Op.MULT, 
b, b);
-    final BinaryExpression c2 = new BinaryExpression(BinaryExpression.Op.ADD, 
a2, b2);
-    final AssignmentMessage pythagorean = new AssignmentMessage(c,
-        new UnaryExpression(UnaryExpression.Op.SQRT, c2));
-
-    final AssignmentMessage nullA = new AssignmentMessage(a, 
NullValue.getInstance());
-    final AssignmentMessage nullB = new AssignmentMessage(b, 
NullValue.getInstance());
-    final AssignmentMessage nullC = new AssignmentMessage(c, 
NullValue.getInstance());
+    final Expression pythagorean = SQRT.apply(ADD.apply(SQUARE.apply(a), 
SQUARE.apply(b)));
 
     final int end = start + 2*count;
     for(int n = (start & 1) == 0? start + 1: start; n < end; n += 2) {
       int n2 = n*n;
       int half_n2 = n2/2;
 
-      RaftClientReply r;
-      r = client.send(new AssignmentMessage(a, new DoubleValue(n)));
-      assertRaftClientReply(r, (double)n);
-      r = client.sendReadOnly(Expression.Utils.toMessage(a2));
-      assertRaftClientReply(r, (double)n2);
-      r = client.send(new AssignmentMessage(b, new DoubleValue(half_n2)));
-      assertRaftClientReply(r, (double)half_n2);
-      r = client.sendReadOnly(Expression.Utils.toMessage(b2));
-      assertRaftClientReply(r, (double)half_n2*half_n2);
-      r = client.send(pythagorean);
-      assertRaftClientReply(r, (double)half_n2 + 1);
-
-      r = client.send(nullA);
-      assertRaftClientReply(r, null);
-      r = client.send(nullB);
-      assertRaftClientReply(r, null);
-      r = client.send(nullC);
-      assertRaftClientReply(r, null);
+      assign(client, a, n);
+      assign(client, b, half_n2);
+      assign(client, c, pythagorean, (double)half_n2 + 1);
+
+      assignNull(client, a);
+      assignNull(client, b);
+      assignNull(client, c);
+    }
+  }
+
+  @Test
+  public void testGaussLegendre() throws Exception {
+    setAndStart(cluster);
+    try (final RaftClient client = cluster.createClient()) {
+      runGaussLegendre(client);
+    }
+  }
+
+  void runGaussLegendre(RaftClient client) throws IOException {
+    defineVariable(client, "a0", 1);
+    defineVariable(client, "b0", DIV.apply(1, SQRT.apply(2)));
+    defineVariable(client, "t0", DIV.apply(1, 4));
+    defineVariable(client, "p0", 1);
+
+    double previous = 0;
+    boolean converged = false;
+    for(int i = 1; i < 8; i++) {
+      final int i_1 = i - 1;
+      final Variable a0 = new Variable("a" + i_1);
+      final Variable b0 = new Variable("b" + i_1);
+      final Variable t0 = new Variable("t" + i_1);
+      final Variable p0 = new Variable("p" + i_1);
+      final Variable a1 = defineVariable(client, "a"+i, 
DIV.apply(ADD.apply(a0, b0), 2));
+      final Variable b1 = defineVariable(client, "b"+i, 
SQRT.apply(MULT.apply(a0, b0)));
+      final Variable t1 = defineVariable(client, "t"+i, SUBTRACT.apply(t0, 
MULT.apply(p0, SQUARE.apply(SUBTRACT.apply(a0, a1)))));
+      final Variable p1 = defineVariable(client, "p"+i, MULT.apply(2, p0));
+
+      final Variable pi_i = new Variable("pi_"+i);
+      final Expression e = assign(client, pi_i, DIV.apply(SQUARE.apply(a1), 
t0));
+      final double pi = e.evaluate(null);
+
+      if (converged) {
+        Assert.assertTrue(pi == previous);
+      } else if (pi == previous) {
+        converged = true;
+      }
+      LOG.info("{} = {}, converged? {}", pi_i, pi, converged);
+      previous = pi;
     }
+    Assert.assertTrue(converged);
+  }
+
+  static Variable defineVariable(RaftClient client, String name, double value) 
throws IOException {
+    final Variable x = new Variable(name);
+    assign(client, x, value);
+    return x;
+  }
+
+  static Variable defineVariable(RaftClient client, String name, Expression e) 
throws IOException {
+    final Variable x = new Variable(name);
+    assign(client, x, e, null);
+    return x;
+  }
+
+  static Expression assign(RaftClient client, Variable x, double value) throws 
IOException {
+    return assign(client, x, new DoubleValue(value), value);
   }
 
-  static void assertRaftClientReply(RaftClientReply reply, Double expected) {
+  static void assignNull(RaftClient client, Variable x) throws IOException {
+    final Expression e = assign(client, x, NullValue.getInstance());
+    Assert.assertEquals(NullValue.getInstance(), e);
+  }
+
+  static Expression assign(RaftClient client, Variable x, Expression e) throws 
IOException {
+    return assign(client, x, e, null);
+  }
+
+  static Expression assign(RaftClient client, Variable x, Expression e, Double 
expected) throws IOException {
+    final RaftClientReply r = client.send(x.assign(e));
+    return assertRaftClientReply(r, expected);
+  }
+
+  static Expression assertRaftClientReply(RaftClientReply reply, Double 
expected) {
     Assert.assertTrue(reply.isSuccess());
     final Expression e = Expression.Utils.bytes2Expression(
         reply.getMessage().getContent().toByteArray(), 0);
-    Assert.assertEquals(expected, e.evaluate(null));
+    if (expected != null) {
+      Assert.assertEquals(expected, e.evaluate(null));
+    }
+    return e;
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/e914e7af/ratis-examples/src/test/java/org/apache/ratis/statemachine/TestRaftStateMachineException.java
----------------------------------------------------------------------
diff --git 
a/ratis-examples/src/test/java/org/apache/ratis/statemachine/TestRaftStateMachineException.java
 
b/ratis-examples/src/test/java/org/apache/ratis/statemachine/TestRaftStateMachineException.java
index d84a369..756de13 100644
--- 
a/ratis-examples/src/test/java/org/apache/ratis/statemachine/TestRaftStateMachineException.java
+++ 
b/ratis-examples/src/test/java/org/apache/ratis/statemachine/TestRaftStateMachineException.java
@@ -23,7 +23,7 @@ import org.apache.ratis.MiniRaftCluster;
 import org.apache.ratis.RaftTestUtil;
 import org.apache.ratis.client.RaftClient;
 import org.apache.ratis.client.RaftClientRpc;
-import org.apache.ratis.examples.RaftExamplesTestUtil;
+import org.apache.ratis.examples.ParameterizedBaseTest;
 import org.apache.ratis.protocol.*;
 import org.apache.ratis.server.impl.RaftServerImpl;
 import org.apache.ratis.server.impl.RaftServerTestUtil;
@@ -72,7 +72,7 @@ public class TestRaftStateMachineException extends BaseTest {
 
   @Parameterized.Parameters
   public static Collection<Object[]> data() throws IOException {
-    return RaftExamplesTestUtil.getMiniRaftClusters(
+    return ParameterizedBaseTest.getMiniRaftClusters(
         StateMachineWithException.class, 3);
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/e914e7af/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java 
b/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java
index 019e8cd..8b6f681 100644
--- a/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java
+++ b/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java
@@ -173,7 +173,12 @@ public abstract class MiniRaftCluster {
   }
 
   public void start() {
-    LOG.info("Starting " + getClass().getSimpleName());
+    LOG.info(".............................................................. 
");
+    LOG.info("... ");
+    LOG.info("...     Starting " + getClass().getSimpleName());
+    LOG.info("... ");
+    LOG.info(".............................................................. 
");
+
     initServers();
     servers.values().forEach(RaftServer::start);
   }

Reply via email to