prevent blocking of process with large amount of errors.

This commit is contained in:
Volker Berlin 2020-06-21 13:02:58 +02:00
parent bb4ffa7842
commit 6b309dac9f
2 changed files with 10 additions and 10 deletions

View File

@ -333,7 +333,7 @@ public class WasmRule extends TemporaryFolder {
} }
Process process = processBuilder.start(); Process process = processBuilder.start();
process.waitFor(); process.waitFor();
nodeModulePath = readStream( process.getInputStream() ).trim(); // module install path nodeModulePath = readStream( process.getInputStream(), false ).trim(); // module install path
System.out.println( "node global module path: " + nodeModulePath ); System.out.println( "node global module path: " + nodeModulePath );
} }
@ -383,7 +383,7 @@ public class WasmRule extends TemporaryFolder {
Process process = processBuilder.start(); Process process = processBuilder.start();
int exitCode = process.waitFor(); int exitCode = process.waitFor();
if( exitCode != 0 ) { if( exitCode != 0 ) {
fail( readStream( process.getErrorStream() ) + "\nExit code: " + exitCode + " from: " + processBuilder.command().get( 0 ) ); fail( readStream( process.getErrorStream(), false ) + "\nExit code: " + exitCode + " from: " + processBuilder.command().get( 0 ) );
} }
} }
@ -405,7 +405,7 @@ public class WasmRule extends TemporaryFolder {
private File createScript( ScriptEngine script, String name, String placeholder, String value ) throws IOException { private File createScript( ScriptEngine script, String name, String placeholder, String value ) throws IOException {
File file = newFile( script.name() + "Test.js" ); File file = newFile( script.name() + "Test.js" );
URL scriptUrl = getClass().getResource( name ); URL scriptUrl = getClass().getResource( name );
String template = readStream( scriptUrl.openStream() ); String template = readStream( scriptUrl.openStream(), true );
template = template.replace( placeholder, value ); template = template.replace( placeholder, value );
try (FileOutputStream scriptStream = new FileOutputStream( file )) { try (FileOutputStream scriptStream = new FileOutputStream( file )) {
scriptStream.write( template.getBytes( StandardCharsets.UTF_8 ) ); scriptStream.write( template.getBytes( StandardCharsets.UTF_8 ) );
@ -572,15 +572,15 @@ public class WasmRule extends TemporaryFolder {
String errorMessage = ""; String errorMessage = "";
do { do {
if( process.getInputStream().available() > 0 ) { if( process.getInputStream().available() > 0 ) {
stdoutMessage += readStream( process.getInputStream() ); stdoutMessage += readStream( process.getInputStream(), false );
} }
if( process.getErrorStream().available() > 0 ) { if( process.getErrorStream().available() > 0 ) {
errorMessage += readStream( process.getErrorStream() ); errorMessage += readStream( process.getErrorStream(), false );
} }
} }
while( !process.waitFor( 10, TimeUnit.MILLISECONDS ) ); while( !process.waitFor( 10, TimeUnit.MILLISECONDS ) );
stdoutMessage += readStream( process.getInputStream() ); stdoutMessage += readStream( process.getInputStream(), false );
errorMessage += readStream( process.getErrorStream() ); errorMessage += readStream( process.getErrorStream(), false );
int exitCode = process.exitValue(); int exitCode = process.exitValue();
if( exitCode != 0 || !stdoutMessage.isEmpty() || !errorMessage.isEmpty() ) { if( exitCode != 0 || !stdoutMessage.isEmpty() || !errorMessage.isEmpty() ) {
System.err.println( stdoutMessage ); System.err.println( stdoutMessage );
@ -715,11 +715,11 @@ public class WasmRule extends TemporaryFolder {
* if an I/O error occurs. * if an I/O error occurs.
*/ */
@SuppressWarnings( "resource" ) @SuppressWarnings( "resource" )
public static String readStream( InputStream input ) throws IOException { public static String readStream( InputStream input, boolean all ) throws IOException {
byte[] bytes = new byte[8192]; byte[] bytes = new byte[8192];
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
int count; int count;
while( (count = input.read( bytes )) > 0 ) { while( (all || input.available() > 0) && (count = input.read( bytes )) > 0 ) {
stream.write( bytes, 0, count ); stream.write( bytes, 0, count );
} }
return new String( stream.toByteArray() ); return new String( stream.toByteArray() );

View File

@ -66,7 +66,7 @@ class Wat2Wasm {
URL url = new URL( "https://github.com/WebAssembly/wabt/releases/latest" ); URL url = new URL( "https://github.com/WebAssembly/wabt/releases/latest" );
HttpURLConnection conn = (HttpURLConnection)url.openConnection(); HttpURLConnection conn = (HttpURLConnection)url.openConnection();
InputStream input = conn.getInputStream(); InputStream input = conn.getInputStream();
String data = WasmRule.readStream( input ); String data = WasmRule.readStream( input, true );
Pattern pattern = Pattern.compile( "/WebAssembly/wabt/releases/download/[0-9.]*/wabt-[0-9.]*-" + fileName ); Pattern pattern = Pattern.compile( "/WebAssembly/wabt/releases/download/[0-9.]*/wabt-[0-9.]*-" + fileName );
Matcher matcher = pattern.matcher( data ); Matcher matcher = pattern.matcher( data );