add test to ensure that autocomplete and interpret can be call concurrently

This commit is contained in:
marc hurabielle 2019-03-20 18:14:08 +09:00
parent 409b75f0fb
commit 616f0122fe

View file

@ -27,12 +27,19 @@ import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.zeppelin.interpreter.InterpreterResult.Code;
import org.apache.zeppelin.interpreter.InterpreterResultMessage;
import org.apache.zeppelin.interpreter.LazyOpenInterpreter;
import org.apache.zeppelin.interpreter.thrift.InterpreterCompletion;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static junit.framework.TestCase.assertTrue;
@ -362,5 +369,45 @@ public class IPythonInterpreterTest extends BasePythonInterpreterTest {
assertTrue(exceptionMsg, exceptionMsg.contains("No such file or directory"));
}
}
@Test
public void testIpython_shouldNotHang_whenCallingAutoCompleteAndInterpretConcurrently()
throws InterpreterException,
InterruptedException, TimeoutException, ExecutionException {
startInterpreter(new Properties());
final String code = "import time\n"
+ "print(1)\n"
+ "time.sleep(5)\n"
+ "print(2)";
final String base = "time.";
// The goal of this test is to ensure that concurrent interpret and complete
// will not make execute hang forever.
ExecutorService pool = Executors.newFixedThreadPool(2);
FutureTask<InterpreterResult> interpretFuture =
new FutureTask(new Callable() {
@Override
public Object call() throws Exception {
return interpreter.interpret(code, getInterpreterContext());
}
});
FutureTask<List<InterpreterCompletion>> completionFuture =
new FutureTask(new Callable() {
@Override
public Object call() throws Exception {
return interpreter.completion(base, base.length(), null);
}
});
pool.execute(interpretFuture);
// we sleep to ensure that the paragraph is running
Thread.sleep(2000);
pool.execute(completionFuture);
// We ensure that running and auto completion are not hanging.
InterpreterResult res = interpretFuture.get(10000, TimeUnit.MILLISECONDS);
List<InterpreterCompletion> autoRes = completionFuture.get(1000, TimeUnit.MILLISECONDS);
assertTrue(res.code().name().equals("SUCCESS"));
assertTrue(autoRes.size() > 0);
}
}