New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FLINK-2480][test]Add tests for PrintSinkFunction #991
Conversation
I still cannot see the CI. |
import org.apache.flink.api.common.ExecutionConfig; | ||
import org.apache.flink.api.common.JobID; | ||
import org.apache.flink.api.common.accumulators.Accumulator; | ||
//import org.apache.flink.api.common.functions.RuntimeContext; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the imports which are commented out.
@fhueske |
Your pull request doesn't compile: https://s3.amazonaws.com/archive.travis-ci.org/jobs/74504427/log.txt |
printSink.setTargetToStandardOut(); | ||
printSink.invoke("hello world!"); | ||
|
||
assertEquals("Print to System.out", printSink.toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're not testing whether the sink actually prints to stdout. You're just checking if the toString()
methods returns the correct mode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I see, the sink actually prints is printSink.invoke("hello world!");.
And in the function invoke, it just calls stream.println(record.toString());.
Could you tell me how to get the prints from the stream?
Because I think assertEquals("hello world!", record); is just like assertEquals("hello world!", "hello world!"); and it`s not necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, ignore this.
I just have got the way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can replace stdout using System.setOut(..)
by a PrintStream
which writes to a ByteArrayOutputStream
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I did this in my new change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, didn't see that. Great!
4dbfc28
to
702b4b1
Compare
Hi, I have done a new changes. |
public void testPrintSinkStdOut(){ | ||
|
||
printStreamMock stream = new printStreamMock(out); | ||
System.setOut(stream); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you save and restore System.out
in an @After
shutdown method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, I add this in my new change.
Thanks for the updates! Looks good and I think we can merge your changes. |
@mxm |
@HuangWHWHW Can you access the CI reports now? Has the Travis team fixed the problem? |
@StephanEwen |
|
||
@After | ||
public void restoreSystemOut() { | ||
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you restore the original PrintStream here? You can save it in a variable like PrintStream original = System.out
and restore it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I add this in my new change.
} | ||
}; | ||
|
||
public PrintStream PrintStreamOriginal = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be private PrintStream PrintStreamOriginal = System.out;
. Then you don't need the null check later on and simply restore it in the @After
method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, fixed these two comments.
*/ | ||
public class PrintSinkFunctionTest<IN> extends RichSinkFunction<IN> { | ||
|
||
public PrintStream PrintStreamOriginal = System.out; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By convention, the variable name should be lower case here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AhAh......
Sorry, I get you now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed public PrintStream PrintStreamOriginal = System.out; to private PrintStream printStreamOriginal = System.out; now. :)
@mxm |
Thanks @HuangWHWHW. I'll merge your changes when Travis has completed. |
@mxm |
} | ||
} | ||
|
||
private Environment envForPrefixNull = new Environment() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can replace all this by private Environment envForPrefixNull = Mockito.mock(Environment.class)
.
Thank you @HuangWHWHW. I merged your pull request with a few minor changes (see new comments). |
Changes Unknown when pulling fde7c49 on HuangWHWHW:FLINK-2480 into ** on apache:master**. |
Test PrintSinkFunction:
set number of subtasks with 0 in runtime ctx to make prefix null.
1.set STD.OUT and test print system.out
2.set STD.ERR and test print system.err