use of java.io.PrintStream in project flink by apache.
the class PrintSinkFunctionTest method testPrintSinkWithPrefix.
@Test
public void testPrintSinkWithPrefix() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream stream = new PrintStream(baos);
System.setOut(stream);
final StreamingRuntimeContext ctx = Mockito.mock(StreamingRuntimeContext.class);
Mockito.when(ctx.getNumberOfParallelSubtasks()).thenReturn(2);
Mockito.when(ctx.getIndexOfThisSubtask()).thenReturn(1);
PrintSinkFunction<String> printSink = new PrintSinkFunction<>();
printSink.setRuntimeContext(ctx);
try {
printSink.open(new Configuration());
} catch (Exception e) {
Assert.fail();
}
printSink.setTargetToStandardErr();
printSink.invoke("hello world!");
assertEquals("Print to System.err", printSink.toString());
assertEquals("2> hello world!" + line, baos.toString());
printSink.close();
stream.close();
}
use of java.io.PrintStream in project flink by apache.
the class PrintSinkFunctionTest method testPrintSinkStdOut.
@Test
public void testPrintSinkStdOut() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream stream = new PrintStream(baos);
System.setOut(stream);
final StreamingRuntimeContext ctx = Mockito.mock(StreamingRuntimeContext.class);
PrintSinkFunction<String> printSink = new PrintSinkFunction<>();
printSink.setRuntimeContext(ctx);
try {
printSink.open(new Configuration());
} catch (Exception e) {
Assert.fail();
}
printSink.setTargetToStandardOut();
printSink.invoke("hello world!");
assertEquals("Print to System.out", printSink.toString());
assertEquals("hello world!" + line, baos.toString());
printSink.close();
stream.close();
}
use of java.io.PrintStream in project flink by apache.
the class PrintSinkFunctionTest method testPrintSinkStdErr.
@Test
public void testPrintSinkStdErr() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream stream = new PrintStream(baos);
System.setOut(stream);
final StreamingRuntimeContext ctx = Mockito.mock(StreamingRuntimeContext.class);
PrintSinkFunction<String> printSink = new PrintSinkFunction<>();
printSink.setRuntimeContext(ctx);
try {
printSink.open(new Configuration());
} catch (Exception e) {
Assert.fail();
}
printSink.setTargetToStandardErr();
printSink.invoke("hello world!");
assertEquals("Print to System.err", printSink.toString());
assertEquals("hello world!" + line, baos.toString());
printSink.close();
stream.close();
}
use of java.io.PrintStream in project hadoop by apache.
the class ReflectionUtils method logThreadInfo.
/**
* Log the current thread stacks at INFO level.
* @param log the logger that logs the stack trace
* @param title a descriptive title for the call stacks
* @param minInterval the minimum time from the last
*/
public static void logThreadInfo(Log log, String title, long minInterval) {
boolean dumpStack = false;
if (log.isInfoEnabled()) {
synchronized (ReflectionUtils.class) {
long now = Time.now();
if (now - previousLogTime >= minInterval * 1000) {
previousLogTime = now;
dumpStack = true;
}
}
if (dumpStack) {
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
printThreadInfo(new PrintStream(buffer, false, "UTF-8"), title);
log.info(buffer.toString(Charset.defaultCharset().name()));
} catch (UnsupportedEncodingException ignored) {
}
}
}
}
use of java.io.PrintStream in project flink by apache.
the class TextInputFormatTest method testSimpleRead.
@Test
public void testSimpleRead() {
final String FIRST = "First line";
final String SECOND = "Second line";
try {
// create input file
File tempFile = File.createTempFile("TextInputFormatTest", "tmp");
tempFile.deleteOnExit();
tempFile.setWritable(true);
PrintStream ps = new PrintStream(tempFile);
ps.println(FIRST);
ps.println(SECOND);
ps.close();
TextInputFormat inputFormat = new TextInputFormat(new Path(tempFile.toURI().toString()));
Configuration parameters = new Configuration();
inputFormat.configure(parameters);
FileInputSplit[] splits = inputFormat.createInputSplits(1);
assertTrue("expected at least one input split", splits.length >= 1);
inputFormat.open(splits[0]);
String result = "";
assertFalse(inputFormat.reachedEnd());
result = inputFormat.nextRecord("");
assertNotNull("Expecting first record here", result);
assertEquals(FIRST, result);
assertFalse(inputFormat.reachedEnd());
result = inputFormat.nextRecord(result);
assertNotNull("Expecting second record here", result);
assertEquals(SECOND, result);
assertTrue(inputFormat.reachedEnd() || null == inputFormat.nextRecord(result));
} catch (Throwable t) {
System.err.println("test failed with exception: " + t.getMessage());
t.printStackTrace(System.err);
fail("Test erroneous");
}
}
Aggregations