<commit_before>package org.ngrinder.perftest.service;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.List;

import net.grinder.AgentControllerDaemon;
import net.grinder.SingleConsole;
import net.grinder.common.GrinderProperties;
import net.grinder.common.processidentity.WorkerProcessReport;
import net.grinder.communication.AgentControllerCommunicationDefauts;
import net.grinder.console.communication.ProcessControl.Listener;
import net.grinder.console.communication.ProcessControl.ProcessReports;
import net.grinder.console.communication.ProcessControlImplementation;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.ngrinder.agent.service.AgentManagerService;
import org.ngrinder.common.constant.NGrinderConstants;
import org.ngrinder.model.PerfTest;
import org.ngrinder.model.Status;
import org.ngrinder.script.model.FileEntry;
import org.ngrinder.script.model.FileType;
import org.ngrinder.script.repository.MockFileEntityRepsotory;
import org.ngrinder.script.util.CompressionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;

public class PerfTestRunnableTest extends AbstractPerfTestTransactionalTest implements NGrinderConstants {

	@Autowired
	private MockPerfTestRunnable perfTestRunnable;

	AgentControllerDaemon agentControllerDaemon;
	
	@Autowired
	private AgentManager agentManager;
	
	@Autowired
	private AgentManagerService agentService;
	
	@Autowired
	public MockFileEntityRepsotory fileEntityRepository;

	private int currentProcessCount = 10;
	private Object processCountSync = new Object();

	@Before
	public void before() throws IOException {
		System.setProperty("java.library.path",
				System.getProperty("java.library.path") + File.pathSeparator + new File("./src/test/resources/native_lib").getAbsolutePath());
		
		CompressionUtil compressUtil = new CompressionUtil();

		File tempRepo = new File(System.getProperty("java.io.tmpdir"), "repo");
		fileEntityRepository.setUserRepository(new File(tempRepo, getTestUser().getUserId()));
		File testUserRoot = fileEntityRepository.getUserRepoDirectory(getTestUser()).getParentFile();

		testUserRoot.mkdirs();
		compressUtil.unzip(new ClassPathResource("TEST_USER.zip").getFile(), testUserRoot);
		testUserRoot.deleteOnExit();

		FileEntry fileEntry = new FileEntry();
		fileEntry.setPath("test1.py");
		String worldString = IOUtils.toString(new ClassPathResource("world.py").getInputStream());
		if (fileEntry.getFileType().isEditable()) {
			fileEntry.setContent(worldString);
		} else {
			fileEntry.setContentBytes(worldString.getBytes());
		}
		fileEntityRepository.save(getTestUser(), fileEntry, "UTF-8");
		
		clearAllPerfTest();
		createPerfTest("test1", Status.READY, null);
		List<PerfTest> allPerfTest = perfTestService.getAllPerfTest();
		assertThat(allPerfTest.size(), is(1));

		agentControllerDaemon = new AgentControllerDaemon();
		agentControllerDaemon.setAgentConfig(agentConfig1);
		agentControllerDaemon.run(AgentControllerCommunicationDefauts.DEFAULT_AGENT_CONTROLLER_SERVER_PORT);
		
		sleep(12000);
		agentService.getAgentList();
		int agentCount = agentManager.getAllAttachedAgents().size();
        String ip = InetAddress.getLocalHost().getHostAddress();<commit_msg>[NGRINDER-209] Fix unit test errors fix PerfRunnable test failure<commit_after>package org.ngrinder.perftest.service;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.List;

import net.grinder.AgentControllerDaemon;
import net.grinder.SingleConsole;
import net.grinder.common.GrinderProperties;
import net.grinder.common.processidentity.WorkerProcessReport;
import net.grinder.communication.AgentControllerCommunicationDefauts;
import net.grinder.console.communication.ProcessControl.Listener;
import net.grinder.console.communication.ProcessControl.ProcessReports;
import net.grinder.console.communication.ProcessControlImplementation;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.ngrinder.agent.service.AgentManagerService;
import org.ngrinder.common.constant.NGrinderConstants;
import org.ngrinder.model.PerfTest;
import org.ngrinder.model.Status;
import org.ngrinder.script.model.FileEntry;
import org.ngrinder.script.model.FileType;
import org.ngrinder.script.repository.MockFileEntityRepsotory;
import org.ngrinder.script.util.CompressionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;

public class PerfTestRunnableTest extends AbstractPerfTestTransactionalTest implements NGrinderConstants {

	@Autowired
	private MockPerfTestRunnable perfTestRunnable;

	AgentControllerDaemon agentControllerDaemon;
	
	@Autowired
	private AgentManager agentManager;
	
	
	@Autowired
	public MockFileEntityRepsotory fileEntityRepository;

	private int currentProcessCount = 10;
	private Object processCountSync = new Object();

	@Before
	public void before() throws IOException {
		System.setProperty("java.library.path",
				System.getProperty("java.library.path") + File.pathSeparator + new File("./src/test/resources/native_lib").getAbsolutePath());
		AgentManagerService agentService = new AgentManagerService();
		CompressionUtil compressUtil = new CompressionUtil();

		File tempRepo = new File(System.getProperty("java.io.tmpdir"), "repo");
		fileEntityRepository.setUserRepository(new File(tempRepo, getTestUser().getUserId()));
		File testUserRoot = fileEntityRepository.getUserRepoDirectory(getTestUser()).getParentFile();

		testUserRoot.mkdirs();
		compressUtil.unzip(new ClassPathResource("TEST_USER.zip").getFile(), testUserRoot);
		testUserRoot.deleteOnExit();

		FileEntry fileEntry = new FileEntry();
		fileEntry.setPath("test1.py");
		String worldString = IOUtils.toString(new ClassPathResource("world.py").getInputStream());
		if (fileEntry.getFileType().isEditable()) {
			fileEntry.setContent(worldString);
		} else {
			fileEntry.setContentBytes(worldString.getBytes());
		}
		fileEntityRepository.save(getTestUser(), fileEntry, "UTF-8");
		
		clearAllPerfTest();
		createPerfTest("test1", Status.READY, null);
		List<PerfTest> allPerfTest = perfTestService.getAllPerfTest();
		assertThat(allPerfTest.size(), is(1));

		agentControllerDaemon = new AgentControllerDaemon();
		agentControllerDaemon.setAgentConfig(agentConfig1);
		agentControllerDaemon.run(AgentControllerCommunicationDefauts.DEFAULT_AGENT_CONTROLLER_SERVER_PORT);
		
		sleep(12000);
		
		int agentCount = agentService.getAgentList().size();
        String ip = InetAddress.getLocalHost().getHostAddress();
{'fraction_non_alphanumeric': 0.10400552486187846, 'fraction_numerical': 0.003729281767955801, 'mean_word_length': 6.862106406080348, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1797961', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2129, 'n_words': 356}