refactor testplan's current status from static byte to enum

This commit is contained in:
Tienan Chen 2013-11-05 16:06:29 +08:00
parent 37b0afa18c
commit 8629eab282
11 changed files with 67 additions and 38 deletions

View File

@ -194,7 +194,7 @@ public class ScriptController extends BaseController {
return null;
}
return this.buildReponseModel(true, null, null, 0, null, this
.getScriptService().getScriptById(scriptId), null);
.getScriptService().getScript(scriptId), null);
}
@RequestMapping(value = "/queryScriptByName", method = { RequestMethod.GET,

View File

@ -115,8 +115,9 @@ public class TestPlanController extends BaseController {
testPlanModel.getMonitorModles());
}
private TestPlanResultModel buildResponseModel(Byte currentStatus,
UUID testPlanId, List<RunningScriptModel> runningScriptModels,
private TestPlanResultModel buildResponseModel(
TestPlanStatus currentStatus, UUID testPlanId,
List<RunningScriptModel> runningScriptModels,
List<MonitorModel> monitorModels) {
TestPlanResultModel result = new TestPlanResultModel();
result.setCurrentStatus(currentStatus);

View File

@ -0,0 +1,5 @@
package org.bench4q.master.api;
public enum TestPlanStatus {
NotStart, InRunning, Complete, PendingNoEnoughMaxLoad, PendingNoEnoughCurrentLoad, Error
}

View File

@ -7,10 +7,12 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import org.bench4q.master.api.TestPlanStatus;
@XmlRootElement(name = "RunTestPlanResultModel")
public class TestPlanResultModel {
private UUID testPlanId;
private Byte currentStatus;
private TestPlanStatus currentStatus;
private List<RunningScriptModel> runningScriptModels;
private List<MonitorModel> monitorModels;
@ -24,11 +26,11 @@ public class TestPlanResultModel {
}
@XmlElement(name = "currentStatus")
public Byte getCurrentStatus() {
public TestPlanStatus getCurrentStatus() {
return currentStatus;
}
public void setCurrentStatus(Byte currentStatus) {
public void setCurrentStatus(TestPlanStatus currentStatus) {
this.currentStatus = currentStatus;
}

View File

@ -23,7 +23,7 @@ public class TestPlan {
private User user;
private String testPlanRunId;
private String testPlanModelContent;
private Byte currentStatus;
private String currentStatus;
private int failTimes;
private boolean reportCreated;
@ -89,11 +89,11 @@ public class TestPlan {
}
@Column(name = "currentStatus", nullable = false)
public Byte getCurrentStatus() {
public String getCurrentStatus() {
return currentStatus;
}
public void setCurrentStatus(Byte currentStatus) {
public void setCurrentStatus(String currentStatus) {
this.currentStatus = currentStatus;
}

View File

@ -68,11 +68,10 @@ public class ScriptService {
}
}
public Script getScriptById(int scriptId) {
public Script getScript(int scriptId) {
Session session = this.sessionHelper.openSession();
try {
return (Script) session.createCriteria(Script.class)
.add(Restrictions.eq("id", scriptId)).uniqueResult();
return doGetScript(scriptId, session);
} catch (Exception e) {
return null;
} finally {
@ -81,6 +80,11 @@ public class ScriptService {
}
}
public static Script doGetScript(int scriptId, Session session) {
return (Script) session.createCriteria(Script.class)
.add(Restrictions.eq("id", scriptId)).uniqueResult();
}
private int getScriptBehaviorsCount(String scriptContent)
throws JAXBException {
int temp = 0;

View File

@ -71,7 +71,7 @@ public class TestPlanScriptService {
return (TestPlanScript) session
.createCriteria(TestPlanScript.class)
.add(Restrictions.eq("script", this.getScriptService()
.getScriptById(scriptId)))
.getScript(scriptId)))
.add(Restrictions.eq("testPlan",
this.testPlanService.getTestPlan(testPlanRunId)))
.uniqueResult();

View File

@ -11,6 +11,7 @@ import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.apache.log4j.Logger;
import org.bench4q.master.api.TestPlanStatus;
import org.bench4q.master.api.model.RunningScriptModel;
import org.bench4q.master.api.model.TestPlanModel;
import org.bench4q.master.api.model.TestScriptConfig;
@ -32,12 +33,13 @@ public class TestPlanService {
private static Logger logger = Logger.getLogger(TestPlanService.class);
public static final long TIME_UNIT = 1000;
public static final Byte TEST_PLAN_STATUS_IN_RUNNING = 0;
public static final Byte TEST_PLAN_STATUS_NOT_START = 1;
public static final Byte TEST_PLAN_STATUS_COMPLETE = 2;
public static final Byte TEST_PLAN_STATUS_ERROR = 3;
public static final Byte TEST_PLAN_STATUS_ERROR_NO_ENOUGH_MAXLOAD = 4;
public static final Byte TEST_PLAN_STATUS_PENDING_NO_ENOUGH_CURRENT_LOAD = 5;
// public static final Byte TEST_PLAN_STATUS_IN_RUNNING = 0;
// public static final Byte TEST_PLAN_STATUS_NOT_START = 1;
// public static final Byte TEST_PLAN_STATUS_COMPLETE = 2;
// public static final Byte TEST_PLAN_STATUS_ERROR = 3;
// public static final Byte TEST_PLAN_STATUS_PENDING_NO_ENOUGH_MAXLOAD = 4;
// public static final Byte TEST_PLAN_STATUS_PENDING_NO_ENOUGH_CURRENT_LOAD
// = 5;
public SessionHelper getSessionHelper() {
return sessionHelper;
@ -85,17 +87,16 @@ public class TestPlanService {
private TestPlan saveToTestPlan(Session session,
TestPlanModel testPlanModel, User user, UUID testPlanRunId) {
TestPlan result = new TestPlan();
TestPlan testPlan = new TestPlan();
testPlan.setCreateDateTime(new Date());
testPlan.setName(testPlanModel.getName());
testPlan.setUser(user);
testPlan.setTestPlanRunId(testPlanRunId.toString());
testPlan.setCurrentStatus(TEST_PLAN_STATUS_NOT_START);
testPlan.setCurrentStatus(TestPlanStatus.NotStart.name());
testPlan.setFailTimes(0);
testPlan.setTestPlanModelContent(marshalTestPlanModel(testPlanModel));
result = (TestPlan) session.merge(testPlan);
return result;
return (TestPlan) session.merge(testPlan);
}
private boolean saveToTestPlanScriptAndScriptConfig(Session session,
@ -107,7 +108,7 @@ public class TestPlanService {
testPlanScript = new TestPlanScript();
testPlanScript.setTestPlan(testPlan);
testPlanScript.setScript(this.scriptService
.getScriptById(runningScriptModel.getScriptId()));
.getScript(runningScriptModel.getScriptId()));
testPlanScript.setRequireLoad(runningScriptModel.getRequireLoad());
TestPlanScript testPlanScriptInDB = (TestPlanScript) session
.merge(testPlanScript);
@ -145,7 +146,9 @@ public class TestPlanService {
TestPlanScript testPlanScript = (TestPlanScript) session
.createCriteria(TestPlanScript.class)
.add(Restrictions.eq("testPlan", testPlan))
.add(Restrictions.eq("scriptId", scriptId)).uniqueResult();
.add(Restrictions.eq("script",
ScriptService.doGetScript(scriptId, session)))
.uniqueResult();
if (testPlanScript == null) {
return -1;
}
@ -299,23 +302,35 @@ public class TestPlanService {
}
}
public void handleTestPlanPendingNoEnoughMaxLoad(UUID testPlanRunID) {
updateStatus(TestPlanStatus.PendingNoEnoughMaxLoad, testPlanRunID, 1);
}
public void handleTestPlanPendingNoEnoughCurrentLoad(UUID testPlanRunID) {
updateStatus(TestPlanStatus.PendingNoEnoughCurrentLoad, testPlanRunID,
1);
}
public void handleTestPlanError(UUID testPlanRunID) {
updateStatus(TEST_PLAN_STATUS_ERROR, testPlanRunID);
updateStatus(TestPlanStatus.Error, testPlanRunID, 1);
}
public void handleTestPlanRunning(UUID testPlanRunID) {
updateStatus(TEST_PLAN_STATUS_IN_RUNNING, testPlanRunID);
updateStatus(TestPlanStatus.InRunning, testPlanRunID, 0);
}
public void handleTestPlanComplete(UUID testPlanRunID) {
updateStatus(TEST_PLAN_STATUS_COMPLETE, testPlanRunID);
updateStatus(TestPlanStatus.Complete, testPlanRunID, 0);
}
private void updateStatus(Byte status, UUID testPlanRunID) {
private void updateStatus(TestPlanStatus status, UUID testPlanRunID,
int failTimes) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
doGetTestPlan(testPlanRunID, session).setCurrentStatus(status);
TestPlan testPlan = doGetTestPlan(testPlanRunID, session);
testPlan.setCurrentStatus(status.name());
testPlan.setFailTimes(testPlan.getFailTimes() + failTimes);
transaction.commit();
} catch (Exception e) {
logger.error(e.getMessage());
@ -329,17 +344,18 @@ public class TestPlanService {
}
}
public Byte queryTestPlanStatus(UUID testPlanRunID) {
public TestPlanStatus queryTestPlanStatus(UUID testPlanRunID) {
Session session = this.getSessionHelper().openSession();
try {
TestPlan testPlan = (TestPlan) session
.createCriteria(TestPlan.class)
.add(Restrictions.eq("testPlanRunId",
testPlanRunID.toString())).uniqueResult();
return testPlan == null ? -1 : testPlan.getCurrentStatus();
return testPlan == null ? null : TestPlanStatus.valueOf(testPlan
.getCurrentStatus());
} catch (Exception e) {
logger.error("there is an exception in queryTestPlanStatus!");
return -1;
return null;
} finally {
if (session != null) {
session.close();

View File

@ -100,7 +100,6 @@ public class TestPlanRunner {
}
private void doRunTestPlan(TestPlanContext testPlanContext, UUID testPlanId) {
if (!this.loadBallancer.generateLoadForTestPlan(testPlanContext,
testPlanId)) {
logger.error("there is a error happened in generateLoadForTestPlan");

View File

@ -123,6 +123,10 @@ public class ExecutionOverTask extends TimerTask {
this.getTestPlanContainer().getRunningTestPlans()
.remove(testPlanID);
this.getTestPlanService().handleTestPlanComplete(testPlanID);
logger.info("Test plan with id " + testPlanID.toString()
+ " finishes!");
// TODO: verify if the matters about this test plan has been
// cleaned.
}
}

View File

@ -14,6 +14,7 @@ import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.bench4q.master.api.TestPlanStatus;
import org.bench4q.master.api.model.MonitorModel;
import org.bench4q.master.api.model.RunningScriptModel;
import org.bench4q.master.api.model.ScriptBriefResultModel;
@ -21,7 +22,6 @@ import org.bench4q.master.api.model.TestScriptConfig;
import org.bench4q.master.api.model.TestPlanModel;
import org.bench4q.master.api.model.TestPlanResultModel;
import org.bench4q.master.communication.HttpRequester.HttpResponse;
import org.bench4q.master.service.TestPlanService;
public class TestPlanTester extends TestBase {
private TestPlanModel testPlan = new TestPlanModel();
@ -65,9 +65,7 @@ public class TestPlanTester extends TestBase {
InterruptedException {
UUID testPlanID = this.testWithTestPlanModel();
Thread.sleep(10000);
TestPlanResultModel testPlanResultModel = this
.getRunningInfo(testPlanID);
while (testPlanResultModel.getCurrentStatus() != TestPlanService.TEST_PLAN_STATUS_IN_RUNNING) {
while (this.getRunningInfo(testPlanID).getCurrentStatus() != TestPlanStatus.InRunning) {
Thread.sleep(6000);
}
for (int i = 0; i < 20; i++) {