1+ package com .datadoghq .profiler ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .IOException ;
5+ import java .io .InputStreamReader ;
6+ import java .util .ArrayList ;
7+ import java .util .List ;
8+ import java .util .concurrent .TimeUnit ;
9+ import java .util .function .Function ;
10+
11+ import static org .junit .jupiter .api .Assertions .*;
12+
13+
14+ public abstract class AbstractProcessProfilerTest {
15+ protected final boolean launch (String target , List <String > jvmArgs , String commands , Function <String , Boolean > onStdoutLine , Function <String , Boolean > onStderrLine ) throws Exception {
16+ String javaHome = System .getenv ("JAVA_TEST_HOME" );
17+ if (javaHome == null ) {
18+ javaHome = System .getenv ("JAVA_HOME" );
19+ }
20+ if (javaHome == null ) {
21+ javaHome = System .getProperty ("java.home" );
22+ }
23+ assertNotNull (javaHome );
24+
25+ List <String > args = new ArrayList <>();
26+ args .add (javaHome + "/bin/java" );
27+ args .addAll (jvmArgs );
28+ args .add ("-cp" );
29+ args .add (System .getProperty ("java.class.path" ));
30+ args .add (ExternalLauncher .class .getName ());
31+ args .add (target );
32+ if (commands != null && !commands .isEmpty ()) {
33+ args .add (commands );
34+ }
35+
36+ ProcessBuilder pb = new ProcessBuilder (args );
37+ Process p = pb .start ();
38+ Thread stdoutReader = new Thread (() -> {
39+ Function <String , Boolean > lineProcessor = onStdoutLine != null ? onStdoutLine : l -> true ;
40+ try (BufferedReader br = new BufferedReader (new InputStreamReader (p .getInputStream ()))) {
41+ String line ;
42+ while ((line = br .readLine ()) != null ) {
43+ System .out .println ("[out] " + line );
44+ if (!lineProcessor .apply (line )) {
45+ try {
46+ p .getOutputStream ().write (1 );
47+ p .getOutputStream ().flush ();
48+ } catch (IOException ignored ) {
49+ }
50+ } else {
51+ if (line .contains ("[ready]" )) {
52+ p .getOutputStream ().write (1 );
53+ p .getOutputStream ().flush ();
54+ }
55+ }
56+ }
57+ } catch (IOException ignored ) {
58+ } catch (Exception e ) {
59+ throw new RuntimeException (e );
60+ }
61+ }, "stdout-reader" );
62+ Thread stderrReader = new Thread (() -> {
63+ Function <String , Boolean > lineProcessor = onStderrLine != null ? onStderrLine : l -> true ;
64+ try (BufferedReader br = new BufferedReader (new InputStreamReader (p .getErrorStream ()))) {
65+ String line ;
66+ while ((line = br .readLine ()) != null ) {
67+ System .out .println ("[err] " + line );
68+ if (!lineProcessor .apply (line )) {
69+ try {
70+ p .getOutputStream ().write (1 );
71+ p .getOutputStream ().flush ();
72+ } catch (IOException ignored ) {
73+ }
74+ }
75+ }
76+ } catch (IOException ignored ) {
77+ } catch (Exception e ) {
78+ throw new RuntimeException (e );
79+ }
80+ }, "stderr-reader" );
81+
82+ stdoutReader .setDaemon (true );
83+ stderrReader .setDaemon (true );
84+
85+ stdoutReader .start ();
86+ stderrReader .start ();
87+
88+ boolean val = p .waitFor (10 , TimeUnit .SECONDS );
89+ if (!val ) {
90+ p .destroyForcibly ();
91+ }
92+ return val ;
93+ }
94+ }
0 commit comments