geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmil...@apache.org
Subject geode-examples git commit: GEODE-2231 A partitioned region example
Date Tue, 03 Jan 2017 21:48:45 GMT
Repository: geode-examples
Updated Branches:
  refs/heads/feature/GEODE-2231 7aacbd04a -> 10d9fc375


GEODE-2231 A partitioned region example


Project: http://git-wip-us.apache.org/repos/asf/geode-examples/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode-examples/commit/10d9fc37
Tree: http://git-wip-us.apache.org/repos/asf/geode-examples/tree/10d9fc37
Diff: http://git-wip-us.apache.org/repos/asf/geode-examples/diff/10d9fc37

Branch: refs/heads/feature/GEODE-2231
Commit: 10d9fc375fa4c54e9c596d694497de0f55417907
Parents: 7aacbd0
Author: Karen Miller <kmiller@pivotal.io>
Authored: Tue Jan 3 13:47:52 2017 -0800
Committer: Karen Miller <kmiller@pivotal.io>
Committed: Tue Jan 3 13:47:52 2017 -0800

----------------------------------------------------------------------
 README.md                                       |   6 +-
 build.gradle                                    |   1 +
 gradle.properties                               |   1 +
 partitioned/README.md                           |  98 +++++++++++
 partitioned/build.gradle                        |  20 +++
 partitioned/scripts/.gitignore                  |   2 +
 partitioned/scripts/pidkiller.sh                |  35 ++++
 partitioned/scripts/setEnv.sh                   |  33 ++++
 partitioned/scripts/startAll.sh                 |  45 +++++
 partitioned/scripts/stopAll.sh                  |  28 ++++
 .../geode/examples/partitioned/BaseClient.java  |  63 +++++++
 .../geode/examples/partitioned/Consumer.java    |  40 +++++
 .../geode/examples/partitioned/Producer.java    |  40 +++++
 partitioned/src/main/main2.iml                  |   6 +
 .../examples/partitioned/ConsumerTest.java      |  71 ++++++++
 .../examples/partitioned/PartitionedTest.java   | 164 +++++++++++++++++++
 .../examples/partitioned/ProducerTest.java      |  70 ++++++++
 partitioned/src/test/test5.iml                  |   6 +
 replicated/README.md                            |   2 +-
 .../examples/replicated/ReplicatedTest.java     |   2 +-
 settings.gradle                                 |   1 +
 .../apache/geode/example/utils/ShellUtil.java   | 106 ------------
 .../apache/geode/examples/utils/ShellUtil.java  | 106 ++++++++++++
 23 files changed, 835 insertions(+), 111 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index 6266603..d7d9d53 100644
