2020package org .apache .sysds .test .usertest .pythonapi ;
2121
2222import org .apache .log4j .Level ;
23+ import org .apache .log4j .Logger ;
2324import org .apache .log4j .spi .LoggingEvent ;
2425import org .apache .sysds .api .PythonDMLScript ;
2526import org .apache .sysds .test .LoggingUtils ;
2627import org .junit .After ;
2728import org .junit .Assert ;
2829import org .junit .Before ;
2930import org .junit .Test ;
31+ import py4j .GatewayServer ;
3032
33+ import java .security .Permission ;
3134import java .util .List ;
3235
3336
3437/** Simple tests to verify startup of Python Gateway server happens without crashes */
3538public class StartupTest {
3639 private LoggingUtils .TestAppender appender ;
40+ private SecurityManager sm ;
3741
3842 @ Before
3943 public void setUp () {
4044 appender = LoggingUtils .overwrite ();
45+ sm = System .getSecurityManager ();
46+ System .setSecurityManager (new NoExitSecurityManager ());
4147 PythonDMLScript .setDMLGateWayListenerLoggerLevel (Level .ALL );
48+ Logger .getLogger (PythonDMLScript .class .getName ()).setLevel (Level .ALL );
4249 }
4350
4451 @ After
4552 public void tearDown () {
4653 LoggingUtils .reinsert (appender );
54+ System .setSecurityManager (sm );
4755 }
4856
4957 private void assertLogMessages (String ... expectedMessages ) {
@@ -87,10 +95,30 @@ public void testStartupIncorrect_5() throws Exception {
8795 PythonDMLScript .main (new String [] {"-python" , "918757" });
8896 }
8997
98+ @ Test
99+ public void testStartupIncorrect_6 () throws Exception {
100+ GatewayServer gws1 = null ;
101+ try {
102+ PythonDMLScript .main (new String []{"-python" , "4001" });
103+ gws1 = PythonDMLScript .GwS ;
104+ Thread .sleep (200 );
105+ PythonDMLScript .main (new String []{"-python" , "4001" });
106+ Thread .sleep (200 );
107+ } catch (SecurityException e ) {
108+ assertLogMessages (
109+ "GatewayServer started" ,
110+ "failed startup"
111+ );
112+ gws1 .shutdown ();
113+ }
114+ }
115+
90116 @ Test
91117 public void testStartupCorrect () throws Exception {
92- PythonDMLScript .main (new String []{"-python" , "4001 " });
118+ PythonDMLScript .main (new String []{"-python" , "4002 " });
93119 Thread .sleep (200 );
120+ PythonDMLScript script = (PythonDMLScript ) PythonDMLScript .GwS .getGateway ().getEntryPoint ();
121+ script .getConnection ();
94122 PythonDMLScript .GwS .shutdown ();
95123 Thread .sleep (200 );
96124 assertLogMessages (
@@ -100,4 +128,14 @@ public void testStartupCorrect() throws Exception {
100128 "GatewayServer stopped"
101129 );
102130 }
131+
132+ class NoExitSecurityManager extends SecurityManager {
133+ @ Override
134+ public void checkPermission (Permission perm ) { }
135+
136+ @ Override
137+ public void checkExit (int status ) {
138+ throw new SecurityException ("Intercepted exit()" );
139+ }
140+ }
103141}
0 commit comments