This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error handling of WriteableDiskCheck.
Addresses the issues that were brought up in #131
- Loading branch information
Michael Spiegel
committed
Feb 20, 2015
1 parent
1bd2f47
commit 3c8f56e
Showing
3 changed files
with
156 additions
and
6 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
hydra-data/src/main/java/com/addthis/hydra/util/StreamGobbler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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.addthis.hydra.util; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.io.PrintWriter; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
class StreamGobbler extends Thread { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(StreamGobbler.class); | ||
|
||
private final InputStream is; | ||
private final OutputStream os; | ||
private final BufferedReader reader; | ||
|
||
public StreamGobbler(String name, InputStream is, OutputStream os) { | ||
super(name); | ||
this.is = is; | ||
this.os = os; | ||
InputStreamReader isr = new InputStreamReader(is); | ||
reader = new BufferedReader(isr); | ||
} | ||
|
||
public void close() { | ||
try { | ||
reader.close(); | ||
} catch (IOException ex) { | ||
log.error("Exception while attempting to close reader: ", ex); | ||
} | ||
} | ||
|
||
public void run() { | ||
try { | ||
PrintWriter pw = new PrintWriter(os); | ||
String line; | ||
while ( (line = reader.readLine()) != null) { | ||
pw.println(line); | ||
} | ||
pw.flush(); | ||
pw.close(); | ||
} catch (IOException ex) { | ||
log.error("Exception while attempting to read from stream: ", ex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
hydra-data/src/test/java/com/addthis/hydra/util/TestStreamGobbler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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.addthis.hydra.util; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class TestStreamGobbler { | ||
|
||
@Test | ||
public void readProcessOutput() throws Exception { | ||
String[] cmdarray = {"echo", "hello world"}; | ||
Process process = Runtime.getRuntime().exec(cmdarray); | ||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
ByteArrayOutputStream error = new ByteArrayOutputStream(); | ||
StreamGobbler outputThread = new StreamGobbler(TestStreamGobbler.class.getName(), | ||
process.getInputStream(), | ||
output); | ||
StreamGobbler errorThread = new StreamGobbler(TestStreamGobbler.class.getName(), | ||
process.getErrorStream(), | ||
error); | ||
outputThread.start(); | ||
errorThread.start(); | ||
int status = process.waitFor(); | ||
outputThread.join(TimeUnit.SECONDS.toMillis(5)); | ||
errorThread.join(TimeUnit.SECONDS.toMillis(5)); | ||
outputThread.close(); | ||
errorThread.close(); | ||
assertEquals(0, status); | ||
assertEquals("hello world", output.toString().trim()); | ||
assertEquals("", error.toString().trim()); | ||
} | ||
} |