You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by km...@apache.org on 2016/12/20 21:38:59 UTC
geode git commit: GEODE-2231 Add a partitioned region example
Repository: geode
Updated Branches:
refs/heads/feature/GEODE-2231 [created] 7e37dcb04
GEODE-2231 Add a partitioned region example
Project: http://git-wip-us.apache.org/repos/asf/geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/7e37dcb0
Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/7e37dcb0
Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/7e37dcb0
Branch: refs/heads/feature/GEODE-2231
Commit: 7e37dcb044d92064c88966baa863eec553559378
Parents: 284bed9
Author: Karen Miller <km...@pivotal.io>
Authored: Mon Dec 19 16:59:55 2016 -0800
Committer: Karen Miller <km...@pivotal.io>
Committed: Tue Dec 20 13:37:22 2016 -0800
----------------------------------------------------------------------
geode-examples/README.md | 4 +-
geode-examples/partitioned/README.md | 98 +++++++++++
geode-examples/partitioned/build.gradle | 20 +++
geode-examples/partitioned/scripts/.gitignore | 2 +
geode-examples/partitioned/scripts/pidkiller.sh | 35 ++++
geode-examples/partitioned/scripts/setEnv.sh | 33 ++++
geode-examples/partitioned/scripts/startAll.sh | 45 +++++
geode-examples/partitioned/scripts/stopAll.sh | 28 ++++
.../geode/examples/partitioned/BaseClient.java | 63 +++++++
.../geode/examples/partitioned/Consumer.java | 40 +++++
.../geode/examples/partitioned/Producer.java | 40 +++++
.../examples/partitioned/ConsumerTest.java | 71 ++++++++
.../examples/partitioned/PartitionedTest.java | 164 +++++++++++++++++++
.../examples/partitioned/ProducerTest.java | 70 ++++++++
geode-examples/settings.gradle | 1 +
15 files changed, 712 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/geode/blob/7e37dcb0/geode-examples/README.md
----------------------------------------------------------------------
diff --git a/geode-examples/README.md b/geode-examples/README.md
index 6266603..04283cf 100644
--- a/geode-examples/README.md
+++ b/geode-examples/README.md
@@ -22,8 +22,8 @@ All examples:
### 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/blob/7e37dcb0/geode-examples/partitioned/README.md
----------------------------------------------------------------------
diff --git a/geode-examples/partitioned/README.md b/geode-examples/partitioned/README.md
new file mode 100644
index 0000000..af0d927
--- /dev/null
+++ b/geode-examples/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```:
+
+ $ gradle run -Pmain=Producer
+ ...
+ ...
+ INFO: Done. Inserted 50 entries.
+
+3. Run the consumer to observe that there are 50 entries in ```myRegion```:
+
+ $ gradle run -Pmain=Consumer
+ ...
+ ...
+ INFO: Done. 50 entries available on the server(s).
+
+ Note that this observation may also be made with ```gfsh```:
+
+ $ 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:
+
+ $ 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:
+
+ $ gradle run -Pmain=Consumer
+ ...
+ ...
+ INFO: Done. 25 entries available on the server(s).
+
+ Again, this observation may also be made with ```gfsh```:
+
+ $ 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. Shutdown the system:
+
+ $ scripts/stopAll.sh
+
http://git-wip-us.apache.org/repos/asf/geode/blob/7e37dcb0/geode-examples/partitioned/build.gradle
----------------------------------------------------------------------
diff --git a/geode-examples/partitioned/build.gradle b/geode-examples/partitioned/build.gradle
new file mode 100644
index 0000000..52283ec
--- /dev/null
+++ b/geode-examples/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/blob/7e37dcb0/geode-examples/partitioned/scripts/.gitignore
----------------------------------------------------------------------
diff --git a/geode-examples/partitioned/scripts/.gitignore b/geode-examples/partitioned/scripts/.gitignore
new file mode 100644
index 0000000..32f8870
--- /dev/null
+++ b/geode-examples/partitioned/scripts/.gitignore
@@ -0,0 +1,2 @@
+locator1/
+server*/
http://git-wip-us.apache.org/repos/asf/geode/blob/7e37dcb0/geode-examples/partitioned/scripts/pidkiller.sh
----------------------------------------------------------------------
diff --git a/geode-examples/partitioned/scripts/pidkiller.sh b/geode-examples/partitioned/scripts/pidkiller.sh
new file mode 100755
index 0000000..ecf8f2d
--- /dev/null
+++ b/geode-examples/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/blob/7e37dcb0/geode-examples/partitioned/scripts/setEnv.sh
----------------------------------------------------------------------
diff --git a/geode-examples/partitioned/scripts/setEnv.sh b/geode-examples/partitioned/scripts/setEnv.sh
new file mode 100755
index 0000000..e9e860e
--- /dev/null
+++ b/geode-examples/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/blob/7e37dcb0/geode-examples/partitioned/scripts/startAll.sh
----------------------------------------------------------------------
diff --git a/geode-examples/partitioned/scripts/startAll.sh b/geode-examples/partitioned/scripts/startAll.sh
new file mode 100755
index 0000000..ff2bc03
--- /dev/null
+++ b/geode-examples/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/blob/7e37dcb0/geode-examples/partitioned/scripts/stopAll.sh
----------------------------------------------------------------------
diff --git a/geode-examples/partitioned/scripts/stopAll.sh b/geode-examples/partitioned/scripts/stopAll.sh
new file mode 100755
index 0000000..a6364a8
--- /dev/null
+++ b/geode-examples/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/blob/7e37dcb0/geode-examples/partitioned/src/main/java/org/apache/geode/examples/partitioned/BaseClient.java
----------------------------------------------------------------------
diff --git a/geode-examples/partitioned/src/main/java/org/apache/geode/examples/partitioned/BaseClient.java b/geode-examples/partitioned/src/main/java/org/apache/geode/examples/partitioned/BaseClient.java
new file mode 100644
index 0000000..7eac0e0
--- /dev/null
+++ b/geode-examples/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/blob/7e37dcb0/geode-examples/partitioned/src/main/java/org/apache/geode/examples/partitioned/Consumer.java
----------------------------------------------------------------------
diff --git a/geode-examples/partitioned/src/main/java/org/apache/geode/examples/partitioned/Consumer.java b/geode-examples/partitioned/src/main/java/org/apache/geode/examples/partitioned/Consumer.java
new file mode 100644
index 0000000..acb6bd4
--- /dev/null
+++ b/geode-examples/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/blob/7e37dcb0/geode-examples/partitioned/src/main/java/org/apache/geode/examples/partitioned/Producer.java
----------------------------------------------------------------------
diff --git a/geode-examples/partitioned/src/main/java/org/apache/geode/examples/partitioned/Producer.java b/geode-examples/partitioned/src/main/java/org/apache/geode/examples/partitioned/Producer.java
new file mode 100644
index 0000000..2aeb8ff
--- /dev/null
+++ b/geode-examples/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/blob/7e37dcb0/geode-examples/partitioned/src/test/java/org/apache/geode/examples/partitioned/ConsumerTest.java
----------------------------------------------------------------------
diff --git a/geode-examples/partitioned/src/test/java/org/apache/geode/examples/partitioned/ConsumerTest.java b/geode-examples/partitioned/src/test/java/org/apache/geode/examples/partitioned/ConsumerTest.java
new file mode 100644
index 0000000..e48f1ce
--- /dev/null
+++ b/geode-examples/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/blob/7e37dcb0/geode-examples/partitioned/src/test/java/org/apache/geode/examples/partitioned/PartitionedTest.java
----------------------------------------------------------------------
diff --git a/geode-examples/partitioned/src/test/java/org/apache/geode/examples/partitioned/PartitionedTest.java b/geode-examples/partitioned/src/test/java/org/apache/geode/examples/partitioned/PartitionedTest.java
new file mode 100644
index 0000000..fd4fed4
--- /dev/null
+++ b/geode-examples/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.example.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/blob/7e37dcb0/geode-examples/partitioned/src/test/java/org/apache/geode/examples/partitioned/ProducerTest.java
----------------------------------------------------------------------
diff --git a/geode-examples/partitioned/src/test/java/org/apache/geode/examples/partitioned/ProducerTest.java b/geode-examples/partitioned/src/test/java/org/apache/geode/examples/partitioned/ProducerTest.java
new file mode 100644
index 0000000..ff8f6cf
--- /dev/null
+++ b/geode-examples/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.example.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/blob/7e37dcb0/geode-examples/settings.gradle
----------------------------------------------------------------------
diff --git a/geode-examples/settings.gradle b/geode-examples/settings.gradle
index 432a8eb..9af877b 100644
--- a/geode-examples/settings.gradle
+++ b/geode-examples/settings.gradle
@@ -17,5 +17,6 @@
rootProject.name = 'geode-examples'
include 'replicated'
+include 'partitioned'
include 'utils'