add test for runnAgentStop
add test for runnAgentStop, and refactor
This commit is contained in:
parent
11e61c546c
commit
2e8a35d729
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -35,7 +35,7 @@ public class BriefAgentFault implements FaultTolerance {
|
|||
if (isLiving()) {
|
||||
return;
|
||||
}
|
||||
synchronized (this.getHaPool().getPool()) {
|
||||
synchronized (this.getHaPool()) {
|
||||
this.getHaPool().checkHeartBeat(this.getAgent());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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<? extends RunningAgentInterface> agentsAfterDistribute) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<? extends RunningAgentInterface> 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<TestPlanScriptResult> doAfterRun() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
public MockRunningScript() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ public class Test_ScriptLoadCommand extends TestBase_MakeUpTestPlan {
|
|||
this.getAgentMessenger(), this.runningAgentFactory,
|
||||
this.getHaPool(), this.getAgentRepository());
|
||||
List<? extends RunningAgentInterface> runningAgents = null;
|
||||
synchronized (this.getHaPool().getPool()) {
|
||||
synchronized (this.getHaPool()) {
|
||||
runningAgents = scriptLoadCommand.execute();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue