You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by te...@apache.org on 2020/10/27 15:40:20 UTC

[shardingsphere-elasticjob] branch master updated: Fix ZookeeperElectionServiceTest.assertContend failed for travis-ci (#1665)

This is an automated email from the ASF dual-hosted git repository.

technoboy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git


The following commit(s) were added to refs/heads/master by this push:
     new 5112c6e  Fix ZookeeperElectionServiceTest.assertContend failed for travis-ci (#1665)
5112c6e is described below

commit 5112c6ecf27357e4a9b924d916905dfa25f42f6d
Author: wwj <22...@qq.com>
AuthorDate: Tue Oct 27 23:32:22 2020 +0800

    Fix ZookeeperElectionServiceTest.assertContend failed for travis-ci (#1665)
    
    * Fix ZookeeperElectionServiceTest.assertContend failed for travis-ci #1660
---
 .../zookeeper/ZookeeperElectionServiceTest.java    | 29 +++++++++++++++++-----
 1 file changed, 23 insertions(+), 6 deletions(-)

diff --git a/elasticjob-infra/elasticjob-registry-center/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/ZookeeperElectionServiceTest.java b/elasticjob-infra/elasticjob-registry-center/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/ZookeeperElectionServiceTest.java
index c03b631..9634575 100644
--- a/elasticjob-infra/elasticjob-registry-center/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/ZookeeperElectionServiceTest.java
+++ b/elasticjob-infra/elasticjob-registry-center/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/ZookeeperElectionServiceTest.java
@@ -32,9 +32,12 @@ import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 
 import java.lang.reflect.Field;
+import java.util.concurrent.CountDownLatch;
+import java.util.function.Supplier;
 
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.atLeastOnce;
 
 @RunWith(MockitoJUnitRunner.class)
 public class ZookeeperElectionServiceTest {
@@ -66,16 +69,30 @@ public class ZookeeperElectionServiceTest {
         anotherService.start();
         KillSession.kill(client.getZookeeperClient().getZooKeeper());
         service.stop();
-        blockUtilHasLeadership(anotherService);
-        verify(anotherElectionCandidate).startLeadership();
+        blockUntilCondition(() -> hasLeadership(anotherService));
+        ((CountDownLatch) getFieldValue(anotherService, "leaderLatch")).countDown();
+        blockUntilCondition(() -> !hasLeadership(anotherService));
+        anotherService.stop();
+        verify(anotherElectionCandidate, atLeastOnce()).startLeadership();
+        verify(anotherElectionCandidate, atLeastOnce()).stopLeadership();
     }
     
     @SneakyThrows
-    private void blockUtilHasLeadership(final Object obj) {
-        Field field = ZookeeperElectionService.class.getDeclaredField("leaderSelector");
-        field.setAccessible(true);
-        while (!((LeaderSelector) field.get(obj)).hasLeadership()) {
+    private void blockUntilCondition(final Supplier<Boolean> condition) {
+        while (!condition.get()) {
             Thread.sleep(100);
         }
     }
+
+    @SneakyThrows
+    private boolean hasLeadership(final ZookeeperElectionService zookeeperElectionService) {
+        return ((LeaderSelector) getFieldValue(zookeeperElectionService, "leaderSelector")).hasLeadership();
+    }
+
+    @SneakyThrows
+    private Object getFieldValue(final Object target, final String fieldName) {
+        Field field = target.getClass().getDeclaredField(fieldName);
+        field.setAccessible(true);
+        return field.get(target);
+    }
 }