diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Agent.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Agent.java index 776325bc..83123bbe 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Agent.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Agent.java @@ -112,6 +112,9 @@ public class Agent { return false; } this.setRemainLoad(this.getRemainLoad() + load); + if (this.getRemainLoad() == this.getMaxLoad()) { + this.setCurrentStatus(AgentService.AGENT_STATUS_Idel); + } return true; } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java index a51c6992..fc7ce182 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java @@ -102,7 +102,7 @@ public class TestPlanFactory { this.runningAgentFactory = runningAgentFactory; } - public TestPlan createATestPlanWithoutIdentity(TestPlanModel testPlanModel, + public TestPlan createATestPlanWithoutId(TestPlanModel testPlanModel, User user, UUID runId) throws IllegalParameterException { Logger.getLogger(TestPlanFactory.class).info( "testPlanName:" + testPlanModel.getName()); diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java index 441bced9..7b8bb278 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/TestPlanEngine.java @@ -116,7 +116,7 @@ public class TestPlanEngine implements TaskCompleteCallback, final User user, final UUID testPlanRunId) { try { TestPlan testPlan = this.getTestPlanFactory() - .createATestPlanWithoutIdentity(testPlanBusinessModel, + .createATestPlanWithoutId(testPlanBusinessModel, user, testPlanRunId); Logger.getLogger(TestPlanService.class).info( "test plan name:" + testPlan.getName()); diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/TestPlanLoadApplication.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/TestPlanLoadApplication.java index 6c455715..7d45429c 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/TestPlanLoadApplication.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/TestPlanLoadApplication.java @@ -52,7 +52,7 @@ public class TestPlanLoadApplication implements Transaction { @Override public TestPlanStatus execute() throws Exception { - synchronized (this.highAvailableAgentPool.getPool()) { + synchronized (this.highAvailableAgentPool) { if (!hasEnoughMaxLoad(this.getTestPlan().getRequiredLoad())) { logger.error("There is no enough max load in the pool!"); return TestPlanStatus.PendingNoEnoughMaxLoad; diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/faultolerence/BriefAgentFault.java b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/faultolerence/BriefAgentFault.java index beb32ce0..51ea16a0 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/faultolerence/BriefAgentFault.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/faultolerence/BriefAgentFault.java @@ -35,7 +35,7 @@ public class BriefAgentFault implements FaultTolerance { if (isLiving()) { return; } - synchronized (this.getHaPool().getPool()) { + synchronized (this.getHaPool()) { this.getHaPool().checkHeartBeat(this.getAgent()); } } 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 1f6ccdac..22134b76 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 @@ -69,11 +69,11 @@ public class HighAvailablePoolImpl extends CurrentLoadSubject implements this.maxAvailableLoad = maxAvailableLoad; } - public void add(Agent agent) { + public synchronized void add(Agent agent) { this.getPool().put(agent.getHostName(), agent); } - public void remove(Agent agent) { + public synchronized void remove(Agent agent) { this.getPool().put(agent.getHostName(), agent); } @@ -118,12 +118,10 @@ public class HighAvailablePoolImpl extends CurrentLoadSubject implements } @Scheduled(cron = "0,30 */1 * * * *") - public void checkAllHeartBeat() { + public synchronized void checkAllHeartBeat() { synchronized (this.getAgentRepository().getAddDeleteLock()) { - synchronized (this.getPool()) { - heartBeatsAndUpdateHAPool(); - doSubstituteIfRequired(); - } + heartBeatsAndUpdateHAPool(); + doSubstituteIfRequired(); } } @@ -139,7 +137,7 @@ public class HighAvailablePoolImpl extends CurrentLoadSubject implements } } - private void calculateHAPoolLoadStatusInMonopolize() { + private synchronized void calculateHAPoolLoadStatusInMonopolize() { int maxLoad = 0, availableLoad = 0; for (Agent agent : this.getPool().values()) { if (agent.getCurrentStatus() == AgentService.AGENT_STATUS_BreakDown) { diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/TestBase_MakeUpTestPlan.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/TestBase_MakeUpTestPlan.java index c009323b..c9ccf804 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/TestBase_MakeUpTestPlan.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/TestBase_MakeUpTestPlan.java @@ -9,6 +9,7 @@ import org.bench4q.master.domain.entity.RunningAgentDB; import org.bench4q.master.domain.entity.TestPlan; import org.bench4q.master.domain.entity.TestPlanScript; import org.bench4q.master.domain.entity.User; +import org.bench4q.master.domain.factory.TestPlanFactory; import org.bench4q.master.domain.repository.AgentRepository; import org.bench4q.master.domain.repository.TestPlanRepository; import org.bench4q.master.domain.repository.UserRepository; @@ -43,6 +44,8 @@ public class TestBase_MakeUpTestPlan extends TestBase { private AgentRepository agentRepository; private AgentMessenger agentMessenger; private TestPlanEngine testPlanEngine; + @Autowired + protected TestPlanFactory testPlanFactory; private UUID testPlanRunIdUuid; private int scriptId; private int scriptIdForSecond; @@ -264,7 +267,14 @@ public class TestBase_MakeUpTestPlan extends TestBase { } else { model = createATestPlanWithOneScript(scriptOne); } - this.getTestPlanEngine().submitTestPlan(model, user, testPlanRunId); + try { + this.getTestPlanRepository().attach( + this.testPlanFactory.createATestPlanWithoutId(model, user, + testPlanRunId)); + } catch (Exception e) { + e.printStackTrace(); + } + this.setTestPlanRunIdUuid(testPlanRunId); return testPlanRunId; } diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/entity/Test_RunningAgent.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/entity/Test_RunningAgent.java index 882deb63..4fb5fb3a 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/entity/Test_RunningAgent.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/entity/Test_RunningAgent.java @@ -2,30 +2,13 @@ package org.bench4q.master.unitTest.entity; import static org.junit.Assert.*; -import java.util.List; -import java.util.Set; -import java.util.UUID; - -import org.bench4q.master.domain.RunningAgentInterface; -import org.bench4q.master.domain.RunningScriptInterface; -import org.bench4q.master.domain.entity.Agent; import org.bench4q.master.domain.entity.RunningAgentDB; -import org.bench4q.master.domain.entity.Script; import org.bench4q.master.domain.entity.TestPlan; import org.bench4q.master.domain.entity.TestPlanScript; -import org.bench4q.master.domain.entity.TestPlanScriptResult; -import org.bench4q.master.domain.factory.RunningAgentFactory; -import org.bench4q.master.domain.factory.TestPlanFactory; -import org.bench4q.master.domain.repository.AgentRepository; -import org.bench4q.master.domain.repository.UserRepository; import org.bench4q.master.domain.valueobject.transaction.TransactionFactory; import org.bench4q.master.domain.valueobject.transaction.impl.ScriptLoadApplication; import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan; import org.bench4q.share.enums.master.TestPlanStatus; -import org.bench4q.share.helper.MarshalHelper; -import org.bench4q.share.models.agent.RunScenarioModel; -import org.bench4q.share.models.agent.TestBriefStatusModel; -import org.bench4q.share.models.master.TestScriptConfig; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -36,14 +19,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:service-test-context.xml" }) public class Test_RunningAgent extends TestBase_MakeUpTestPlan { - @Autowired - private RunningAgentFactory runningAgentFactory; - - @Autowired - private TestPlanFactory testPlanFactory; - - @Autowired - private AgentRepository agentRepository; @Autowired private TransactionFactory transactionFactory; @@ -83,71 +58,29 @@ public class Test_RunningAgent extends TestBase_MakeUpTestPlan { @Test public void test_stop() { - // ScriptLoadApplication application = this.transactionFactory - // .buildScriptTransaction(runningScript, - // EACH_SCRIPT_LOAD_SMALLSCALE); - // RunningAgentDB underTest = this.runningAgentFactory - // .buildRunningAgentDBWithoutId(this.getAgentRepository() - // .getAgentBy(Test_AGENT_HOSTNAME), - // EACH_SCRIPT_LOAD_SMALLSCALE, runningScript, agentRunId); - } + synchronized (this.getHaPool()) { + TestPlanScript runningScript = this.getTestPlanRepository() + .getTestPlanInDomainBy(getTestPlanRunIdUuid()) + .extracSpecifiedScript(getScriptId()); + int currentLoadBeforeRun = this.getHaPool() + .getCurrentAvailableLoad(); + ScriptLoadApplication application = (ScriptLoadApplication) this.transactionFactory + .buildScriptTransaction(runningScript, + EACH_SCRIPT_LOAD_SMALLSCALE); + RunningAgentDB underTest = null; - public class MockRunningScript implements RunningScriptInterface { - private Script script = Test_RunningAgent.this - .getScriptService() - .loadScripts( - Test_RunningAgent.this.getUserRepository().getUser( - "admin")).get(0); + underTest = (RunningAgentDB) application.execute().get(0); + assertTrue(underTest.distributeScriptAndParams()); - @Override - public int getScriptId() { - return this.script.getId(); + assertTrue(underTest.run()); + int currentLoadAfterRun = this.getHaPool() + .getCurrentAvailableLoad(); + assertEquals(currentLoadBeforeRun - 500, currentLoadAfterRun); + assertTrue(underTest.stop()); + int currentLoadAfterStop = this.getHaPool() + .getCurrentAvailableLoad(); + assertEquals(currentLoadBeforeRun, currentLoadAfterStop); } - @Override - public UUID getTestPlanID() { - // TODO Auto-generated method stub - return null; - } - - @Override - public RunScenarioModel getScenario() { - return (RunScenarioModel) MarshalHelper.tryUnmarshal( - RunScenarioModel.class, this.script.getScriptContent()); - } - - @Override - public void doAfterApplyLoad( - List agentsAfterDistribute) { - // TODO Auto-generated method stub - - } - - @Override - public Set getRunningAgents() { - // TODO Auto-generated method stub - return null; - } - - @Override - public int getRequireLoad() { - // TODO Auto-generated method stub - return 0; - } - - @Override - public void doForComplete() { - // TODO Auto-generated method stub - - } - - @Override - public List doAfterRun() { - // TODO Auto-generated method stub - return null; - } - - public MockRunningScript() { - } } } diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/infrastructure/communication/Test_AgentMessenger.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/infrastructure/communication/Test_AgentMessenger.java index 845c0fb2..de663275 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/infrastructure/communication/Test_AgentMessenger.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/infrastructure/communication/Test_AgentMessenger.java @@ -100,4 +100,10 @@ public class Test_AgentMessenger extends TestBase_MakeUpTestPlan { // Just test for if it can run properly; } + @Test + public void test_getStatus() { + // this.agentMessenger.getStatus(Agent.createAgentWithoutId(hostName, + // port)) + } + } diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/transaction/Test_ScriptLoadCommand.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/transaction/Test_ScriptLoadCommand.java index 45b58562..57ec21c1 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/transaction/Test_ScriptLoadCommand.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/transaction/Test_ScriptLoadCommand.java @@ -64,7 +64,7 @@ public class Test_ScriptLoadCommand extends TestBase_MakeUpTestPlan { this.getAgentMessenger(), this.runningAgentFactory, this.getHaPool(), this.getAgentRepository()); List runningAgents = null; - synchronized (this.getHaPool().getPool()) { + synchronized (this.getHaPool()) { runningAgents = scriptLoadCommand.execute(); }