1919
2020import java .io .BufferedReader ;
2121import java .io .File ;
22+ import java .io .InputStream ;
2223import java .io .InputStreamReader ;
2324import java .util .HashMap ;
2425import java .util .List ;
@@ -59,29 +60,10 @@ public void testChildProcLauncher() throws Exception {
5960 .setConf (SparkLauncher .DRIVER_EXTRA_CLASSPATH , System .getProperty ("java.class.path" ))
6061 .setMainClass (SparkLauncherTestApp .class .getName ())
6162 .addAppArgs ("proc" );
62-
6363 printArgs (launcher .buildLauncherCommand ());
64-
6564 final Process app = launcher .launch ();
66- Thread stderr = new Thread (new Runnable () {
67- @ Override
68- public void run () {
69- try {
70- BufferedReader in = new BufferedReader (
71- new InputStreamReader (app .getErrorStream (), "UTF-8" ));
72- String line ;
73- while ((line = in .readLine ()) != null ) {
74- LOG .warn (line );
75- }
76- } catch (Exception e ) {
77- e .printStackTrace ();
78- }
79- }
80- });
81- stderr .setDaemon (true );
82- stderr .setName ("stderr" );
83- stderr .start ();
84-
65+ new Redirector ("stdout" , app .getInputStream ()).start ();
66+ new Redirector ("stderr" , app .getErrorStream ()).start ();
8567 assertEquals (0 , app .waitFor ());
8668 }
8769
@@ -267,9 +249,11 @@ private void printArgs(List<String> cmd) {
267249
268250 public static class SparkLauncherTestApp {
269251
270- public static void main (String [] args ) {
252+ public static void main (String [] args ) throws Exception {
271253 if (args [0 ].equals ("proc" )) {
272254 assertEquals ("bar" , System .getProperty ("foo" ));
255+ } else if (args [0 ].equals ("arg" )) {
256+ assertEquals ("newline=" , args [1 ]);
273257 } else {
274258 assertEquals ("thread" , args [0 ]);
275259 }
@@ -278,4 +262,29 @@ public static void main(String[] args) {
278262
279263 }
280264
265+ private static class Redirector extends Thread {
266+
267+ private final InputStream in ;
268+
269+ Redirector (String name , InputStream in ) {
270+ this .in = in ;
271+ setName (name );
272+ setDaemon (true );
273+ }
274+
275+ @ Override
276+ public void run () {
277+ try {
278+ BufferedReader reader = new BufferedReader (new InputStreamReader (in , "UTF-8" ));
279+ String line ;
280+ while ((line = reader .readLine ()) != null ) {
281+ LOG .warn (line );
282+ }
283+ } catch (Exception e ) {
284+ LOG .error ("Error reading process output." , e );
285+ }
286+ }
287+
288+ }
289+
281290}
0 commit comments