package com.hazelcast.simulator.tests.external;

import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.simulator.common.SimulatorProperties;
import com.hazelcast.simulator.provisioner.Bash;
import com.hazelcast.simulator.test.TestContext;
import com.hazelcast.simulator.test.annotations.Run;
import com.hazelcast.simulator.test.annotations.Setup;

import java.io.File;

import static com.hazelcast.simulator.utils.FileUtils.deleteQuiet;
import static com.hazelcast.simulator.utils.HostAddressPicker.pickHostAddress;
import static java.lang.String.format;

public class ExternalClientStarterTest {

    private static final ILogger LOGGER = Logger.getLogger(ExternalClientStarterTest.class);

    // properties
    public String binaryName = "binaryName";
    public String arguments = "";
    public String logFileName = "external-client";
    public int processCount = 1;

    private final SimulatorProperties props = new SimulatorProperties();
    private final Bash bash = new Bash(props);
    private final String ipAddress = pickHostAddress();

    @Setup
    public void setUp(TestContext testContext) throws Exception {
        testContext.getTargetInstance().getAtomicLong("externalClientsStarted").addAndGet(processCount);

        // delete the local binary, so it won't get downloaded again
        deleteQuiet(new File(binaryName));
    }

    @Run
    public void run() {
        for (int i = 1; i <= processCount; i++) {
            String tmpArguments = arguments
                    .replace("$PROCESS_INDEX", String.valueOf(i))
                    .replace("$IP_ADDRESS", ipAddress);

            String tmpLogFileName = logFileName + "_" + i;

            LOGGER.info(format("Starting external client: %s %s >> %s.log", binaryName, tmpArguments, tmpLogFileName));
            bash.execute(format("../upload/%s %s >> %s.log &", binaryName, tmpArguments, tmpLogFileName));
        }
    }
}

@@ -42,16 +42,61 @@
 ernal;%0A%0A
+import com.hazelcast.core.HazelcastInstance;%0A
 import c
@@ -117,32 +117,32 @@
 ogging.ILogger;%0A
-
 import com.hazel
@@ -450,16 +450,97 @@
 .File;%0A%0A
+import static com.hazelcast.simulator.tests.helpers.HazelcastTestUtils.isClient;%0A
 import s
@@ -1208,24 +1208,55 @@
 Address();%0A%0A
+    private boolean isClient;%0A%0A
     @Setup%0A 
@@ -1332,39 +1332,151 @@
     
-testContext.getTargetInstance()
+HazelcastInstance hazelcastInstance = testContext.getTargetInstance();%0A        if (isClient(hazelcastInstance)) %7B%0A            hazelcastInstance
 .get
@@ -1536,16 +1536,55 @@
 sCount);
+%0A            isClient = true;%0A        %7D
 %0A%0A      
@@ -1705,16 +1705,16 @@
    @Run%0A
-
     publ
@@ -1725,24 +1725,80 @@
 oid run() %7B%0A
+        if (!isClient) %7B%0A            return;%0A        %7D%0A%0A
         for 

Fix auto detection of client count in ExternalClientStarterTest.