/*
 * 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.shardingsphere.elasticjob.reg.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryOneTime;
import org.apache.curator.test.KillSession;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.fixture.EmbedTestingServer;
import org.apache.shardingsphere.elasticjob.reg.base.ElectionCandidate;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class ZookeeperElectionServiceTest {
    
    private static final String HOST_AND_PORT = "localhost:8899";
    
    private static final String ELECTION_PATH = "/election";
    
    @Mock
    private ElectionCandidate electionCandidate;
    
    @BeforeClass
    public static void init() {
        EmbedTestingServer.start();
    }
    
    @Test
    @Ignore
    public void assertContend() throws Exception {
        CuratorFramework client = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000));
        client.start();
        client.blockUntilConnected();
        ZookeeperElectionService service = new ZookeeperElectionService(HOST_AND_PORT, client, ELECTION_PATH, electionCandidate);
        service.start();
        ElectionCandidate anotherElectionCandidate = mock(ElectionCandidate.class);
        CuratorFramework anotherClient = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000));
        ZookeeperElectionService anotherService = new ZookeeperElectionService("ANOTHER_CLIENT:8899", anotherClient, ELECTION_PATH, anotherElectionCandidate);
        anotherClient.start();
        anotherClient.blockUntilConnected();
        anotherService.start();
        KillSession.kill(client.getZookeeperClient().getZooKeeper());
        service.stop();
        verify(anotherElectionCandidate).startLeadership();
    }
}

@@ -855,24 +855,52 @@
 zookeeper;%0A%0A
+import lombok.SneakyThrows;%0A
 import org.a
@@ -1002,16 +1002,83 @@
 actory;%0A
+import org.apache.curator.framework.recipes.leader.LeaderSelector;%0A
 import o
@@ -1351,33 +1351,8 @@
 ss;%0A
-import org.junit.Ignore;%0A
 impo
@@ -1470,24 +1470,57 @@
 nitRunner;%0A%0A
+import java.lang.reflect.Field;%0A%0A
 import stati
@@ -1985,20 +1985,8 @@
 est%0A
-    @Ignore%0A
     
@@ -2967,24 +2967,72 @@
 ice.stop();%0A
+        blockUtilHasLeadership(anotherService);%0A
         veri
@@ -3085,10 +3085,333 @@
 ;%0A    %7D%0A
+    %0A    @SneakyThrows%0A    private void blockUtilHasLeadership(final Object obj) %7B%0A        Field field = ZookeeperElectionService.class.getDeclaredField(%22leaderSelector%22);%0A        field.setAccessible(true);%0A        while (!((LeaderSelector) field.get(obj)).hasLeadership()) %7B%0A            Thread.sleep(100);%0A        %7D%0A    %7D%0A
 %7D%0A

Fix ZookeeperElectionServiceTest (#1245) (#1247)