package org.apache.helix.integration;

import org.apache.helix.HelixDataAccessor;
import org.apache.helix.HelixManager;
import org.apache.helix.TestHelper;
import org.apache.helix.TestHelper.StartCMResult;
import org.apache.helix.manager.zk.ZKHelixManager;
import org.apache.helix.model.ConfigScope;
import org.apache.helix.model.IdealState.IdealStateModeProperty;
import org.apache.helix.model.builder.ConfigScopeBuilder;
import org.testng.Assert;
import org.testng.annotations.Test;

/*
 * 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.
 */

public class TestInstanceAutoJoin extends ZkStandAloneCMTestBase
{
  String db2 = TEST_DB+"2";
  @Test
  public void testInstanceAutoJoin() throws Exception
  {
    String instanceName = PARTICIPANT_PREFIX + "_" + (START_PORT + 0);
    HelixManager manager = _startCMResultMap.get(instanceName)._manager;
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    
    _setupTool.addResourceToCluster(CLUSTER_NAME, db2, 60, "OnlineOffline", IdealStateModeProperty.AUTO_REBALANCE+"");
    

    _setupTool.rebalanceStorageCluster(CLUSTER_NAME, db2, 1);
    String instance2 = "localhost_279699";
    StartCMResult result = TestHelper.startDummyProcess(ZK_ADDR, CLUSTER_NAME, instance2);
    
    
    Thread.sleep(500);
    Assert.assertFalse(result._thread.isAlive());
    Assert.assertTrue(null == manager.getHelixDataAccessor().getProperty(accessor.keyBuilder().liveInstance(instance2)));

    ConfigScope scope =
        new ConfigScopeBuilder().forCluster(CLUSTER_NAME)
                                .build();
    
    manager.getConfigAccessor().set(scope, ZKHelixManager.ALLOW_PARTICIPANT_AUTO_JOIN, "true");
    
    result =
        TestHelper.startDummyProcess(ZK_ADDR, CLUSTER_NAME, instance2);
    
    StartCMResult result2 =
        TestHelper.startDummyProcess(ZK_ADDR, CLUSTER_NAME, instance2);
    
    Thread.sleep(500);
    Assert.assertTrue(result._thread.isAlive() || result2._thread.isAlive());
    Assert.assertTrue(null != manager.getHelixDataAccessor().getProperty(accessor.keyBuilder().liveInstance(instance2)));
    
    result._manager.disconnect();
    result2._manager.disconnect();
    result._thread.interrupt();
    result2._thread.interrupt();
  }
}

@@ -2708,32 +2708,252 @@
 ead.isAlive());%0A
+    for(int i = 0; i%3C 20; i++)%0A    %7B%0A      if(null == manager.getHelixDataAccessor().getProperty(accessor.keyBuilder().liveInstance(instance2)))%0A      %7B%0A        Thread.sleep(100);%0A      %7D%0A      else%0A        break;%0A    %7D%0A
     Assert.asser

fix intermitent test failures