--- a/README.md
+++ b/README.md
@@ -17,13 +17,13 @@ All examples:
 ### Installation and a Tutorial for Beginners
 
 *  [How to Install](http://geode.apache.org/docs/guide/getting_started/installation/install_standalone.html)
-*  Set a `GEODE_HOME` environment variable to point to the root directory of the installation;
this directory contains `bin/`. For those that have built from source, it will be the `geode-assembly/build/install/apache-geode`
directory.
+*  Set a `GEODE_HOME` environment variable to point to the root directory of the installation;
this directory contains `bin/`. For those that have built from source, it will be the `/path/to/geode/geode-assembly/build/install/apache-geode`
directory.
 *  If desired run the tutorial: [Apache Geode in 15 minutes or Less](http://geode.apache.org/docs/guide/getting_started/15_minute_quickstart_gfsh.html)
 
 ### Basics
 
-*  [Replicated Region](replicated)
-*  Partitioned Region
+*  [Replicated Region](replicated/README.md)
+*  [Partitioned Region](partitioned/README.md)
 *  Persistence
 *  OQL (Querying)
 

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/build.gradle
----------------------------------------------------------------------
diff --git a/build.gradle b/build.gradle
index 1e638bc..2cde62e 100644
--- a/build.gradle
+++ b/build.gradle
@@ -34,6 +34,7 @@ subprojects {
         testCompile "org.mockito:mockito-core:$mockitocoreVersion"
         compile "org.apache.commons:commons-exec:$commonsExecVersion"
         compile "com.jayway.awaitility:awaitility:$awaitilityVersion"
+        runtime "org.slf4j:slf4j-log4j12:$slf4jVersion"
     }
 
     task run(type: JavaExec) {

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/gradle.properties
----------------------------------------------------------------------
diff --git a/gradle.properties b/gradle.properties
index c1739af..fc5ffd9 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -19,3 +19,4 @@ junitVersion = 4.12
 mockitocoreVersion = 1.10.19
 commonsExecVersion = 1.3
 awaitilityVersion = 1.7.0
+slf4jVersion = 1.7.22

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/partitioned/README.md
----------------------------------------------------------------------
diff --git a/partitioned/README.md b/partitioned/README.md
new file mode 100644
index 0000000..30bdf90
--- /dev/null
+++ b/partitioned/README.md
@@ -0,0 +1,98 @@
+# Geode partitioned region example
+
+This basic example demonstrates the properties of a partitioned region. 
+Two servers host a partitioned region, which has no redundancy.
+The producer puts 50 entries into the partitioned region.
+The consumer prints the number of entries in the region.
+Due to partitioning,
+the entries are distributed among the two servers hosting the region.
+Since there is no redundancy of the data within the region,
+when one of the servers goes away,
+the entries hosted within that server are also gone.
+
+This example is a simple demonstration of some basic Geode APIs,
+as well how to write tests using mocks for Geode applications.
+
+## Steps
+1. From the ```geode-examples/partitioned``` directory,
+run a script that starts a locator and two servers:
+
+        $ scripts/startAll.sh
+
+    Each of the servers hosts the partitioned region called ```myRegion```.
+
+2. Run the producer to put 50 entries into ```myRegion```:
+
+        $ ../gradlew run -Pmain=Producer
+        ...
+        ... 
+        INFO: Done. Inserted 50 entries.
+
+3. Run the consumer to observe that there are 50 entries in ```myRegion```:
+
+        $ ../gradlew run -Pmain=Consumer
+        ...
+        ...
+        INFO: Done. 50 entries available on the server(s).
+
+    Note that this observation may also be made with ```gfsh```:
+ 
+        $ $GEODE_HOME/bin/gfsh
+        ...
+        gfsh>connect
+        gfsh>describe region --name=myRegion
+        ..........................................................
+        Name            : myRegion
+        Data Policy     : partition
+        Hosting Members : server2
+                          server1
+
+        Non-Default Attributes Shared By Hosting Members  
+
+         Type  |    Name     | Value
+        ------ | ----------- | ---------
+        Region | size        | 50
+               | data-policy | PARTITION
+
+        gfsh>quit
+
+4. Kill one of the servers:
+
+        $ $GEODE_HOME/bin/gfsh
+        ...
+        gfsh>connect
+        gfsh>stop server --name=server1
+        gfsh>quit
+
+5. Run the consumer a second time, and notice that only approximately half of
+the entries are still available: 
+
+        $ ../gradlew run -Pmain=Consumer
+        ...
+        ...
+        INFO: Done. 25 entries available on the server(s).
+
+    Again, this observation may also be made with ```gfsh```:
+
+        $ $GEODE_HOME/bin/gfsh
+        ...
+        gfsh>connect
+        gfsh>describe region --name=myRegion
+        ..........................................................
+        Name            : myRegion
+        Data Policy     : partition
+        Hosting Members : server2
+
+        Non-Default Attributes Shared By Hosting Members  
+
+         Type  |    Name     | Value
+        ------ | ----------- | ---------
+        Region | size        | 25
+               | data-policy | PARTITION
+
+        gfsh>quit
+
+6. Shut down the system:
+
+        $ scripts/stopAll.sh
+

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/partitioned/build.gradle
----------------------------------------------------------------------
diff --git a/partitioned/build.gradle b/partitioned/build.gradle
new file mode 100644
index 0000000..52283ec
--- /dev/null
+++ b/partitioned/build.gradle
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+sourceSets.test {
+  resources.srcDirs = ["${projectDir}/scripts"]
+}

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/partitioned/scripts/.gitignore
----------------------------------------------------------------------
diff --git a/partitioned/scripts/.gitignore b/partitioned/scripts/.gitignore
new file mode 100644
index 0000000..32f8870
--- /dev/null
+++ b/partitioned/scripts/.gitignore
@@ -0,0 +1,2 @@
+locator1/
+server*/

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/partitioned/scripts/pidkiller.sh
----------------------------------------------------------------------
diff --git a/partitioned/scripts/pidkiller.sh b/partitioned/scripts/pidkiller.sh
new file mode 100755
index 0000000..ecf8f2d
--- /dev/null
+++ b/partitioned/scripts/pidkiller.sh
@@ -0,0 +1,35 @@
+#!/bin/bash
+#
+# 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.
+#
+# @brief Script that look for .pid files on a directory and kill those processes.
+#
+
+export DIR=$1
+
+if [ $# -eq 0 ]
+  then
+    echo "No arguments supplied. Script needs directory to look for pid files."
+    exit 1
+fi
+
+for pid in `find $DIR -name "*.pid"`
+do
+ echo "Found: $pid"
+ kill -9 `cat $pid`
+ echo "Killed."
+done
+

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/partitioned/scripts/setEnv.sh
----------------------------------------------------------------------
diff --git a/partitioned/scripts/setEnv.sh b/partitioned/scripts/setEnv.sh
new file mode 100755
index 0000000..e9e860e
--- /dev/null
+++ b/partitioned/scripts/setEnv.sh
@@ -0,0 +1,33 @@
+#!/bin/bash
+#
+# 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.
+#
+
+## check if locator port has been set otherwise set to default
+export GEODE_LOCATOR_PORT="${GEODE_LOCATOR_PORT:-10334}"
+
+## check if GEODE_HOME has been set
+: ${GEODE_HOME?"GEODE_HOME enviroment variable needs to be set"}
+
+## check if gfsh script is accessible and print version
+: ${GEODE_HOME/bin/gfsh?"gfsh doesn't seem to be available. Please check $GEODE_HOME"}
+echo "Geode version: `$GEODE_HOME/bin/gfsh version`"
+
+## prefer GEODE_HOME for finding gfsh
+export PATH=$GEODE_HOME/bin:$PATH
+
+
+: ${GEODE_LOCATOR_PORT?}

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/partitioned/scripts/startAll.sh
----------------------------------------------------------------------
diff --git a/partitioned/scripts/startAll.sh b/partitioned/scripts/startAll.sh
new file mode 100755
index 0000000..ff2bc03
--- /dev/null
+++ b/partitioned/scripts/startAll.sh
@@ -0,0 +1,45 @@
+#!/bin/bash
+#
+# 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.
+#
+
+set -e
+
+current=`pwd`
+
+cd `dirname $0`
+
+. ./setEnv.sh
+
+cd $current
+
+#export GEODE_LOCATOR_PORT="${GEODE_LOCATOR_PORT:-10334}"
+# start a locator
+gfsh start locator --name=locator1 --mcast-port=0 --port=${GEODE_LOCATOR_PORT}
+
+# start 2 servers on a random available port
+for N in {1..2}
+do
+ gfsh start server --locators=localhost[${GEODE_LOCATOR_PORT}] --name=server$N  --server-port=0
--mcast-port=0
+done
+
+# create a region using GFSH
+gfsh -e "connect --locator=localhost[${GEODE_LOCATOR_PORT}]" -e "create region --name=myRegion
--type=PARTITION"
+
+gfsh -e "connect --locator=localhost[${GEODE_LOCATOR_PORT}]" -e "list members"
+
+exit 0
+

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/partitioned/scripts/stopAll.sh
----------------------------------------------------------------------
diff --git a/partitioned/scripts/stopAll.sh b/partitioned/scripts/stopAll.sh
new file mode 100755
index 0000000..a6364a8
--- /dev/null
+++ b/partitioned/scripts/stopAll.sh
@@ -0,0 +1,28 @@
+#!/bin/bash
+#
+# 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.
+#
+set -e
+
+current=`pwd`
+
+cd `dirname $0`
+
+. ./setEnv.sh
+
+cd $current
+
+gfsh -e "connect --locator=localhost[${GEODE_LOCATOR_PORT}]" -e "shutdown --include-locators=true"

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/partitioned/src/main/java/org/apache/geode/examples/partitioned/BaseClient.java
----------------------------------------------------------------------
diff --git a/partitioned/src/main/java/org/apache/geode/examples/partitioned/BaseClient.java
b/partitioned/src/main/java/org/apache/geode/examples/partitioned/BaseClient.java
new file mode 100644
index 0000000..7eac0e0
--- /dev/null
+++ b/partitioned/src/main/java/org/apache/geode/examples/partitioned/BaseClient.java
@@ -0,0 +1,63 @@
+/*
+ * 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.examples.partitioned;
+
+import java.util.logging.Logger;
+
+import org.apache.geode.cache.Region;
+import org.apache.geode.cache.client.ClientCache;
+import org.apache.geode.cache.client.ClientCacheFactory;
+import org.apache.geode.cache.client.ClientRegionShortcut;
+
+
+public abstract class BaseClient {
+
+  static final Logger logger = Logger.getAnonymousLogger();
+  protected ClientCache clientCache;
+
+  protected void setRegion(Region region) {
+    this.region = region;
+  }
+
+  private Region region;
+  private final String locatorHost = System.getProperty("GEODE_LOCATOR_HOST", "localhost");
+  private final int locatorPort = Integer.getInteger("GEODE_LOCATOR_PORT", 10334);
+  protected static final String REGION_NAME = "myRegion";
+  static final int NUM_ENTRIES = 50;
+
+  public BaseClient() {
+    this.clientCache = getClientCache();
+  }
+
+  protected Region getRegion() {
+    if (region == null) {
+      region = getClientCache()
+              .<String, String>createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
+              .create(REGION_NAME);
+    }
+    return region;
+  }
+
+  protected ClientCache getClientCache() {
+    if (clientCache == null) {
+      clientCache = new ClientCacheFactory().addPoolLocator(locatorHost, locatorPort)
+              .set("log-level", "WARN")
+              .create();
+    }
+    return clientCache;
+  }
+}

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/partitioned/src/main/java/org/apache/geode/examples/partitioned/Consumer.java
----------------------------------------------------------------------
diff --git a/partitioned/src/main/java/org/apache/geode/examples/partitioned/Consumer.java
b/partitioned/src/main/java/org/apache/geode/examples/partitioned/Consumer.java
new file mode 100644
index 0000000..acb6bd4
--- /dev/null
+++ b/partitioned/src/main/java/org/apache/geode/examples/partitioned/Consumer.java
@@ -0,0 +1,40 @@
+/*
+ * 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.examples.partitioned;
+
+import org.apache.geode.cache.client.ClientCache;
+
+public class Consumer extends BaseClient {
+
+  public static void main(String[] args) {
+    new Consumer().countEntriesOnServer();
+  }
+
+  public Consumer() {
+  }
+
+  public Consumer(ClientCache clientCache) {
+    this.clientCache = clientCache;
+  }
+
+  public int countEntriesOnServer() {
+    int size = getRegion().keySetOnServer().size();
+    logger.info(String.format("Done. %d entries available on the server(s).", size));
+    return size;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/partitioned/src/main/java/org/apache/geode/examples/partitioned/Producer.java
----------------------------------------------------------------------
diff --git a/partitioned/src/main/java/org/apache/geode/examples/partitioned/Producer.java
b/partitioned/src/main/java/org/apache/geode/examples/partitioned/Producer.java
new file mode 100644
index 0000000..2aeb8ff
--- /dev/null
+++ b/partitioned/src/main/java/org/apache/geode/examples/partitioned/Producer.java
@@ -0,0 +1,40 @@
+/*
+ * 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.examples.partitioned;
+
+import org.apache.geode.cache.client.ClientCache;
+
+public class Producer extends BaseClient {
+
+  public static void main(String[] args) {
+    new Producer().populateRegion();
+  }
+
+  public Producer() {
+  }
+
+  public Producer(ClientCache clientCache) {
+    this.clientCache = clientCache;
+  }
+
+  public void populateRegion() {
+    for (int i=0; i < NUM_ENTRIES; i++) {
+      getRegion().put(i, "value" + i);
+    }
+    logger.info("Done. Inserted " + NUM_ENTRIES + " entries.");
+  }
+}

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/partitioned/src/main/main2.iml
----------------------------------------------------------------------
diff --git a/partitioned/src/main/main2.iml b/partitioned/src/main/main2.iml
new file mode 100644
index 0000000..19dbd15
--- /dev/null
+++ b/partitioned/src/main/main2.iml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="false">
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/partitioned/src/test/java/org/apache/geode/examples/partitioned/ConsumerTest.java
----------------------------------------------------------------------
diff --git a/partitioned/src/test/java/org/apache/geode/examples/partitioned/ConsumerTest.java
b/partitioned/src/test/java/org/apache/geode/examples/partitioned/ConsumerTest.java
new file mode 100644
index 0000000..e48f1ce
--- /dev/null
+++ b/partitioned/src/test/java/org/apache/geode/examples/partitioned/ConsumerTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.examples.partitioned;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+import java.util.Set;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import org.apache.geode.cache.Region;
+import org.apache.geode.cache.client.ClientCache;
+import org.apache.geode.cache.client.NoAvailableLocatorsException;
+
+public class ConsumerTest {
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private Consumer consumer;
+  private ClientCache clientCache = mock(ClientCache.class);
+  private Region region = mock(Region.class);
+  private Set keys = mock(Set.class);
+
+  @Before
+  public void setup() {
+    when(region.getName()).thenReturn(Consumer.REGION_NAME);
+    when(keys.size()).thenReturn(Consumer.NUM_ENTRIES);
+    when(region.keySetOnServer()).thenReturn(keys);
+    when(clientCache.getRegion(any())).thenReturn(region);
+    consumer = new Consumer(clientCache);
+    consumer.setRegion(region);
+  }
+
+  @Test
+  public void numberOfEntriesOnServerShouldMatchConsumerEntries() throws Exception {
+    assertEquals(consumer.NUM_ENTRIES, consumer.countEntriesOnServer());
+  }
+
+  @Test
+  public void numberOfEntriesShouldBeGreaterThanZero() throws Exception {
+    assertTrue(consumer.NUM_ENTRIES > 0);
+  }
+
+  @Test
+  public void countingEntriesWithoutConnectionShouldThrowNoAvailableLocatorsException() throws
Exception {
+    consumer = new Consumer();
+    expectedException.expect(NoAvailableLocatorsException.class);
+    assertEquals(consumer.NUM_ENTRIES, consumer.countEntriesOnServer());
+  }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/partitioned/src/test/java/org/apache/geode/examples/partitioned/PartitionedTest.java
----------------------------------------------------------------------
diff --git a/partitioned/src/test/java/org/apache/geode/examples/partitioned/PartitionedTest.java
b/partitioned/src/test/java/org/apache/geode/examples/partitioned/PartitionedTest.java
new file mode 100644
index 0000000..aa746a4
--- /dev/null
+++ b/partitioned/src/test/java/org/apache/geode/examples/partitioned/PartitionedTest.java
@@ -0,0 +1,164 @@
+/*
+ * 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.examples.partitioned;
+
+import static org.hamcrest.core.Is.*;
+import static org.junit.Assert.*;
+import static org.junit.Assume.*;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.DefaultExecuteResultHandler;
+import org.apache.commons.exec.ExecuteException;
+import org.apache.commons.exec.environment.EnvironmentUtils;
+import org.apache.geode.examples.utils.ShellUtil;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+/**
+ * Tests for the shell scripts of the partitioned example
+ */
+public class PartitionedTest {
+
+  //TODO: parameterize
+  public static final String GEODE_LOCATOR_PORT = "GEODE_LOCATOR_PORT=";
+  private static final String startScriptFileName = "startAll.sh";
+  private static final String stopScriptFileName = "stopAll.sh";
+  private static final String pidkillerScriptFileName = "pidkiller.sh";
+  private boolean processRunning = false;
+  private ShellUtil shell = new ShellUtil();
+  private final long scriptTimeout = TimeUnit.SECONDS.toMillis(120);
+  private static final Logger logger = Logger.getAnonymousLogger();
+
+  @Rule
+  public TemporaryFolder testFolder = new TemporaryFolder();
+
+  private int locatorPort;
+  private Map environment;
+
+  @Before
+  public void setup() throws IOException {
+    // ignores test if running on windows
+    assumeThat(System.getProperty("os.name").startsWith("Windows"), is(false));
+
+    locatorPort = getAvailablePort();
+    environment = EnvironmentUtils.getProcEnvironment();
+    EnvironmentUtils.addVariableToEnvironment(environment, GEODE_LOCATOR_PORT + locatorPort);
+    logger.fine("Locator port: " + locatorPort);
+  }
+
+  @Test
+  public void checkIfScriptsExistsAndAreExecutable() throws IOException {
+    assertTrue(shell.getFileFromClassLoader(startScriptFileName).map(x -> x.isFile()).orElse(false));
+    assertTrue(shell.getFileFromClassLoader(stopScriptFileName).map(x -> x.isFile()).orElse(false));
+  }
+
+  @Test
+  public void executeStartThenStopScript() throws InterruptedException, IOException {
+    final int exitCodeStart = executeScript(startScriptFileName);
+    assertEquals(0, exitCodeStart);
+
+    final int exitCodeStop = executeScript(stopScriptFileName);
+    assertEquals(0, exitCodeStop);
+  }
+
+  @Test
+  public void failToStopWhenNoServersAreRunning() throws InterruptedException, IOException
{
+    final int exitCode;
+
+    exitCode = executeScript(stopScriptFileName);
+    assertEquals(1, exitCode);
+  }
+
+  /**
+   * Execute the kill script that looks for pid files
+   * @throws IOException
+   * @throws InterruptedException
+   */
+  private void runKillScript() throws IOException, InterruptedException {
+    CommandLine cmdLine = CommandLine.parse(shell.getFileFromClassLoader(pidkillerScriptFileName)
+                                                 .map(x -> x.getAbsolutePath())
+                                                 .orElseThrow(IllegalArgumentException::new));
+    cmdLine.addArgument(testFolder.getRoot().getAbsolutePath());
+
+    DefaultExecuteResultHandler resultHandler = shell.execute(cmdLine, scriptTimeout, environment,
testFolder
+      .getRoot());
+    resultHandler.waitFor(scriptTimeout);
+  }
+
+  /**
+   * Given a script file name, runs the script and return the exit code.
+   * If exitCode != 0 extract and prints exception.
+   * @param scriptName
+   * @return <code>int</code> with exitCode
+   * @throws IOException
+   * @throws InterruptedException
+   */
+  private int executeScript(String scriptName) throws IOException, InterruptedException {
+    final int exitCode;
+    DefaultExecuteResultHandler resultHandler = shell.execute(scriptName, scriptTimeout,
environment, testFolder
+      .getRoot());
+    processRunning = true;
+    resultHandler.waitFor();
+
+    logger.finest(String.format("Executing %s...", scriptName));
+    exitCode = resultHandler.getExitValue();
+
+    // extract and log exception if any happened
+    if (exitCode != 0) {
+      ExecuteException executeException = resultHandler.getException();
+      logger.log(Level.SEVERE, executeException.getMessage(), executeException);
+    }
+    return exitCode;
+  }
+
+  @After
+  public void tearDown() {
+    if (processRunning) {
+      try {
+        runKillScript();
+      } catch (IOException | InterruptedException e) {
+        e.printStackTrace();
+      }
+    }
+  }
+
+  /**
+   * Get a random available port
+   * @return <code>int</code>  port number
+   */
+  private static int getAvailablePort() {
+    try (ServerSocket socket = new ServerSocket(0)) {
+      int port = socket.getLocalPort();
+      socket.close();
+      return port;
+    } catch (IOException ioex) {
+      logger.log(Level.SEVERE, ioex.getMessage(), ioex);
+    }
+    throw new IllegalStateException("No TCP/IP ports available.");
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/partitioned/src/test/java/org/apache/geode/examples/partitioned/ProducerTest.java
----------------------------------------------------------------------
diff --git a/partitioned/src/test/java/org/apache/geode/examples/partitioned/ProducerTest.java
b/partitioned/src/test/java/org/apache/geode/examples/partitioned/ProducerTest.java
new file mode 100644
index 0000000..f654b2f
--- /dev/null
+++ b/partitioned/src/test/java/org/apache/geode/examples/partitioned/ProducerTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.examples.partitioned;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.*;
+
+import java.util.Set;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import org.apache.geode.cache.Region;
+import org.apache.geode.cache.client.ClientCache;
+
+public class ProducerTest {
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private Producer producer;
+  private ClientCache clientCache = mock(ClientCache.class);
+  private Region region = mock(Region.class);
+  private Set keys = mock(Set.class);
+
+  @Before
+  public void setup() throws Exception {
+    when(region.getName()).thenReturn(Producer.REGION_NAME);
+    when(region.keySetOnServer()).thenReturn(keys);
+    when(clientCache.getRegion(any())).thenReturn(region);
+  }
+
+  @Test
+  public void populateRegionShouldReturnCorrectNumberOfEntries() throws Exception {
+    producer = new Producer(clientCache);
+    producer.setRegion(region);
+
+    producer.populateRegion();
+    verify(region, times(producer.NUM_ENTRIES)).put(any(), any());
+  }
+
+  @Test
+  public void populateWhenRegionDoesNotExistShouldThrowNullPointer() throws Exception {
+    producer = new Producer(clientCache);
+    expectedException.expect(NullPointerException.class);
+    producer.populateRegion();
+  }
+
+  @After
+  public void tearDown() {
+
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/partitioned/src/test/test5.iml
----------------------------------------------------------------------
diff --git a/partitioned/src/test/test5.iml b/partitioned/src/test/test5.iml
new file mode 100644
index 0000000..19dbd15
--- /dev/null
+++ b/partitioned/src/test/test5.iml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="false">
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/replicated/README.md
----------------------------------------------------------------------
diff --git a/replicated/README.md b/replicated/README.md
index ea923dd..2e58ea6 100644
--- a/replicated/README.md
+++ b/replicated/README.md
@@ -38,7 +38,7 @@ The producer puts 50 entries into the replicated region. The consumer prints
the
         ...
         INFO: Done. 50 entries available on the server(s).
 
-6. Shutdown the system:
+6. Shut down the system:
 
         $ scripts/stopAll.sh
 

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/replicated/src/test/java/org/apache/geode/examples/replicated/ReplicatedTest.java
----------------------------------------------------------------------
diff --git a/replicated/src/test/java/org/apache/geode/examples/replicated/ReplicatedTest.java
b/replicated/src/test/java/org/apache/geode/examples/replicated/ReplicatedTest.java
index ab84485..5a3db17 100644
--- a/replicated/src/test/java/org/apache/geode/examples/replicated/ReplicatedTest.java
+++ b/replicated/src/test/java/org/apache/geode/examples/replicated/ReplicatedTest.java
@@ -31,7 +31,7 @@ import org.apache.commons.exec.CommandLine;
 import org.apache.commons.exec.DefaultExecuteResultHandler;
 import org.apache.commons.exec.ExecuteException;
 import org.apache.commons.exec.environment.EnvironmentUtils;
-import org.apache.geode.example.utils.ShellUtil;
+import org.apache.geode.examples.utils.ShellUtil;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/settings.gradle
----------------------------------------------------------------------
diff --git a/settings.gradle b/settings.gradle
index 432a8eb..9af877b 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -17,5 +17,6 @@
 rootProject.name = 'geode-examples'
 
 include 'replicated'
+include 'partitioned'
 include 'utils'
 

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/utils/src/main/java/org/apache/geode/example/utils/ShellUtil.java
----------------------------------------------------------------------
diff --git a/utils/src/main/java/org/apache/geode/example/utils/ShellUtil.java b/utils/src/main/java/org/apache/geode/example/utils/ShellUtil.java
deleted file mode 100644
index c5290b8..0000000
--- a/utils/src/main/java/org/apache/geode/example/utils/ShellUtil.java
+++ /dev/null
@@ -1,106 +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.geode.example.utils;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.URL;
-import java.util.Map;
-import java.util.Optional;
-
-import org.apache.commons.exec.CommandLine;
-import org.apache.commons.exec.DefaultExecuteResultHandler;
-import org.apache.commons.exec.DefaultExecutor;
-import org.apache.commons.exec.ExecuteWatchdog;
-import org.apache.commons.exec.PumpStreamHandler;
-import org.apache.commons.exec.ShutdownHookProcessDestroyer;
-
-/**
- * Utility class for executing shell commands using Apache commons-exec
- */
-public class ShellUtil {
-
-  private final ClassLoader classLoader = getClass().getClassLoader();
-
-  public Optional<File> getFileFromClassLoader(String fileName) {
-    URL resourceURL = classLoader.getResource(fileName);
-    return (resourceURL == null) ? Optional.empty() : Optional.of(new File(resourceURL.getFile()));
-  }
-
-  public CommandLine parseCommandLine(String fileName) {
-    return getFileFromClassLoader(fileName).map(file -> CommandLine.parse(file.getAbsolutePath()))
-                                           .orElseThrow(IllegalArgumentException::new);
-  }
-
-  public DefaultExecuteResultHandler execute(CommandLine cmdLine, long timeout, Map environment,
File dir) throws IOException {
-    ExecutorTemplate exampleTestExecutor = new ExecutorTemplate(timeout, dir).invoke();
-    DefaultExecutor executor = exampleTestExecutor.getExecutor();
-    DefaultExecuteResultHandler resultHandler = exampleTestExecutor.getResultHandler();
-    executor.execute(cmdLine, environment, resultHandler);
-
-    return resultHandler;
-
-  }
-
-  public DefaultExecuteResultHandler execute(String fileName, long timeout, Map environment,
File dir) throws IOException {
-    ExecutorTemplate exampleTestExecutor = new ExecutorTemplate(timeout, dir).invoke();
-    DefaultExecutor executor = exampleTestExecutor.getExecutor();
-    DefaultExecuteResultHandler resultHandler = exampleTestExecutor.getResultHandler();
-    executor.execute(parseCommandLine(fileName), environment, resultHandler);
-
-    return resultHandler;
-  }
-
-  /**
-   * Executor template for common scenarios
-   */
-  private static class ExecutorTemplate {
-
-    private final long timeout;
-    private final File dir;
-    private DefaultExecutor executor;
-    private DefaultExecuteResultHandler resultHandler;
-
-    public ExecutorTemplate(final long timeout, final File dir) {
-      this.timeout = timeout;
-      this.dir = dir;
-    }
-
-    public DefaultExecutor getExecutor() {
-      return executor;
-    }
-
-    public DefaultExecuteResultHandler getResultHandler() {
-      return resultHandler;
-    }
-
-    public ExecutorTemplate invoke() {
-      executor = new DefaultExecutor();
-      ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
-      executor.setWatchdog(watchdog);
-
-      PumpStreamHandler psh = new PumpStreamHandler(System.out, System.err);
-      executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
-      executor.setStreamHandler(psh);
-      executor.setWorkingDirectory(dir);
-
-      resultHandler = new DefaultExecuteResultHandler();
-      return this;
-    }
-  }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/10d9fc37/utils/src/main/java/org/apache/geode/examples/utils/ShellUtil.java
----------------------------------------------------------------------
diff --git a/utils/src/main/java/org/apache/geode/examples/utils/ShellUtil.java b/utils/src/main/java/org/apache/geode/examples/utils/ShellUtil.java
new file mode 100644
index 0000000..58d59f9
--- /dev/null
+++ b/utils/src/main/java/org/apache/geode/examples/utils/ShellUtil.java
@@ -0,0 +1,106 @@
+/*
+ * 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.examples.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Map;
+import java.util.Optional;
+
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.DefaultExecuteResultHandler;
+import org.apache.commons.exec.DefaultExecutor;
+import org.apache.commons.exec.ExecuteWatchdog;
+import org.apache.commons.exec.PumpStreamHandler;
+import org.apache.commons.exec.ShutdownHookProcessDestroyer;
+
+/**
+ * Utility class for executing shell commands using Apache commons-exec
+ */
+public class ShellUtil {
+
+  private final ClassLoader classLoader = getClass().getClassLoader();
+
+  public Optional<File> getFileFromClassLoader(String fileName) {
+    URL resourceURL = classLoader.getResource(fileName);
+    return (resourceURL == null) ? Optional.empty() : Optional.of(new File(resourceURL.getFile()));
+  }
+
+  public CommandLine parseCommandLine(String fileName) {
+    return getFileFromClassLoader(fileName).map(file -> CommandLine.parse(file.getAbsolutePath()))
+                                           .orElseThrow(IllegalArgumentException::new);
+  }
+
+  public DefaultExecuteResultHandler execute(CommandLine cmdLine, long timeout, Map environment,
File dir) throws IOException {
+    ExecutorTemplate exampleTestExecutor = new ExecutorTemplate(timeout, dir).invoke();
+    DefaultExecutor executor = exampleTestExecutor.getExecutor();
+    DefaultExecuteResultHandler resultHandler = exampleTestExecutor.getResultHandler();
+    executor.execute(cmdLine, environment, resultHandler);
+
+    return resultHandler;
+
+  }
+
+  public DefaultExecuteResultHandler execute(String fileName, long timeout, Map environment,
File dir) throws IOException {
+    ExecutorTemplate exampleTestExecutor = new ExecutorTemplate(timeout, dir).invoke();
+    DefaultExecutor executor = exampleTestExecutor.getExecutor();
+    DefaultExecuteResultHandler resultHandler = exampleTestExecutor.getResultHandler();
+    executor.execute(parseCommandLine(fileName), environment, resultHandler);
+
+    return resultHandler;
+  }
+
+  /**
+   * Executor template for common scenarios
+   */
+  private static class ExecutorTemplate {
+
+    private final long timeout;
+    private final File dir;
+    private DefaultExecutor executor;
+    private DefaultExecuteResultHandler resultHandler;
+
+    public ExecutorTemplate(final long timeout, final File dir) {
+      this.timeout = timeout;
+      this.dir = dir;
+    }
+
+    public DefaultExecutor getExecutor() {
+      return executor;
+    }
+
+    public DefaultExecuteResultHandler getResultHandler() {
+      return resultHandler;
+    }
+
+    public ExecutorTemplate invoke() {
+      executor = new DefaultExecutor();
+      ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
+      executor.setWatchdog(watchdog);
+
+      PumpStreamHandler psh = new PumpStreamHandler(System.out, System.err);
+      executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
+      executor.setStreamHandler(psh);
+      executor.setWorkingDirectory(dir);
+
+      resultHandler = new DefaultExecuteResultHandler();
+      return this;
+    }
+  }
+}
\ No newline at end of file


Mime
View raw message