zentol commented on a change in pull request #11839: URL: https://github.com/apache/flink/pull/11839#discussion_r415751417
########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/util/StdOutErrRedirectorTest.java ########## @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.flink.runtime.util; + +import org.apache.flink.api.java.tuple.Tuple1; +import org.apache.flink.util.TestLogger; + +import org.hamcrest.Matchers; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.List; + +import static org.apache.flink.core.testutils.CommonTestUtils.assertThrows; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; + +/** + * Unit test for StdOutErrRedirector. + */ +public class StdOutErrRedirectorTest extends TestLogger { + + private ByteArrayOutputStream byteArrayOutputStream; + private PrintStream originalStream; + + @Before + public void setUp() throws UnsupportedEncodingException { + byteArrayOutputStream = new ByteArrayOutputStream(); + originalStream = new PrintStream(byteArrayOutputStream, true, "UTF-8"); + } + + @After + public void tearDown() throws IOException { + byteArrayOutputStream.close(); + originalStream.close(); + } + + @Test + public void testNormalPrint() { + final int intMsg = 1234; + final String strMsg = "some message"; + final List<String> logContents = new ArrayList<>(); + + final PrintStream proxyStream = StdOutErrRedirector.createLoggerProxy( + msg -> { + assertThat(StdOutErrRedirector.isRedirecting(), is(true)); + logContents.add(msg); + }, + originalStream); + + assertThat(StdOutErrRedirector.isRedirecting(), is(false)); + + proxyStream.print(intMsg); + assertThat(StdOutErrRedirector.isRedirecting(), is(false)); + proxyStream.println(strMsg); + assertThat(StdOutErrRedirector.isRedirecting(), is(false)); + + assertThat(logContents.size(), is(3)); + assertThat(logContents, Matchers.contains(String.valueOf(intMsg), strMsg, System.lineSeparator())); + } + + @Test + public void testRecursivePrint() throws Exception { + final String recursive = "recursive"; + final Tuple1<PrintStream> streamTuple1 = new Tuple1<>(); Review comment: use an AtomicReference instead ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
