fix: manually pipe messages from child process sdtout/stderr (#426)
This commit is contained in:
@@ -40,6 +40,9 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<redirectTestOutputToFile>true</redirectTestOutputToFile>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.microsoft.playwright.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
// We manually copy stderr and stdout from child process as INHERIT for err/out streams
|
||||
// doesn't work well in Java Enterprise, see
|
||||
// https://github.com/microsoft/playwright-java/issues/418#issuecomment-832650650
|
||||
public class StreamRedirectThread extends Thread {
|
||||
private final InputStream from;
|
||||
private final OutputStream to;
|
||||
private volatile boolean terminated;
|
||||
|
||||
public StreamRedirectThread(InputStream from, OutputStream to) {
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
byte[] buffer = new byte[1<<14];
|
||||
try {
|
||||
while (true) {
|
||||
while (from.available() != 0) {
|
||||
int len = from.read(buffer);
|
||||
if (len != -1) {
|
||||
to.write(buffer);
|
||||
}
|
||||
}
|
||||
if (terminated) {
|
||||
break;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
}
|
||||
|
||||
public void terminateAndJoin() {
|
||||
terminated = true;
|
||||
try {
|
||||
join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user