You are viewing a plain text version of this content. The canonical link for it is here.
Posted to submarine-dev@hadoop.apache.org by li...@apache.org on 2019/10/18 09:25:09 UTC

[hadoop-submarine] branch master updated: SUBMARINE-247. Add test cases for Python Interpreter

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

liuxun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hadoop-submarine.git


The following commit(s) were added to refs/heads/master by this push:
     new 3249ef7  SUBMARINE-247. Add test cases for Python Interpreter
3249ef7 is described below

commit 3249ef70a2cb313c2cf5c8a3deb27e348964e791
Author: luzhonghao <lu...@163.com>
AuthorDate: Thu Oct 17 15:55:33 2019 +0800

    SUBMARINE-247. Add test cases for Python Interpreter
    
    ### What is this PR for?
    The PR aims to add some test cases for the  submarine python interpreter , including
    1.PythonInterpreter::calcOnePlusOne
    2.PythonInterpreter::close()
    3.PythonInterpreter::cancel()
    
    ### What type of PR is it?
     Improvement
    
    ### What is the Jira issue?
    https://issues.apache.org/jira/browse/SUBMARINE-247
    
    ### How should this be tested?
    * CI  https://travis-ci.org/luzhonghao/hadoop-submarine/builds/599067380
    
    ### Questions:
    * Does the licenses files need update? No
    * Is there breaking changes for older versions? No
    * Does this needs documentation? No
    
    Author: luzhonghao <lu...@163.com>
    
    Closes #52 from luzhonghao/SUBMARINE-247 and squashes the following commits:
    
    59ced10 [luzhonghao] [SUBMARINE-247].Add test cases for Python Interpreter
---
 .../interpreter/PythonInterpreterTest.java         | 86 +++++++++++++++++++---
 1 file changed, 75 insertions(+), 11 deletions(-)

diff --git a/submarine-workbench/interpreter/python-interpreter/src/test/java/org/apache/submarine/interpreter/PythonInterpreterTest.java b/submarine-workbench/interpreter/python-interpreter/src/test/java/org/apache/submarine/interpreter/PythonInterpreterTest.java
index 6565227..d897da0 100644
--- a/submarine-workbench/interpreter/python-interpreter/src/test/java/org/apache/submarine/interpreter/PythonInterpreterTest.java
+++ b/submarine-workbench/interpreter/python-interpreter/src/test/java/org/apache/submarine/interpreter/PythonInterpreterTest.java
@@ -24,35 +24,99 @@ import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static org.junit.Assert.assertEquals;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
-import java.io.IOException;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 public class PythonInterpreterTest {
   private static final Logger LOG = LoggerFactory.getLogger(PythonInterpreterTest.class);
 
-  private static PythonInterpreter pythonInterpreter = null;
+  private static PythonInterpreter pythonInterpreterForCancel = null;
+
+  private static PythonInterpreter pythonInterpreterForClose = null;
 
   @BeforeClass
   public static void setUp() {
-    pythonInterpreter = new PythonInterpreter();
-    pythonInterpreter.open();
+    pythonInterpreterForCancel = new PythonInterpreter();
+    pythonInterpreterForClose = new PythonInterpreter();
+    pythonInterpreterForCancel.open();
+    pythonInterpreterForClose.open();
   }
 
   @AfterClass
-  public static void tearDown() throws Exception {
-    if (null != pythonInterpreter) {
-      pythonInterpreter.close();
+  public static void tearDown()  {
+    if (null != pythonInterpreterForCancel) {
+      pythonInterpreterForCancel.close();
+    }
+    if (null != pythonInterpreterForClose) {
+      pythonInterpreterForClose.close();
     }
   }
 
+
   @Test
-  public void calcOnePlusOne() throws IOException {
+  public void calcOnePlusOne() {
     String code = "1+1";
-    InterpreterResult result = pythonInterpreter.interpret(code);
+    InterpreterResult result = pythonInterpreterForCancel.interpret(code);
     LOG.info("result = {}", result);
 
     assertEquals(result.code(), InterpreterResult.Code.SUCCESS);
-    assertEquals(result.message().get(0).getData(), "2\n"); // 1 + 1 = 2 + '\n'
+    // 1 + 1 = 2 + '\n'
+    assertEquals(result.message().get(0).getData(), "2\n");
+  }
+
+  private class infinityPythonJobforCancel implements Runnable {
+    @Override
+    public void run() {
+      String code = "import time\nwhile True:\n  time.sleep(1)";
+      InterpreterResult ret = null;
+      ret = pythonInterpreterForCancel.interpret(code);
+      assertNotNull(ret);
+      Pattern expectedMessage = Pattern.compile("KeyboardInterrupt");
+      Matcher m = expectedMessage.matcher(ret.message().toString());
+      assertTrue(m.find());
+    }
+  }
+
+  private class infinityPythonJobforClose implements Runnable {
+    @Override
+    public void run() {
+      String code = "import time\nwhile True:\n  time.sleep(1)";
+      pythonInterpreterForClose.interpret(code);
+    }
   }
+
+
+  @Test
+  public void testCloseIntp() throws InterruptedException {
+    assertEquals(InterpreterResult.Code.SUCCESS,
+            pythonInterpreterForClose.interpret("1+1\n").code());
+    Thread t = new Thread(new infinityPythonJobforClose());
+    t.start();
+    Thread.sleep(5000);
+    pythonInterpreterForClose.close();
+    assertTrue(t.isAlive());
+    t.join(2000);
+    assertFalse(t.isAlive());
+  }
+
+
+  @Test
+  public void testCancelIntp() throws InterruptedException {
+    assertEquals(InterpreterResult.Code.SUCCESS,
+            pythonInterpreterForCancel.interpret("1+1\n").code());
+    Thread t = new Thread(new infinityPythonJobforCancel());
+    t.start();
+    Thread.sleep(5000);
+    pythonInterpreterForCancel.cancel();
+    assertTrue(t.isAlive());
+    t.join(3000);
+    assertFalse(t.isAlive());
+  }
+
+
 }