From e1b37bb4a00f563d5fe7424141f026e3e3bf1044 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Wed, 23 Apr 2014 20:27:13 +0800 Subject: [PATCH] refactor and remove some bugs refactor and remove some bugs --- .../org/bench4q/agent/api/TestController.java | 57 +++---- .../org/bench4q/agent/scenario/Scenario.java | 2 +- .../agent/scenario/ScenarioContext.java | 19 +++ .../agent/scenario/ScenarioEngine.java | 19 ++- .../ScriptParameterization/1/param1.txt | 3 +- .../ScriptParameterization/1/param2.txt | 1 + .../a75b14de-d99a-49ad-80b8-6ad6078ae726.pdf | 0 .../bench4q/master/api/AgentController.java | 27 ++-- .../bench4q/master/domain/entity/Agent.java | 19 ++- .../master/domain/entity/RunningAgentDB.java | 4 +- .../bench4q/master/domain/entity/Script.java | 3 + .../master/domain/entity/TestPlan.java | 8 +- .../domain/repository/AgentRepository.java | 25 ++++ .../master/domain/service/AgentService.java | 29 ++-- .../master/domain/service/TestPlanEngine.java | 3 +- .../impl/AgentExecutionTransaction.java | 117 --------------- .../transaction/impl/ScriptLoadBase.java | 53 +++---- .../communication/AgentMessenger.java | 141 +++--------------- .../highavailable/HighAvailablePool.java | 37 +++-- .../master/test/TestBase_MakeUpTestPlan.java | 6 +- .../communication/Test_AgentMessenger.java | 17 ++- .../controller/TestPlanControllerTest.java | 67 --------- .../Test_TestPlanController.java} | 7 +- .../Test_TestPlanResultController.java} | 5 +- .../testPlan}/Test_LoadDistribute.java | 2 +- .../test/repository/Test_UserRepository.java | 5 - .../test/service/Test_TestPlanEngine.java | 13 +- .../service/Test_TestPlanScriptService.java | 11 -- .../test/service/Test_TestPlanService.java | 2 +- .../transaction/Test_ScriptLoadCommand.java | 2 +- 30 files changed, 234 insertions(+), 470 deletions(-) create mode 100644 Bench4Q-Master/ScriptParameterization/1/param2.txt create mode 100644 Bench4Q-Master/report/a75b14de-d99a-49ad-80b8-6ad6078ae726.pdf delete mode 100644 Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/AgentExecutionTransaction.java delete mode 100644 Bench4Q-Master/src/test/java/org/bench4q/master/test/controller/TestPlanControllerTest.java rename Bench4Q-Master/src/test/java/org/bench4q/master/test/{testplan/TestPlanTester.java => controller/Test_TestPlanController.java} (98%) rename Bench4Q-Master/src/test/java/org/bench4q/master/test/{testplan/TestTestPlanResult.java => controller/Test_TestPlanResultController.java} (86%) rename Bench4Q-Master/src/test/java/org/bench4q/master/test/{testplan => domain/testPlan}/Test_LoadDistribute.java (98%) diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java index b5ac5747..9e4d6711 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java @@ -1,6 +1,5 @@ package org.bench4q.agent.api; -import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -30,7 +29,6 @@ import org.bench4q.share.models.agent.statistics.AgentPagesBriefModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @@ -44,10 +42,6 @@ public class TestController { private ParameterFileCollector paramFileCollector; private Logger logger = Logger.getLogger(TestController.class); - private Logger getLogger() { - return logger; - } - private ScenarioEngine getScenarioEngine() { return scenarioEngine; } @@ -66,21 +60,37 @@ public class TestController { this.paramFileCollector = paramFileCollector; } - @RequestMapping(value = "/submitScenarioWithParams", method = RequestMethod.POST) + @RequestMapping(value = "/bookTest/{poolSize}", method = { + RequestMethod.GET, RequestMethod.POST }) @ResponseBody - public String submitParams( - @RequestParam("files[]") List files, - @RequestParam("scenarioModel") String scenarioModel) { + public RunScenarioResultModel bookTest(@PathVariable int poolSize) { try { UUID runId = UUID.randomUUID(); + this.getScenarioEngine().addRunningTestWithoutScenario(runId, + poolSize); + RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel(); + runScenarioResultModel.setRunId(runId); + return runScenarioResultModel; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + @RequestMapping(value = "/submitScenarioWithParams/{runId}", method = RequestMethod.POST) + @ResponseBody + public String submitParams( + @PathVariable UUID runId, + @RequestParam(value = "files[]", required = false) List files, + @RequestParam("scenarioModel") String scenarioModel) { + try { this.getParamFileCollector().collectParamFiles(files, runId); System.out.println(scenarioModel); RunScenarioModel runScenarioModel = (RunScenarioModel) MarshalHelper .unmarshal(RunScenarioModel.class, scenarioModel); this.getScenarioEngine().submitScenario(runId, - Scenario.scenarioBuilderWithCompile(runScenarioModel), - runScenarioModel.getPoolSize()); + Scenario.scenarioBuilderWithCompile(runScenarioModel)); return MarshalHelper.tryMarshal(buildWith(runId)); } catch (Exception e) { logger.error("/submitScenarioWithParams", e); @@ -101,29 +111,6 @@ public class TestController { : null; } - @RequestMapping(value = "/runWithoutParams", method = RequestMethod.POST) - @ResponseBody - public RunScenarioResultModel run( - @RequestBody RunScenarioModel runScenarioModel) - throws UnknownHostException { - Scenario scenario = Scenario - .scenarioBuilderWithCompile(runScenarioModel); - UUID runId = UUID.randomUUID(); - System.out.println(runScenarioModel.getPoolSize()); - this.getLogger().info(MarshalHelper.tryMarshal(runScenarioModel)); - if (runScenarioModel.getPoolSize() <= 0) { - logger.info("This RunScenarioModel's pool size is L.E zero, so throw out"); - return null; - } - this.getScenarioEngine().submitScenario(runId, scenario, - runScenarioModel.getPoolSize()); - - this.getScenarioEngine().runWith(runId); - RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel(); - runScenarioResultModel.setRunId(runId); - return runScenarioResultModel; - } - @RequestMapping(value = "/briefAll/{runId}", method = RequestMethod.GET) @ResponseBody public TestBriefStatusModel briefAll(@PathVariable UUID runId) { diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java index 7aeaa414..2bd42aca 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java @@ -76,7 +76,7 @@ public class Scenario { } } - public static Scenario scenarioBuilder(String scenarioContent) { + public static Scenario scenarioBuilderWithCompile(String scenarioContent) { return scenarioBuilderWithCompile((RunScenarioModel) MarshalHelper .tryUnmarshal(RunScenarioModel.class, scenarioContent)); } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java index 57f7e28f..dd3e07d7 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java @@ -94,6 +94,25 @@ public class ScenarioContext { return scenarioContext; } + public static ScenarioContext buildScenarioContextWithoutScenario( + UUID testId, int poolSize) { + ScenarioContext scenarioContext = new ScenarioContext(); + scenarioContext.setTestId(testId); + final SynchronousQueue workQueue = new SynchronousQueue(); + ThreadPoolExecutor executor = new ThreadPoolExecutor(poolSize, + poolSize, keepAliveTime, TimeUnit.MINUTES, workQueue, + new DiscardPolicy()); + scenarioContext.setStartDate(new Date(System.currentTimeMillis())); + scenarioContext.setExecutorService(executor); + scenarioContext.setDataStatistics(new ScenarioResultCollector(testId)); + return scenarioContext; + } + + public ScenarioContext addScenrio(Scenario scenario) { + this.setScenario(scenario); + return this; + } + /** * Now, I tolerate that if the requiredLoad < * this.getExecutor.getCorePoolSize(), then the excess threads will be diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java index 6552d587..e8cfa25f 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java @@ -25,13 +25,24 @@ public class ScenarioEngine { this.runningTests = runningTests; } - public void submitScenario(UUID runId, final Scenario scenario, int poolSize) { + public void addRunningTestWithoutScenario(UUID runId, int poolSize) { try { final ScenarioContext scenarioContext = ScenarioContext - .buildScenarioContext(runId, scenario, poolSize); + .buildScenarioContextWithoutScenario(runId, poolSize); this.getRunningTests().put(runId, scenarioContext); + } catch (Exception e) { + e.printStackTrace(); + } + } - System.out.println(poolSize); + public void submitScenario(UUID runId, final Scenario scenario) { + try { + this.getRunningTests().get(runId).addScenrio(scenario); + // final ScenarioContext scenarioContext = ScenarioContext + // .buildScenarioContext(runId, scenario, poolSize); + // this.getRunningTests().put(runId, scenarioContext); + + // System.out.println(poolSize); } catch (Exception e) { e.printStackTrace(); @@ -69,6 +80,6 @@ public class ScenarioEngine { public void updatePopulation(UUID testId, int requiredLoad) { ScenarioContext context = this.getRunningTests().get(testId); - context.updatePopulation(requiredLoad); + context.updatePopulation(requiredLoad); } } diff --git a/Bench4Q-Master/ScriptParameterization/1/param1.txt b/Bench4Q-Master/ScriptParameterization/1/param1.txt index 8b3a483f..7eb8d5f4 100644 --- a/Bench4Q-Master/ScriptParameterization/1/param1.txt +++ b/Bench4Q-Master/ScriptParameterization/1/param1.txt @@ -1,2 +1 @@ -123,234,566 -167,567,789 \ No newline at end of file +key=set;value=nv \ No newline at end of file diff --git a/Bench4Q-Master/ScriptParameterization/1/param2.txt b/Bench4Q-Master/ScriptParameterization/1/param2.txt new file mode 100644 index 00000000..aa40ea43 --- /dev/null +++ b/Bench4Q-Master/ScriptParameterization/1/param2.txt @@ -0,0 +1 @@ +everyThing ok = ok \ No newline at end of file diff --git a/Bench4Q-Master/report/a75b14de-d99a-49ad-80b8-6ad6078ae726.pdf b/Bench4Q-Master/report/a75b14de-d99a-49ad-80b8-6ad6078ae726.pdf new file mode 100644 index 00000000..e69de29b diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/api/AgentController.java b/Bench4Q-Master/src/main/java/org/bench4q/master/api/AgentController.java index 097a4210..0783ad17 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/api/AgentController.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/api/AgentController.java @@ -39,16 +39,13 @@ public class AgentController extends BaseController { "you don't have the power to add agent to pool!", new ArrayList()); } - synchronized (this.getAgentService().getAgentLock()) { - if (!this.getAgentService().addAgentToPool( - BusinessModelMapFactory.toBusiness(agent))) { - return setAgentResponseModel(false, - "add agent to DB fails in addAgentToPool", - new ArrayList()); - } - this.getAgentService().getAgentLock().notifyAll(); - return setAgentResponseModel(true, "", new ArrayList()); + if (!this.getAgentService().addAgentToPool( + BusinessModelMapFactory.toBusiness(agent))) { + return setAgentResponseModel(false, + "add agent to DB fails in addAgentToPool", + new ArrayList()); } + return setAgentResponseModel(true, "", new ArrayList()); } @RequestMapping(value = "/removeAgentFromPool", method = RequestMethod.GET) @@ -61,15 +58,13 @@ public class AgentController extends BaseController { "you don't have the power to remove agent from pool!", new ArrayList()); } - synchronized (this.getAgentService().getAgentLock()) { - if (!this.getAgentService().removeAgentFromPool(agentId)) { - return setAgentResponseModel(false, - "remove agent from DB fails in removeAgentFromPool", - new ArrayList()); - } - return this.setAgentResponseModel(true, "", + if (!this.getAgentService().removeAgentFromPool(agentId)) { + return setAgentResponseModel(false, + "remove agent from DB fails in removeAgentFromPool", new ArrayList()); } + return this + .setAgentResponseModel(true, "", new ArrayList()); } @RequestMapping(value = "/queryAgentList", method = { RequestMethod.POST, 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 d651d6f0..11ccbfd4 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 @@ -6,7 +6,12 @@ import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; + +import org.bench4q.master.domain.repository.AgentRepository; import org.bench4q.master.domain.service.AgentService; +import org.bench4q.master.helper.ApplicationContextHelper; +import org.bench4q.master.infrastructure.communication.AgentMessenger; +import org.bench4q.share.models.agent.RunScenarioResultModel; @Entity @Table(name = "agent") @@ -18,8 +23,6 @@ public class Agent { protected int remainLoad; protected int currentStatus; - // private AgentRepository repository; - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", nullable = false) @@ -86,4 +89,16 @@ public class Agent { return agent; } + public void resetLoad() { + this.setRemainLoad(this.getMaxLoad()); + } + + public RunScenarioResultModel bookTest(int requireLoad) { + RunScenarioResultModel resultModel = ApplicationContextHelper.getBean( + AgentMessenger.class).bookTest(this, requireLoad); + this.setCurrentStatus(AgentService.AGENT_STATUS_InRun); + this.setRemainLoad(this.getRemainLoad() - requireLoad); + ApplicationContextHelper.getBean(AgentRepository.class).update(this); + return resultModel; + } } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/RunningAgentDB.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/RunningAgentDB.java index 9151a0aa..1217629d 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/RunningAgentDB.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/RunningAgentDB.java @@ -133,11 +133,11 @@ public class RunningAgentDB implements RunningAgentInterface { runScenarioModel.setPoolSize(getLoadInUse()); RunScenarioResultModel runScenarioResultModel = this .getAgentMessenger().submitScenrioWithParams(this.getAgent(), - script2.loadParamFiles(), runScenarioModel); + this.getAgentRunId(), script2.loadParamFiles(), + runScenarioModel); if (runScenarioResultModel == null) { return false; } - this.setAgentRunId(runScenarioResultModel.getRunId()); return true; } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Script.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Script.java index 77e20414..1c4f1749 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Script.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Script.java @@ -102,6 +102,9 @@ public class Script { private void saveScriptParamFiles(List paramFiles) { String folderPath = buildParamFilesFolder(); FileHelper.guardFolderExist(folderPath); + if (paramFiles == null) { + return; + } for (MultipartFile multipartFile : paramFiles) { try { multipartFile.transferTo(new File(folderPath diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlan.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlan.java index 542d3170..a84705ab 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlan.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlan.java @@ -195,13 +195,17 @@ public class TestPlan implements IAggregate { public void run() { Logger.getLogger(TestPlan.class).info( this.getTestPlanRunId() + " start to run "); + this.setLastRunningTime(new Date()); if (!applyForLoad()) { + this.failTimes++; return; } if (!distributeScriptAndParams()) { + this.failTimes++; return; } doRun(); + this.update(); } private boolean applyForLoad() { @@ -216,7 +220,9 @@ public class TestPlan implements IAggregate { result = false; } this.setCurrentStatus(currentStatus.name()); - this.getRepository().updateEntity(this); + Logger.getLogger(TestPlan.class).info( + this.getTestPlanRunId() + " status is " + this.currentStatus); + this.update(); return result; } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/repository/AgentRepository.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/repository/AgentRepository.java index dd595905..f2df57d9 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/repository/AgentRepository.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/repository/AgentRepository.java @@ -1,5 +1,6 @@ package org.bench4q.master.domain.repository; +import java.util.Collection; import java.util.List; import org.bench4q.master.domain.entity.Agent; @@ -14,6 +15,12 @@ import org.springframework.stereotype.Component; @Component public class AgentRepository extends AbstractRepositoty { + private final Object addDeleteLock = new Object(); + + public Object getAddDeleteLock() { + return addDeleteLock; + } + public boolean attach(Agent agentWithoutId) { Session session = this.getSessionHelper().openSession(); Transaction transaction = session.beginTransaction(); @@ -82,6 +89,24 @@ public class AgentRepository extends AbstractRepositoty { } } + public boolean Update(Collection poolToUpdate) { + Session session = this.getSessionHelper().openSession(); + Transaction transaction = session.beginTransaction(); + try { + for (Agent agent : poolToUpdate) { + session.update(agent); + } + transaction.commit(); + return true; + } catch (Exception e) { + transaction.rollback(); + logger.error(ExceptionLog.getStackTrace(e)); + return false; + } finally { + releaseSession(session); + } + } + public Agent getAgentBy(String hostName) { Session session = this.getSessionHelper().openSession(); try { diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/AgentService.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/AgentService.java index d13b6fae..ab09ebde 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/AgentService.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/AgentService.java @@ -49,22 +49,27 @@ public class AgentService { } public boolean addAgentToPool(Agent agentWithoutId) { - if (!this.getAgentRepository().attach( - Agent.createAgentWithoutId(agentWithoutId.getHostName(), - agentWithoutId.getPort()))) { - return false; + synchronized (this.getAgentRepository().getAddDeleteLock()) { + if (!this.getAgentRepository().attach( + Agent.createAgentWithoutId(agentWithoutId.getHostName(), + agentWithoutId.getPort()))) { + return false; + } + Agent agent = this.getAgentRepository().getAgentBy( + agentWithoutId.getHostName()); + if (agent == null) { + return false; + } + this.getHighAvailablePool().getPool() + .put(agent.getHostName(), agent); + return true; } - Agent agent = this.getAgentRepository().getAgentBy( - agentWithoutId.getHostName()); - if (agent == null) { - return false; - } - this.getHighAvailablePool().getPool().put(agent.getHostName(), agent); - return true; } public boolean removeAgentFromPool(int agentId) { - return this.getAgentRepository().detach(agentId); + synchronized (this.getAgentRepository().getAddDeleteLock()) { + return this.getAgentRepository().detach(agentId); + } } public List loadAgentPoolFromDB() { 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 610a7d47..1de1973d 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 @@ -119,6 +119,7 @@ public class TestPlanEngine implements TaskCompleteCallback, return; } testPlan.run(); + this.getTestPlanRepository().attachRunningTestPlan(testPlan); } public void doTaskComplete(UUID testPlanID) { @@ -132,7 +133,7 @@ public class TestPlanEngine implements TaskCompleteCallback, logger.info("Test plan with id " + testPlanID.toString() + " finishes!"); } - public void cleanUpTestPlan(TestPlan testPlan) { + private void cleanUpTestPlan(TestPlan testPlan) { this.getHaPool().cleanUpAboutTestPlan(testPlan.getTestPlanScripts()); this.getTestPlanRepository().detachRunningTestPlan( UUID.fromString(testPlan.getTestPlanRunId())); diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/AgentExecutionTransaction.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/AgentExecutionTransaction.java deleted file mode 100644 index 9625fd91..00000000 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/AgentExecutionTransaction.java +++ /dev/null @@ -1,117 +0,0 @@ -package org.bench4q.master.domain.valueobject.transaction.impl; - -import java.io.IOException; - -import org.apache.log4j.Logger; -import org.bench4q.master.domain.entity.Agent; -import org.bench4q.master.domain.service.AgentService; -import org.bench4q.master.domain.valueobject.transaction.Transaction; -import org.bench4q.master.domain.valueobject.transaction.exception.AgentRunException; -import org.bench4q.master.exception.ExceptionLog; -import org.bench4q.master.helper.ApplicationContextHelper; -import org.bench4q.master.infrastructure.communication.AgentMessenger; -import org.bench4q.share.models.agent.RunScenarioModel; -import org.bench4q.share.models.agent.RunScenarioResultModel; - -public class AgentExecutionTransaction implements Transaction { - - private String agentHostName; - private int agentPort; - private RunScenarioModel runScenarioModel; - private AgentService agentService; - private AgentMessenger agentMessenger; - private Logger logger = Logger.getLogger(AgentExecutionTransaction.class); - - private String getAgentHostName() { - return agentHostName; - } - - private void setAgentHostName(String agentHostName) { - this.agentHostName = agentHostName; - } - - private int getAgentPort() { - return agentPort; - } - - private void setAgentPort(int agentPort) { - this.agentPort = agentPort; - } - - private RunScenarioModel getRunScenarioModel() { - return runScenarioModel; - } - - private void setRunScenarioModel(RunScenarioModel runScenarioModel) { - this.runScenarioModel = runScenarioModel; - } - - private AgentService getAgentService() { - return agentService; - } - - private void setAgentService(AgentService agentService) { - this.agentService = agentService; - } - - public AgentMessenger getAgentMessenger() { - return agentMessenger; - } - - public void setAgentMessenger(AgentMessenger agentMessenger) { - this.agentMessenger = agentMessenger; - } - - public AgentExecutionTransaction(String agentHostName, int port, - RunScenarioModel runScenarioModel) { - this.setAgentHostName(agentHostName); - this.setAgentPort(port); - this.setRunScenarioModel(runScenarioModel); - this.setAgentService(ApplicationContextHelper.getContext().getBean( - AgentService.class)); - this.setAgentMessenger(ApplicationContextHelper.getContext().getBean( - AgentMessenger.class)); - } - - public Object execute() throws AgentRunException { - try { - if (!this.getAgentService().getLoadFromAgent( - this.getAgentHostName(), - this.getRunScenarioModel().getPoolSize())) { - throw new AgentRunException(); - } - if (!isValidRunScenarioModel(runScenarioModel)) { - throw new AgentRunException(); - } - RunScenarioResultModel ret = this.getAgentMessenger() - .runWithoutParams( - buildAgent(this.getAgentHostName(), - this.getAgentPort()), - this.getRunScenarioModel()); - if (ret == null) { - throw new AgentRunException(); - } - return ret; - } catch (IOException e) { - this.logger.error(ExceptionLog.getStackTrace(e).toString()); - throw new AgentRunException(); - } - } - - private Agent buildAgent(String hostName, int port) { - Agent agent = new Agent(); - agent.setHostName(hostName); - agent.setPort(port); - return agent; - } - - private boolean isValidRunScenarioModel(RunScenarioModel runScenarioModel) { - return runScenarioModel == null ? false : true; - } - - public void rollBack() { - this.getAgentService().backLoadToAgent(this.getAgentHostName(), - this.getRunScenarioModel().getPoolSize()); - } - -} diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/ScriptLoadBase.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/ScriptLoadBase.java index c278592b..7c1295cf 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/ScriptLoadBase.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/transaction/impl/ScriptLoadBase.java @@ -11,8 +11,6 @@ import org.bench4q.master.domain.RunningAgentInterface; import org.bench4q.master.domain.RunningScriptInterface; import org.bench4q.master.domain.RunningAgentInterface.AbstractRunningAgent; import org.bench4q.master.domain.entity.Agent; -import org.bench4q.master.domain.repository.AgentRepository; -import org.bench4q.master.domain.service.AgentService; import org.bench4q.master.domain.valueobject.transaction.Transaction; import org.bench4q.master.domain.valueobject.transaction.exception.ScriptLoadDistributeException; import org.bench4q.master.exception.ExceptionLog; @@ -21,6 +19,7 @@ import org.bench4q.master.infrastructure.communication.AgentMessenger; import org.bench4q.master.infrastructure.highavailable.AgentRunBlotter; import org.bench4q.master.infrastructure.highavailable.HighAvailablePool; import org.bench4q.share.models.agent.RunScenarioModel; +import org.bench4q.share.models.agent.RunScenarioResultModel; import org.bench4q.share.models.agent.StopTestModel; public abstract class ScriptLoadBase implements Transaction { @@ -29,7 +28,6 @@ public abstract class ScriptLoadBase implements Transaction { private List agentListThisTime; private AgentMessenger runningAgentService; private HighAvailablePool highAvailableAgentPool; - private AgentRepository agentRepository; private static Logger logger = Logger .getLogger(ScriptLoadApplication.class); @@ -75,14 +73,6 @@ public abstract class ScriptLoadBase implements Transaction { this.highAvailableAgentPool = highAvailableAgentPool; } - private AgentRepository getAgentRepository() { - return agentRepository; - } - - private void setAgentRepository(AgentRepository agentRepository) { - this.agentRepository = agentRepository; - } - public static Logger getLogger() { return logger; } @@ -103,8 +93,6 @@ public abstract class ScriptLoadBase implements Transaction { .getBean(AgentMessenger.class)); this.setHighAvailableAgentPool(ApplicationContextHelper.getContext() .getBean(HighAvailablePool.class)); - this.setAgentRepository(ApplicationContextHelper - .getBean(AgentRepository.class)); this.setAgentListThisTime(new ArrayList()); } @@ -122,8 +110,8 @@ public abstract class ScriptLoadBase implements Transaction { + this.getRunningScript().getScriptId()); throw new RuntimeException("Running Script not valid!"); } - this.runAgentsWithScenario(getRequireLoad(), this - .getRunningScript().getScriptId(), this.getTestPlanRunID()); + this.applyForLoad(getRequireLoad(), this.getRunningScript() + .getScriptId(), this.getTestPlanRunID()); return this.getAgentListThisTime(); } catch (Exception e) { @@ -142,11 +130,10 @@ public abstract class ScriptLoadBase implements Transaction { protected abstract int getRequireLoad(); - private void runAgentsWithScenario(int totalRequireLoad, int scriptId, + private void applyForLoad(int totalRequireLoad, int scriptId, UUID testPlanId) throws JAXBException, ScriptLoadDistributeException { - // RunScenarioResultModel runScenarioResultModel = new - // RunScenarioResultModel(); + RunScenarioResultModel runScenarioResultModel = null; int loadForRunCurrent; if (totalRequireLoad >= this.getHighAvailableAgentPool() @@ -154,7 +141,6 @@ public abstract class ScriptLoadBase implements Transaction { logger.info("currentAvailableLoad not enough for substitute"); return; } - for (Agent agent : this.getHighAvailableAgentPool().getPool().values()) { if (allocationFinish(totalRequireLoad)) { break; @@ -164,20 +150,20 @@ public abstract class ScriptLoadBase implements Transaction { continue; } loadForRunCurrent = getMin(totalRequireLoad, agent.getRemainLoad()); - // runScenarioResultModel = agent.runWithouParams(runScenarioModel); - // if (runScenarioResultModel == null) { - // continue; - // } - agent.setCurrentStatus(AgentService.AGENT_STATUS_InRun); - agent.setRemainLoad(agent.getRemainLoad() - loadForRunCurrent); - updateAgentInRepository(agent); + runScenarioResultModel = agent.bookTest(loadForRunCurrent); + if (runScenarioResultModel == null + || runScenarioResultModel.getRunId() == null) { + logger.error(runScenarioResultModel == null ? "runScenarioResultModel is null" + : "runScenarioResultModel.getRunId()" + + runScenarioResultModel.getRunId()); + continue; + } RunningAgentInterface runningAgent = AbstractRunningAgent .buildRunningAgentDBWithoutId(agent, loadForRunCurrent, - this.getRunningScript(), null); + this.getRunningScript(), + runScenarioResultModel.getRunId()); this.getAgentListThisTime().add(runningAgent); - // This one should move to runningAgent.run(); - - updateAgentBlotterInHA(testPlanId, runningAgent); + addAgentBlotterToHA(testPlanId, runningAgent); totalRequireLoad -= loadForRunCurrent; } if (!allocationFinish(totalRequireLoad)) { @@ -187,15 +173,12 @@ public abstract class ScriptLoadBase implements Transaction { } } - private void updateAgentInRepository(Agent agent) { - this.getAgentRepository().update(agent); - } - private boolean allocationFinish(int requireLoad) { return requireLoad <= 0; } private boolean inUse(Agent agent) { + logger.info(agent.getCurrentStatus() + " : " + agent.getRemainLoad()); return agent.getRemainLoad() < agent.getMaxLoad(); } @@ -204,7 +187,7 @@ public abstract class ScriptLoadBase implements Transaction { : remainLoadByStart; } - private void updateAgentBlotterInHA(UUID testPlanId, + private void addAgentBlotterToHA(UUID testPlanId, RunningAgentInterface runningAgent) { this.getHighAvailableAgentPool() .getAgentRunBlotters() diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/communication/AgentMessenger.java b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/communication/AgentMessenger.java index 47ed70d3..b4d3bc86 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/communication/AgentMessenger.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/communication/AgentMessenger.java @@ -1,16 +1,12 @@ package org.bench4q.master.infrastructure.communication; import java.io.File; -import java.io.IOException; import java.util.LinkedList; import java.util.List; import java.util.UUID; -import javax.xml.bind.JAXBException; - import org.apache.log4j.Logger; import org.bench4q.master.domain.entity.Agent; -import org.bench4q.master.exception.ExceptionLog; import org.bench4q.master.infrastructure.highavailable.faultolerence.FaultTolerenceFactory; import org.bench4q.share.communication.HttpRequester; import org.bench4q.share.communication.HttpRequester.HttpResponse; @@ -20,10 +16,6 @@ import org.bench4q.share.models.agent.RunScenarioResultModel; import org.bench4q.share.models.agent.ServerStatusModel; import org.bench4q.share.models.agent.StopTestModel; import org.bench4q.share.models.agent.TestBriefStatusModel; -import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; -import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel; -import org.bench4q.share.models.agent.statistics.AgentPageBriefModel; -import org.bench4q.share.models.agent.statistics.AgentPagesBriefModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -41,50 +33,43 @@ public class AgentMessenger { this.httpRequester = httpRequester; } - public RunScenarioResultModel runWithoutParams(Agent agent, - RunScenarioModel runScenarioModel) throws IOException { - HttpResponse httpResponse = null; - try { - httpResponse = this.httpRequester.sendPostXml(buildBaseUrl(agent) - + "/test/runWithoutParams", MarshalHelper.marshal( - RunScenarioModel.class, runScenarioModel), null); - if (HttpRequester.isInvalidResponse(httpResponse)) { - return null; - } - return (RunScenarioResultModel) MarshalHelper.unmarshal( - RunScenarioResultModel.class, httpResponse.getContent()); - } catch (JAXBException e) { - logIt(httpResponse, e); - FaultTolerenceFactory.getRunAgentFault(agent, runScenarioModel) - .doTolerance(); - return null; - } - - } - private void logIt(HttpResponse httpResponse, Throwable e) { logger.error(HttpRequester.isInvalidResponse(httpResponse) ? "" : httpResponse.getContent(), e); } - public RunScenarioResultModel submitScenrioWithParams(Agent agent, - List paramFiles, final RunScenarioModel runScenarioModel) { + public RunScenarioResultModel bookTest(Agent agent, int requireLoad) { HttpResponse httpResponse = null; try { - final String modelCOntent = MarshalHelper + httpResponse = this.getHttpRequester().sendGet( + buildBaseUrl(agent) + "/test/bookTest/" + requireLoad, + null, null); + System.out.println(buildBaseUrl(agent) + "/test/bookTest/" + + requireLoad); + return (RunScenarioResultModel) MarshalHelper.unmarshal( + RunScenarioResultModel.class, httpResponse.getContent()); + } catch (Exception e) { + logger.error(httpResponse.getContent(), e); + return null; + } + } + + public RunScenarioResultModel submitScenrioWithParams(Agent agent, + UUID agentRunId, List paramFiles, + final RunScenarioModel runScenarioModel) { + HttpResponse httpResponse = null; + try { + final String modelContent = MarshalHelper .tryMarshal(runScenarioModel); httpResponse = this.httpRequester.postFiles(null, - buildBaseUrl(agent) + "/test/submitScenarioWithParams", - "files[]", paramFiles, "scenarioModel", - new LinkedList() { + buildBaseUrl(agent) + "/test/submitScenarioWithParams/" + + agentRunId, "files[]", paramFiles, + "scenarioModel", new LinkedList() { private static final long serialVersionUID = 1L; { - add(modelCOntent); + add(modelContent); } }); - if (HttpRequester.isInvalidResponse(httpResponse)) { - return null; - } return (RunScenarioResultModel) MarshalHelper.unmarshal( RunScenarioResultModel.class, httpResponse.getContent()); } catch (Exception e) { @@ -139,29 +124,6 @@ public class AgentMessenger { } } - public AgentBriefStatusModel brief(Agent agent, UUID agentRunId) { - HttpResponse httpResponse = null; - try { - if (agent == null || agentRunId == null) { - return null; - } - httpResponse = this.httpRequester.sendGet(buildBaseUrl(agent) - + "/test/brief/" + agentRunId, null, null); - if (HttpRequester.isInvalidResponse(httpResponse)) { - return null; - } - logger.info(httpResponse.getContent()); - return (AgentBriefStatusModel) MarshalHelper.unmarshal( - AgentBriefStatusModel.class, httpResponse.getContent()); - } catch (Exception e) { - logIt(httpResponse, e); - logger.error(e.toString() + " When brief the agent with hostName " - + agent.getHostName()); - FaultTolerenceFactory.getBriefFaultTolerance(agent).doTolerance(); - return null; - } - } - public StopTestModel stop(Agent agent, UUID runId) { HttpResponse httpResponse; try { @@ -187,61 +149,6 @@ public class AgentMessenger { return agent.getHostName() + ":" + agent.getPort(); } - public AgentBehaviorsBriefModel behaviorsBrief(Agent agent, UUID runId) { - try { - HttpResponse httpResponse = this.getHttpRequester().sendGet( - buildBaseUrl(agent) + "/test/behaviorsBrief/" - + runId.toString(), null, null); - if (HttpRequester.isInvalidResponse(httpResponse)) { - return null; - } - logger.info(httpResponse.getContent()); - return (AgentBehaviorsBriefModel) MarshalHelper.unmarshal( - AgentBehaviorsBriefModel.class, httpResponse.getContent()); - } catch (Exception e) { - logger.error(ExceptionLog.getStackTrace(e) + " When behaviorsBrief"); - FaultTolerenceFactory.getBriefFaultTolerance(agent).doTolerance(); - return null; - } - } - - public AgentPageBriefModel pageBrief(Agent agent, UUID runId, int pageId) { - try { - HttpResponse httpResponse = this.getHttpRequester().sendGet( - buildBaseUrl(agent) + "/test/pageBrief/" + runId.toString() - + "/" + pageId, null, null); - if (HttpRequester.isInvalidResponse(httpResponse)) { - return null; - } - - return (AgentPageBriefModel) MarshalHelper.unmarshal( - AgentPageBriefModel.class, httpResponse.getContent()); - } catch (Exception e) { - logger.error(ExceptionLog.getStackTrace(e)); - FaultTolerenceFactory.getBriefFaultTolerance(agent).doTolerance(); - return null; - } - } - - public AgentPagesBriefModel pagesBrief(Agent agent, UUID runId) { - try { - HttpResponse httpResponse = this.getHttpRequester().sendGet( - buildBaseUrl(agent) + "/test/pagesBrief/" - + runId.toString(), null, null); - if (HttpRequester.isInvalidResponse(httpResponse)) { - return null; - } - logger.info(httpResponse.getContent()); - return (AgentPagesBriefModel) MarshalHelper.unmarshal( - AgentPagesBriefModel.class, httpResponse.getContent()); - } catch (Exception e) { - logger.error(ExceptionLog.getStackTrace(e)); - FaultTolerenceFactory.getBriefFaultTolerance(agent).doTolerance(); - return null; - } - - } - public ServerStatusModel askLiving(String hostName, int port) { try { HttpResponse httpResponse = this.getHttpRequester().sendGet( diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/highavailable/HighAvailablePool.java b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/highavailable/HighAvailablePool.java index a630eef8..22ea1ade 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/highavailable/HighAvailablePool.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/highavailable/HighAvailablePool.java @@ -11,6 +11,7 @@ import org.apache.log4j.Logger; import org.bench4q.master.domain.RunningAgentInterface; import org.bench4q.master.domain.RunningScriptInterface; import org.bench4q.master.domain.entity.Agent; +import org.bench4q.master.domain.repository.AgentRepository; import org.bench4q.master.domain.service.AgentService; import org.bench4q.master.infrastructure.communication.AgentMessenger; import org.bench4q.share.models.agent.ServerStatusModel; @@ -20,7 +21,7 @@ import org.springframework.stereotype.Component; @Component public class HighAvailablePool extends CurrentLoadSubject { - private AgentService agentService; + private AgentRepository agentRepository; private AgentMessenger agentMessenger; private Map pool; private Map agentStatusOfPreviousBeatMap; @@ -38,11 +39,6 @@ public class HighAvailablePool extends CurrentLoadSubject { this.pool = pool; } - @Autowired - public void setAgentService(AgentService agentService) { - this.agentService = agentService; - } - private AgentMessenger getAgentMessenger() { return agentMessenger; } @@ -52,6 +48,15 @@ public class HighAvailablePool extends CurrentLoadSubject { this.agentMessenger = agentMessenger; } + private AgentRepository getAgentRepository() { + return agentRepository; + } + + @Autowired + private void setAgentRepository(AgentRepository agentRepository) { + this.agentRepository = agentRepository; + } + public Long getMaxAvailableLoad() { this.calculateHAPoolLoadStatusInMonopolize(); return maxAvailableLoad; @@ -102,7 +107,7 @@ public class HighAvailablePool extends CurrentLoadSubject { @Scheduled(cron = "0,30 */1 * * * *") public void timerTask() { - synchronized (this.agentService.getAgentLock()) { + synchronized (this.getAgentRepository().getAddDeleteLock()) { synchronized (this.getPool()) { heartBeatsAndUpdateHAPool(); doSubstituteIfRequired(); @@ -142,9 +147,10 @@ public class HighAvailablePool extends CurrentLoadSubject { this.pool.clear(); this.setCurrentAvailableLoad(0); this.setMaxAvailableLoad((long) 0); - for (Agent agent : this.agentService.loadAgentPoolFromDB()) { + for (Agent agent : this.getAgentRepository().loadEntities()) { checkHeartBeat(agent); } + this.getAgentRepository().Update(this.getPool().values()); } public void checkHeartBeat(Agent agent) { @@ -174,14 +180,13 @@ public class HighAvailablePool extends CurrentLoadSubject { private void updateAgentStatus(int status, Agent agent) { agent.setCurrentStatus(status); // TODO: update this all together to db - this.agentService.updateAgentStatus(status, agent.getHostName()); + // this.getAgentRepository().update(agent); } private void doForHealth(ServerStatusModel newModel, Agent agent) { List agentUnfinishedRunIds = newModel.getRunningTests(); if (agentUnfinishedRunIds == null || agentUnfinishedRunIds.size() == 0) { - // TODO:remove this function - // doForInIdle(agent); + doForInIdle(agent); return; } doForInRunning(agent, newModel); @@ -192,11 +197,11 @@ public class HighAvailablePool extends CurrentLoadSubject { this.agentStatusOfPreviousBeatMap.put(agent.getHostName(), newModel); } - // private void doForInIdle(Agent agent) { - // - // updateAgentStatus(AgentService.AGENT_STATUS_Idel, agent); - // this.agentService.resetAgent(agent); - // } + private void doForInIdle(Agent agent) { + agent.setCurrentStatus(AgentService.AGENT_STATUS_Idel); + agent.resetLoad(); + // this.getAgentRepository().update(agent); + } private List queryUnfinishedTest(ServerStatusModel newModel, String hostName) { diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java b/Bench4Q-Master/src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java index a1a1a69c..cda31739 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java @@ -211,6 +211,9 @@ public class TestBase_MakeUpTestPlan extends TestBase { protected void cleanUpForTestPlanRunning() { TestPlan testPlan = this.getTestPlanRepository().getTestPlanInDomainBy( getTestPlanRunIdUuid()); + if (testPlan == null) { + return; + } testPlan.setCurrentStatus(TestPlanStatus.Complete.name()); RunningAgentDB runningAgent = extractRunningAgent( testPlan.extracSpecifiedScript(getScriptId()), @@ -322,7 +325,8 @@ public class TestBase_MakeUpTestPlan extends TestBase { protected void deleteTestPlan() { this.getTestPlanRepository().detach( this.getTestPlanRepository() - .getTestPlanInDomainBy(this.getTestPlanRunIdUuid()).getId()); + .getTestPlanInDomainBy(this.getTestPlanRunIdUuid()) + .getId()); } protected ScriptBriefResultModel buildScriptBriefResultModel(int i) { diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/test/communication/Test_AgentMessenger.java b/Bench4Q-Master/src/test/java/org/bench4q/master/test/communication/Test_AgentMessenger.java index 0d4c6922..7fef412e 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/test/communication/Test_AgentMessenger.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/test/communication/Test_AgentMessenger.java @@ -20,7 +20,6 @@ import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.agent.RunScenarioModel; import org.bench4q.share.models.agent.RunScenarioResultModel; import org.bench4q.share.models.agent.TestBriefStatusModel; -import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -32,7 +31,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; public class Test_AgentMessenger extends TestBase_MakeUpTestPlan { private static final String FILE_SEPARATOR = System .getProperty("file.separator"); - private static final String Test_HOSTNAME = "http://133.133.12.4"; + private static final String Test_HOSTNAME = "http://127.0.0.1"; private static final int Test_PORT = 6565; private Logger logger = Logger.getLogger(Test_AgentMessenger.class); private AgentMessenger agentMessenger; @@ -49,6 +48,7 @@ public class Test_AgentMessenger extends TestBase_MakeUpTestPlan { @Test public void testSubmitScenarioWithParamsAndRun() throws IOException, JAXBException { + List paramFiles = new ArrayList(); int scriptId = getUserFirstScript(this.getUserRepository().getUser( "admin")); @@ -68,12 +68,17 @@ public class Test_AgentMessenger extends TestBase_MakeUpTestPlan { .unmarshal(RunScenarioModel.class, FileUtils.readFileToString(scenarioFile)); inputModel.setPoolSize(20); + Agent agent = Agent.createAgentWithoutId(Test_HOSTNAME, Test_PORT); - RunScenarioResultModel model = this.getAgentMessenger() - .submitScenrioWithParams(agent, paramFiles, inputModel); + RunScenarioResultModel model = this.getAgentMessenger().bookTest(agent, + 20); assertNotNull(model); assertNotNull(model.getRunId()); System.out.println(model.getRunId()); + RunScenarioResultModel modelAfter = this.getAgentMessenger() + .submitScenrioWithParams(agent, model.getRunId(), paramFiles, + inputModel); + assertEquals(model.getRunId(), modelAfter.getRunId()); model = this.getAgentMessenger().runWithParams(agent, model.getRunId()); assertNotNull(model); assertNotNull(model.getRunId()); @@ -131,8 +136,4 @@ public class Test_AgentMessenger extends TestBase_MakeUpTestPlan { cleanUpForTestPlanRunning(); } - @After - public void clear() { - cleanUpForTestPlanRunning(); - } } diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/test/controller/TestPlanControllerTest.java b/Bench4Q-Master/src/test/java/org/bench4q/master/test/controller/TestPlanControllerTest.java deleted file mode 100644 index 7449d4d9..00000000 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/test/controller/TestPlanControllerTest.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.bench4q.master.test.controller; - -import java.io.IOException; - -import javax.xml.bind.JAXBException; - -import org.bench4q.master.test.TestBase_MakeUpTestPlan; -import org.bench4q.master.test.testplan.TestPlanTester; -import org.bench4q.share.communication.HttpRequester.HttpResponse; -import org.bench4q.share.helper.MarshalHelper; -import org.bench4q.share.models.master.TestPlanModel; -import org.junit.Test; - -import static org.junit.Assert.*; - -public class TestPlanControllerTest extends TestBase_MakeUpTestPlan { - private String url = BASE_URL + "/testPlan"; - - public TestPlanControllerTest() { - } - - @Test - public void runTestPlanWithoutLogOn() throws JAXBException, IOException { - HttpResponse httpResponse = this.httpRequester.sendPostXml(this.url - + "/runTestPlanWithTestPlanModel", MarshalHelper.marshal( - TestPlanModel.class, - createATestPlanWithOneScript(TestPlanTester.SCRIPTID1)), null); - System.out.println(httpResponse.getContent()); - assertEquals(400, httpResponse.getCode()); - } - - @Test - public void runTestPlanWithNullMonitorList() throws IOException, - JAXBException { - HttpResponse httpResponse = this.httpRequester - .sendPostXml( - this.url + "/runTestPlanWithTestPlanModel", - MarshalHelper - .marshal( - TestPlanModel.class, - TestPlanTester - .createATestPlanWithNullMonitorList(TestPlanTester.SCRIPTID1)), - makeAccessTockenMap(accessTocken)); - System.out.println(httpResponse.getContent()); - assertEquals(200, httpResponse.getCode()); - } - - public void runTestPlanWithZeroMonitor() throws IOException, JAXBException { - HttpResponse httpResponse = this.httpRequester - .sendPostXml( - this.url + "/runTestPlanWithTestPlanModel", - MarshalHelper - .marshal( - TestPlanModel.class, - TestPlanTester - .createATestPlanWithZeroMonitor(TestPlanTester.SCRIPTID1)), - makeAccessTockenMap(accessTocken)); - System.out.println(httpResponse.getContent()); - assertEquals(200, httpResponse.getCode()); - } - @Test - public void testLoadTestPlans() throws IOException, JAXBException{ - HttpResponse httpResponse = this.httpRequester.sendGet(this.url - + "/loadTestPlans",null, this.makeAccessTockenMap(this.login())); - System.out.println(httpResponse.getContent()); - } -} diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/test/testplan/TestPlanTester.java b/Bench4Q-Master/src/test/java/org/bench4q/master/test/controller/Test_TestPlanController.java similarity index 98% rename from Bench4Q-Master/src/test/java/org/bench4q/master/test/testplan/TestPlanTester.java rename to Bench4Q-Master/src/test/java/org/bench4q/master/test/controller/Test_TestPlanController.java index 386fe1c9..5809b529 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/test/testplan/TestPlanTester.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/test/controller/Test_TestPlanController.java @@ -1,4 +1,4 @@ -package org.bench4q.master.test.testplan; +package org.bench4q.master.test.controller; import static org.junit.Assert.*; @@ -15,7 +15,6 @@ import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import org.bench4q.master.test.TestBase_MakeUpTestPlan; -import org.bench4q.master.test.controller.TestBase; import org.bench4q.share.communication.HttpRequester; import org.bench4q.share.communication.HttpRequester.HttpResponse; import org.bench4q.share.enums.master.TestPlanStatus; @@ -36,7 +35,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:service-test-context.xml" }) -public class TestPlanTester extends TestBase_MakeUpTestPlan { +public class Test_TestPlanController extends TestBase_MakeUpTestPlan { private TestPlanModel testPlanBusinessModel = new TestPlanModel(); private String _url = TestBase.BASE_URL + "/testPlan"; private int scriptSumNum; @@ -62,7 +61,7 @@ public class TestPlanTester extends TestBase_MakeUpTestPlan { } @SuppressWarnings("resource") - public TestPlanTester() { + public Test_TestPlanController() { new ClassPathXmlApplicationContext("classpath:service-test-context.xml"); } diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/test/testplan/TestTestPlanResult.java b/Bench4Q-Master/src/test/java/org/bench4q/master/test/controller/Test_TestPlanResultController.java similarity index 86% rename from Bench4Q-Master/src/test/java/org/bench4q/master/test/testplan/TestTestPlanResult.java rename to Bench4Q-Master/src/test/java/org/bench4q/master/test/controller/Test_TestPlanResultController.java index 584eb0ac..11bd4142 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/test/testplan/TestTestPlanResult.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/test/controller/Test_TestPlanResultController.java @@ -1,4 +1,4 @@ -package org.bench4q.master.test.testplan; +package org.bench4q.master.test.controller; import static org.junit.Assert.*; @@ -6,13 +6,12 @@ import java.io.IOException; import javax.xml.bind.JAXBException; -import org.bench4q.master.test.controller.TestBase; import org.bench4q.share.communication.HttpRequester.HttpResponse; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.master.ResultLoadModel; import org.junit.Test; -public class TestTestPlanResult extends TestBase { +public class Test_TestPlanResultController extends TestBase { private static String URL = BASE_URL + "/TestPlanResult"; private static String testPlanRunId = "0cdc3398-5b61-4bff-be48-8075e5d2fa64"; diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/test/testplan/Test_LoadDistribute.java b/Bench4Q-Master/src/test/java/org/bench4q/master/test/domain/testPlan/Test_LoadDistribute.java similarity index 98% rename from Bench4Q-Master/src/test/java/org/bench4q/master/test/testplan/Test_LoadDistribute.java rename to Bench4Q-Master/src/test/java/org/bench4q/master/test/domain/testPlan/Test_LoadDistribute.java index 6bcc0d7f..ccc29dff 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/test/testplan/Test_LoadDistribute.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/test/domain/testPlan/Test_LoadDistribute.java @@ -1,4 +1,4 @@ -package org.bench4q.master.test.testplan; +package org.bench4q.master.test.domain.testPlan; import static org.junit.Assert.*; diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/test/repository/Test_UserRepository.java b/Bench4Q-Master/src/test/java/org/bench4q/master/test/repository/Test_UserRepository.java index 1b17f9e0..41de1f72 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/test/repository/Test_UserRepository.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/test/repository/Test_UserRepository.java @@ -86,11 +86,6 @@ public class Test_UserRepository { assertNotNull(this.getUserRepository().getUser("test1")); } - @Test - public void testGetEntity() { - assertNotNull(this.getUserRepository().getEntity(5)); - } - @Test public void testUpdateEntity() { // This test can reference to the test Of the diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_TestPlanEngine.java b/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_TestPlanEngine.java index 0c517b59..aae05444 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_TestPlanEngine.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_TestPlanEngine.java @@ -4,6 +4,7 @@ import java.util.Date; import org.bench4q.master.Main; 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.test.TestBase_MakeUpTestPlan; import org.bench4q.share.enums.master.TestPlanStatus; @@ -38,12 +39,6 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan { testForStatus(status); } - @Test - public void testInRunning() throws Exception { - testForStatus(TestPlanStatus.InRunning); - } - - @Test public void testRunTestPlanRightly() throws InterruptedException { assertNotNull(this.getAgentMessenger().askLiving(Test_AGENT_HOSTNAME, @@ -52,6 +47,7 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan { int scriptId = getUserFirstScript(user); this.getHaPool().timerTask(); Date dateBeforeRun = new Date(); + Thread.sleep(1000); this.setTestPlanRunIdUuid(this.getTestPlanEngine().runWith( createATestPlanWithOneScript(scriptId), user)); assertNotNull(getTestPlanRunIdUuid()); @@ -65,7 +61,10 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan { TestPlanStatus.valueOf(getTestPlanRepository() .getRunningTestPlanBy(getTestPlanRunIdUuid()) .getCurrentStatus())); - + for (TestPlanScript testPlanScript : testPlan.getTestPlanScripts()) { + testPlanScript.doForComplete(); + } + this.getTestPlanEngine().doTaskComplete(getTestPlanRunIdUuid()); } @Test diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptService.java b/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptService.java index ed05e152..32b82166 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptService.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptService.java @@ -48,17 +48,6 @@ public class Test_TestPlanScriptService extends TestBase_MakeUpTestPlan { assertTrue(scriptBriefResultModels.size() == 0); } - @Test - public void testLoadScriptResultRightly() { - TestPlanScript testPlanScript = this.getTestPlanScriptService() - .getTestPlanScript(getScriptId(), getTestPlanRunIdUuid()); - assertNotNull(testPlanScript); - List scriptBriefList = this - .getTestPlanScriptService().queryScriptBriefResults( - testPlanScript); - assertTrue(scriptBriefList.size() > 0); - } - @Test public void testSaveScriptBriefResultWithWrongScriptIdOrRunId() { ScriptBriefResultModel briefResultModel = new ScriptBriefResultModel(); diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_TestPlanService.java b/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_TestPlanService.java index 9d2aee00..2bd8bced 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_TestPlanService.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/test/service/Test_TestPlanService.java @@ -83,7 +83,7 @@ public class Test_TestPlanService extends TestBase_MakeUpTestPlan { submitATestPlanWithTwoScript(); int sizeAfterSubmitTestPlanWithTwoScript = this.getTestPlanRepository() .loadEntities(user).size(); - assertEquals(initialTestPlanSize + 2, + assertEquals(initialTestPlanSize + 1, sizeAfterSubmitTestPlanWithTwoScript); } diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/test/transaction/Test_ScriptLoadCommand.java b/Bench4Q-Master/src/test/java/org/bench4q/master/test/transaction/Test_ScriptLoadCommand.java index 02aeb031..df7f99d2 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/test/transaction/Test_ScriptLoadCommand.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/test/transaction/Test_ScriptLoadCommand.java @@ -63,7 +63,7 @@ public class Test_ScriptLoadCommand extends TestBase_MakeUpTestPlan { assertNotNull(runningAgents); assertNotNull(runningAgents.get(0)); - assertNull(runningAgents.get(0).getAgentRunId()); + assertNotNull(runningAgents.get(0).getAgentRunId()); this.setAgentRunId(runningAgents.get(0).getAgentRunId()); assertEquals(EACH_SCRIPT_LOAD_SMALLSCALE, runningAgents.get(0) .getLoadInUse());