jhuan31 commented on a change in pull request #933: ZOOKEEPER-3379: De-flaky
test in Quorum Packet Metrics
URL: https://github.com/apache/zookeeper/pull/933#discussion_r286594305
##########
File path:
zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerHandlerMetricsTest.java
##########
@@ -60,36 +61,40 @@ public void setup() throws IOException {
//adding 5ms artificial delay when sending each packet
BinaryOutputArchive oa = mock(BinaryOutputArchive.class);
- doAnswer(new Answer() {
- @Override
- public Object answer(InvocationOnMock invocationOnMock) throws
Throwable {
- Thread.sleep(5);
- return null;
- }
- }).when(oa).writeRecord(any(QuorumPacket.class), Matchers.anyString());
+ doAnswer(invocationOnMock -> {Thread.sleep(5); return null;})
+ .when(oa).writeRecord(any(QuorumPacket.class),
Matchers.anyString());
+
+ BufferedOutputStream bos = mock(BufferedOutputStream.class);
+ // flush is called when all packets are sent and the queue is empty
+ doAnswer(invocationOnMock -> {allSentLatch.countDown(); return
null;}).when(bos).flush();
Review comment:
I added check for null to avoid NPE. I try to separate setup from testing
logic. Initializing the latch I think is part of the testing logic because it
might not always be appropriate to set the latch at the beginning of the test.
The place where the latch is set and the count the latch is set to depend on
what is being tested.
There is one test method in this test file so far. So I could move all the
setup into the test. But again I want to separate setup from testing logic
itself. If later new test methods are to be added and they don't share the
common setup, then we may need to refactor the setup code--but if they don't
share the common setup, they probably shouldn't be in the same test file.
----------------------------------------------------------------
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]
With regards,
Apache Git Services