[GitHub] [zookeeper] jhuan31 commented on a change in pull request #933: ZOOKEEPER-3379: De-flaky test in Quorum Packet Metrics

2019-05-22 Thread GitBox
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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [zookeeper] jhuan31 commented on a change in pull request #933: ZOOKEEPER-3379: De-flaky test in Quorum Packet Metrics

2019-05-22 Thread GitBox
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_r286590804
 
 

 ##
 File path: 
zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerHandlerMetricsTest.java
 ##
 @@ -43,6 +43,7 @@
 public class LearnerHandlerMetricsTest {
 private MockLearnerHandler learnerHandler;
 private long sid = 5;
+private CountDownLatch allSentLatch;
 
 Review comment:
   yes.  made it volatile


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [zookeeper] jhuan31 commented on a change in pull request #933: ZOOKEEPER-3379: De-flaky test in Quorum Packet Metrics

2019-05-22 Thread GitBox
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_r286590875
 
 

 ##
 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();
 
 learnerHandler = new MockLearnerHandler(socket, leader);
 learnerHandler.setOutputArchive(oa);
-learnerHandler.setBufferedOutput(mock(BufferedOutputStream.class));
+learnerHandler.setBufferedOutput(bos);
 learnerHandler.sid = sid;
 }
 
 @Test
-public void testMetrics() {
+public void testMetrics() throws InterruptedException {
 ServerMetrics.getMetrics().resetAll();
 
 //adding 1001 packets in the queue, two marker packets will be added 
since the interval is every 1000 packets
 for (int i=0; i<1001; i++) {
 learnerHandler.queuePacket(new QuorumPacket());
 }
+
+allSentLatch = new CountDownLatch(1);
+
 learnerHandler.startSendingPackets();
 
+allSentLatch.await(8, TimeUnit.SECONDS);
+
 //make sure we have enough time to send all the packets in the queue
-try {
+/*try {
 
 Review comment:
   removed. Thanks!


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:
us...@infra.apache.org


With regards,
Apache Git Services