You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by je...@apache.org on 2018/10/26 17:14:06 UTC

[pulsar] branch master updated: adding Python function instance unit test (#2850)

This is an automated email from the ASF dual-hosted git repository.

jerrypeng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 1a1cf22  adding Python function instance unit test (#2850)
1a1cf22 is described below

commit 1a1cf226a718fbaece393e8e1ed9584b59a8752b
Author: Boyang Jerry Peng <je...@gmail.com>
AuthorDate: Fri Oct 26 10:14:01 2018 -0700

    adding Python function instance unit test (#2850)
    
    * adding python function instance unittests
    
    * adding license headers
---
 bin/pulsar                                         |  2 +-
 bin/pulsar-admin                                   |  2 +-
 distribution/server/src/assemble/bin.xml           |  2 +-
 pulsar-client-cpp/run-unit-tests.sh                |  4 ++
 pulsar-functions/instance/pom.xml                  | 31 ++++++++++++
 .../src/scripts/run_python_instance_tests.sh       | 30 +++++++++++
 .../src/test/python/test_python_instance.py        | 58 ++++++++++++++++++++++
 pulsar-functions/runtime/pom.xml                   | 28 -----------
 8 files changed, 126 insertions(+), 31 deletions(-)

diff --git a/bin/pulsar b/bin/pulsar
index 9cacfe7..02a81a0 100755
--- a/bin/pulsar
+++ b/bin/pulsar
@@ -94,7 +94,7 @@ fi
 # find the python instance location
 if [ ! -f "${PY_INSTANCE_FILE}" ]; then
     # didn't find a released python instance, then search the built python instance
-    BUILT_PY_INSTANCE_FILE="${FUNCTIONS_HOME}/runtime/target/python-instance/python_instance_main.py"
+    BUILT_PY_INSTANCE_FILE="${FUNCTIONS_HOME}/instance/target/python-instance/python_instance_main.py"
     if [ -z "${BUILT_PY_INSTANCE_FILE}" ]; then
         echo "\nCouldn't find pulsar-functions python instance.";
         echo "Make sure you've run 'mvn package'\n";
diff --git a/bin/pulsar-admin b/bin/pulsar-admin
index 1a1339d..39d1239 100755
--- a/bin/pulsar-admin
+++ b/bin/pulsar-admin
@@ -130,7 +130,7 @@ fi
 # find the python instance location
 if [ ! -f "${PY_INSTANCE_FILE}" ]; then
     # didn't find a released python instance, then search the built python instance
-    BUILT_PY_INSTANCE_FILE="${FUNCTIONS_HOME}/runtime/target/python-instance/python_instance_main.py"
+    BUILT_PY_INSTANCE_FILE="${FUNCTIONS_HOME}/instance/target/python-instance/python_instance_main.py"
     if [ -f "${BUILT_PY_INSTANCE_FILE}" ]; then
         PY_INSTANCE_FILE=${BUILT_PY_INSTANCE_FILE}
     else
diff --git a/distribution/server/src/assemble/bin.xml b/distribution/server/src/assemble/bin.xml
index c9ad893..d17d4ff 100644
--- a/distribution/server/src/assemble/bin.xml
+++ b/distribution/server/src/assemble/bin.xml
@@ -44,7 +44,7 @@
       <directory>${basedir}/licenses</directory>
     </fileSet>
     <fileSet>
-      <directory>${basedir}/../../pulsar-functions/runtime/target/python-instance</directory>
+      <directory>${basedir}/../../pulsar-functions/instance/target/python-instance</directory>
       <outputDirectory>instances/python-instance</outputDirectory>
     </fileSet>
     <fileSet>
diff --git a/pulsar-client-cpp/run-unit-tests.sh b/pulsar-client-cpp/run-unit-tests.sh
index ed4b6b4..cffe002 100755
--- a/pulsar-client-cpp/run-unit-tests.sh
+++ b/pulsar-client-cpp/run-unit-tests.sh
@@ -88,6 +88,10 @@ if [ $RES -eq 0 ]; then
     python pulsar_test.py
     RES=$?
 
+    echo "---- Running Python Function Instance unit tests"
+    bash /pulsar/pulsar-functions/instance/src/scripts/run_python_instance_tests.sh
+    RES=$?
+
     popd
     popd
 
diff --git a/pulsar-functions/instance/pom.xml b/pulsar-functions/instance/pom.xml
index e206b11..1c8f3ab 100644
--- a/pulsar-functions/instance/pom.xml
+++ b/pulsar-functions/instance/pom.xml
@@ -103,4 +103,35 @@
 
   </dependencies>
 
+  <build>
+    <plugins>
+      <!-- this task is used for copy all python files together to provide a instance directory
+           for running python instance -->
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-antrun-plugin</artifactId>
+        <executions>
+          <execution>
+            <phase>compile</phase>
+            <goals>
+              <goal>run</goal>
+            </goals>
+            <configuration>
+              <tasks>
+                <echo>building python instance</echo>
+                <mkdir dir="${basedir}/target/python-instance"/>
+                <copydir src="${basedir}/src/main/python" dest="${basedir}/target/python-instance"/>
+                <mkdir dir="${basedir}/target/python-instance/pulsar"/>
+                <copydir src="${basedir}/../../pulsar-client-cpp/python/pulsar"
+                         dest="${basedir}/target/python-instance/pulsar"/>
+                <mkdir dir="${basedir}/target/python-instance/tests"/>
+                <copydir src="${basedir}/src/test/python" dest="${basedir}/target/python-instance/tests"/>
+              </tasks>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
 </project>
diff --git a/pulsar-functions/instance/src/scripts/run_python_instance_tests.sh b/pulsar-functions/instance/src/scripts/run_python_instance_tests.sh
new file mode 100644
index 0000000..9b33c24
--- /dev/null
+++ b/pulsar-functions/instance/src/scripts/run_python_instance_tests.sh
@@ -0,0 +1,30 @@
+#!/usr/bin/env 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.
+#
+
+
+# Make sure dependencies are installed
+pip install mock --user
+pip install protobuf --user
+
+CUR_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
+PULSAR_HOME=$CUR_DIR/../../../../
+
+# run instance tests
+PULSAR_HOME=${PULSAR_HOME} PYTHONPATH=${PULSAR_HOME}/pulsar-functions/instance/target/python-instance python -m unittest discover -v ${PULSAR_HOME}/pulsar-functions/instance/target/python-instance/tests
\ No newline at end of file
diff --git a/pulsar-functions/instance/src/test/python/test_python_instance.py b/pulsar-functions/instance/src/test/python/test_python_instance.py
new file mode 100644
index 0000000..0b5355f
--- /dev/null
+++ b/pulsar-functions/instance/src/test/python/test_python_instance.py
@@ -0,0 +1,58 @@
+#
+# 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.
+#
+
+
+# DEPENDENCIES:  unittest2,mock
+
+from contextimpl import ContextImpl
+from python_instance import InstanceConfig
+from mock import Mock
+
+import Function_pb2
+import log
+import os
+import unittest
+
+class TestContextImpl(unittest.TestCase):
+
+  def setUp(self):
+    log.init_logger("INFO", "foo", os.environ.get("PULSAR_HOME") + "/conf/functions-logging/console_logging_config.ini")
+
+  def test_context_publish(self):
+    instance_id = 'test_instance_id'
+    function_id = 'test_function_id'
+    function_version = 'test_function_version'
+    function_details = Function_pb2.FunctionDetails()
+    max_buffered_tuples = 100;
+    instance_config = InstanceConfig(instance_id, function_id, function_version, function_details, max_buffered_tuples)
+    logger = log.Log
+    pulsar_client = Mock()
+    producer = Mock()
+    producer.send_async = Mock(return_value=None)
+    pulsar_client.create_producer = Mock(return_value=producer)
+    user_code=__file__
+    consumers = None
+    context_impl = ContextImpl(instance_config, logger, pulsar_client, user_code, consumers)
+
+    context_impl.publish("test_topic_name", "test_message")
+
+    producer.send_async.assert_called_with("test_message", None, properties=None)
+
+
+
diff --git a/pulsar-functions/runtime/pom.xml b/pulsar-functions/runtime/pom.xml
index 843f5e3..66d245f 100644
--- a/pulsar-functions/runtime/pom.xml
+++ b/pulsar-functions/runtime/pom.xml
@@ -70,32 +70,4 @@
 
   </dependencies>
 
-  <build>
-    <plugins>
-      <!-- this task is used for copy all python files together to provide a instance directory
-           for running python instance -->
-      <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-antrun-plugin</artifactId>
-        <executions>
-          <execution>
-            <phase>package</phase>
-            <goals>
-              <goal>run</goal>
-            </goals>
-            <configuration>
-              <tasks>
-                <echo>building python instance</echo>
-                <mkdir dir="${basedir}/target/python-instance"/>
-                <copydir src="${basedir}/../instance/src/main/python" dest="${basedir}/target/python-instance"/>
-                <mkdir dir="${basedir}/target/python-instance/pulsar"/>
-                <copydir src="${basedir}/../../pulsar-client-cpp/python/pulsar" dest="${basedir}/target/python-instance/pulsar"/>
-              </tasks>
-            </configuration>
-          </execution>
-        </executions>
-      </plugin>
-    </plugins>
-  </build>
-
 </project>