jerrypeng closed pull request #2850: adding Python function instance unit test
URL: https://github.com/apache/pulsar/pull/2850
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/bin/pulsar b/bin/pulsar
index 9cacfe7782..02a81a04ef 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 1a1339d8ab..39d12393b2 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 c9ad893d56..d17d4ff39f 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 ed4b6b4a1c..cffe0023b7 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 e206b111cb..1c8f3ab78e 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 0000000000..9b33c24481
--- /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 0000000000..0b5355f81f
--- /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 843f5e3826..66d245f8d6 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>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to