You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2018/08/10 14:49:02 UTC

[GitHub] aljoscha commented on a change in pull request #6538: [hotfix][streaming] Fix and simplify PrintSinkFunctionTest

aljoscha commented on a change in pull request #6538: [hotfix][streaming] Fix and simplify PrintSinkFunctionTest
URL: https://github.com/apache/flink/pull/6538#discussion_r209283937
 
 

 ##########
 File path: flink-streaming-java/src/test/java/org/apache/flink/streaming/api/functions/PrintSinkFunctionTest.java
 ##########
 @@ -20,106 +20,84 @@
 import org.apache.flink.configuration.Configuration;
 import org.apache.flink.streaming.api.functions.sink.PrintSinkFunction;
 import org.apache.flink.streaming.api.functions.sink.SinkContextUtil;
-import org.apache.flink.streaming.api.operators.StreamingRuntimeContext;
+import org.apache.flink.streaming.util.MockStreamingRuntimeContext;
 
 import org.junit.After;
-import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
-import org.mockito.Mockito;
 
 import java.io.ByteArrayOutputStream;
 import java.io.PrintStream;
 
 import static org.junit.Assert.assertEquals;
 
 /**
- * Tests for the {@link org.apache.flink.streaming.api.functions.sink.PrintSinkFunction}.
+ * Tests for the {@link PrintSinkFunction}.
  */
 public class PrintSinkFunctionTest {
 
-	public PrintStream printStreamOriginal = System.out;
-	private String line = System.lineSeparator();
+	private final PrintStream originalSystemOut = System.out;
+	private final PrintStream originalSystemErr = System.err;
 
-	@Test
-	public void testPrintSinkStdOut() throws Exception {
-		ByteArrayOutputStream baos = new ByteArrayOutputStream();
-		PrintStream stream = new PrintStream(baos);
-		System.setOut(stream);
+	private final ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
+	private final ByteArrayOutputStream arrayErrorStream = new ByteArrayOutputStream();
 
-		final StreamingRuntimeContext ctx = Mockito.mock(StreamingRuntimeContext.class);
+	private final String line = System.lineSeparator();
 
-		PrintSinkFunction<String> printSink = new PrintSinkFunction<>();
-		printSink.setRuntimeContext(ctx);
-		try {
-			printSink.open(new Configuration());
-		} catch (Exception e) {
-			Assert.fail();
+	@Before
+	public void setUp() {
+		System.setOut(new PrintStream(arrayOutputStream));
+		System.setErr(new PrintStream(arrayErrorStream));
+	}
+
+	@After
+	public void tearDown() {
+		if (System.out != originalSystemOut) {
+			System.out.close();
 		}
+		if (System.err != originalSystemErr) {
+			System.err.close();
+		}
+		System.setOut(originalSystemOut);
+		System.setErr(originalSystemErr);
+	}
+
+	@Test
+	public void testPrintSinkStdOut() throws Exception {
+		PrintSinkFunction<String> printSink = new PrintSinkFunction<>();
+		printSink.setRuntimeContext(new MockStreamingRuntimeContext(false, 1, 0));
 		printSink.setTargetToStandardOut();
+		printSink.open(new Configuration());
+
 		printSink.invoke("hello world!", SinkContextUtil.forTimestamp(0));
 
 		assertEquals("Print to System.out", printSink.toString());
-		assertEquals("hello world!" + line, baos.toString());
-
-		printSink.close();
 
 Review comment:
   we could still close the sink function

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services