diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/RunningAgentFactory.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/RunningAgentFactory.java index f303d7ba..f6fc136e 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/RunningAgentFactory.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/RunningAgentFactory.java @@ -14,12 +14,29 @@ import org.springframework.stereotype.Component; @Component public class RunningAgentFactory { - @Autowired + private AgentMessenger agentMessenger; - @Autowired private AgentRepository agentRepository; + private AgentMessenger getAgentMessenger() { + return agentMessenger; + } + + @Autowired + private void setAgentMessenger(AgentMessenger agentMessenger) { + this.agentMessenger = agentMessenger; + } + + private AgentRepository getAgentRepository() { + return agentRepository; + } + + @Autowired + private void setAgentRepository(AgentRepository agentRepository) { + this.agentRepository = agentRepository; + } + public RunningAgentInterface buildRunningAgentDBWithoutId(Agent agent, int loadInUse, RunningScriptInterface runningScript, UUID agentRunId) { RunningAgentDB runningAgent = RunningAgentDB @@ -44,7 +61,7 @@ public class RunningAgentFactory { } public void convertToDomain(RunningAgentDB fromReop) { - fromReop.setAgentMessenger(this.agentMessenger); - fromReop.setAgentRepository(this.agentRepository); + fromReop.setAgentMessenger(this.getAgentMessenger()); + fromReop.setAgentRepository(this.getAgentRepository()); } } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/highavailable/impl/HighAvailablePoolImpl.java b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/highavailable/impl/HighAvailablePoolImpl.java index d2480f31..999fee52 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/highavailable/impl/HighAvailablePoolImpl.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/highavailable/impl/HighAvailablePoolImpl.java @@ -50,7 +50,6 @@ public class HighAvailablePoolImpl extends CurrentLoadSubject implements return agentRepository; } - @Autowired private void setAgentRepository(AgentRepository agentRepository) { this.agentRepository = agentRepository; } @@ -122,12 +121,21 @@ public class HighAvailablePoolImpl extends CurrentLoadSubject implements this.agentRunIdShouldBeSubstitute = agentRunIdShouldBeSubstitute; } - public HighAvailablePoolImpl() { + @Autowired + public HighAvailablePoolImpl(AgentRepository agentRepository) { this.setPool(new HashMap()); this.setAgentRunBlotters(new HashMap()); this.setAgentStatusOfPreviousBeatMap(new HashMap()); this.setCurrentAvailableLoad(0); this.setAgentRunIdShouldBeSubstitute(new LinkedList()); + this.setAgentRepository(agentRepository); + initialPool(); + } + + private void initialPool() { + for (Agent agent : this.getAgentRepository().loadEntities()) { + this.add(agent); + } } @Scheduled(cron = "0,30 */1 * * * *") diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/infrastructure/ha/Mock_AgentMessenger.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/infrastructure/ha/Mock_AgentMessenger.java index 5213ec74..2afb208a 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/infrastructure/ha/Mock_AgentMessenger.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/infrastructure/ha/Mock_AgentMessenger.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.UUID; import org.bench4q.master.domain.entity.Agent; +import org.bench4q.master.domain.service.AgentService; import org.bench4q.master.infrastructure.communication.AgentMessenger; import org.bench4q.share.models.agent.RunScenarioModel; import org.bench4q.share.models.agent.RunScenarioResultModel; @@ -14,7 +15,9 @@ import org.bench4q.share.models.agent.TestBriefStatusModel; import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel; import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; import org.bench4q.share.models.agent.statistics.AgentPagesBriefModel; +import org.springframework.stereotype.Component; +@Component public class Mock_AgentMessenger implements AgentMessenger { private UUID testId = UUID.randomUUID(); @@ -47,11 +50,23 @@ public class Mock_AgentMessenger implements AgentMessenger { @Override public StopTestModel stop(Agent agent, UUID runId) { - return new StopTestModel(true); + if (runId == this.testId) { + return new StopTestModel(true); + } + return new StopTestModel(false); } @Override public ServerStatusModel getStatus(Agent agent) { + ServerStatusModel result = new ServerStatusModel(); + if (agent.getCurrentStatus() == AgentService.AGENT_STATUS_Idel) { + return result; + } else if (agent.getCurrentStatus() == AgentService.AGENT_STATUS_BreakDown) { + return null; + } else if (agent.getCurrentStatus() == AgentService.AGENT_STATUS_InRun) { + result.getRunningTests().add(UUID.randomUUID()); + return result; + } return null; } diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/infrastructure/ha/Test_highAvailableWithMockMessenger.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/infrastructure/ha/Test_highAvailableWithMockMessenger.java index efbc4ec7..2aef0294 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/infrastructure/ha/Test_highAvailableWithMockMessenger.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/infrastructure/ha/Test_highAvailableWithMockMessenger.java @@ -4,8 +4,11 @@ import static org.junit.Assert.*; import java.util.ArrayList; import java.util.List; +import java.util.UUID; +import org.bench4q.master.domain.RunningAgentInterface; import org.bench4q.master.domain.entity.Agent; +import org.bench4q.master.domain.factory.RunningAgentFactory; import org.bench4q.master.domain.repository.AgentRepository; import org.bench4q.master.helper.SessionHelper; import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl; @@ -19,7 +22,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { HighAvailablePoolImpl.class, AgentRepository.class, Mock_AgentMessenger.class, HttpRequester.class, - SessionHelper.class }) + SessionHelper.class, RunningAgentFactory.class }) public class Test_highAvailableWithMockMessenger { private List testCase1 = new ArrayList(); @@ -37,6 +40,23 @@ public class Test_highAvailableWithMockMessenger { @Test public void test_applyFor() { - + int loadToApply = 10; + this.ha.checkAllHeartBeat(); + List result = null; + synchronized (this.ha) { + assertTrue(this.ha.getCurrentAvailableLoad() > loadToApply); + result = this.ha.applyFor(loadToApply); + } + assertEquals(1, result.size()); + assertEquals(1, result.size()); + assertEquals(loadToApply, result.get(0).getLoadInUse()); + UUID runId = result.get(0).getAgentRunId(); + assertNotNull(runId); + assertNull(result.get(0).getRunningScript()); + assertEquals(1, this.ha.getAgentRunBlotters().size()); + assertEquals(result.get(0), this.ha.getAgentRunBlotters().get(runId)); + for (RunningAgentInterface runningAgent : result) { + assertTrue(runningAgent.stop()); + } } } diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/ServerStatusModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/ServerStatusModel.java index 86e709df..450017ea 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/ServerStatusModel.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/ServerStatusModel.java @@ -1,5 +1,6 @@ package org.bench4q.share.models.agent; +import java.util.LinkedList; import java.util.List; import java.util.UUID; @@ -9,8 +10,8 @@ import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "serverStatus") public class ServerStatusModel { - private List runningTests; - private List finishedTests; + private List runningTests = new LinkedList(); + private List finishedTests = new LinkedList(); @XmlElementWrapper(name = "runningTests") @XmlElement(name = "runningTest")