Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,35 @@ class JobRunnerB implements JobRunner {
return null;
}
}
```
```
##TaskTracker的JobRunner测试
一般在编写TaskTracker的时候,只需要测试JobRunner的实现逻辑是否正确,又不想启动LTS进行远程测试。为了方便测试,LTS提供了JobRunner的快捷测试方法。自己的测试类集成`com.lts.tasktracker.runner.JobRunnerTester`即可,并实现`initContext`和`newJobRunner`方法即可。如`lts-example`中的例子:

```java
public class TestJobRunnerTester extends JobRunnerTester {

public static void main(String[] args) throws Throwable {
// 1. Mock Job 数据
Job job = new Job();
job.setTaskId("2313213");
// 2. 运行测试
TestJobRunnerTester tester = new TestJobRunnerTester();
Result result = tester.run(job);
System.out.println(JSONUtils.toJSONString(result));
}

@Override
protected void initContext() {
// TODO 初始化Spring容器等
}

@Override
protected JobRunner newJobRunner() {
return new TestJobRunner();
}
}
```

##SPI扩展说明
###LTS-Logger扩展
1. 引入`lts-logger-api-{version}.jar`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,105 +27,105 @@ private String appendContextMessage(String msg) {
public void trace(String msg, Throwable e) {
try {
logger.trace(appendContextMessage(msg), e);
} catch (Throwable t) {
} catch (Throwable ignored) {
}
}

public void trace(Throwable e) {
try {
logger.trace(e);
} catch (Throwable t) {
} catch (Throwable ignored) {
}
}

public void trace(String msg) {
try {
logger.trace(appendContextMessage(msg));
} catch (Throwable t) {
} catch (Throwable ignored) {
}
}

public void debug(String msg, Throwable e) {
try {
logger.debug(appendContextMessage(msg), e);
} catch (Throwable t) {
} catch (Throwable ignored) {
}
}

public void debug(Throwable e) {
try {
logger.debug(e);
} catch (Throwable t) {
} catch (Throwable ignored) {
}
}

public void debug(String msg) {
try {
logger.debug(appendContextMessage(msg));
} catch (Throwable t) {
} catch (Throwable ignored) {
}
}

public void info(String msg, Throwable e) {
try {
logger.info(appendContextMessage(msg), e);
} catch (Throwable t) {
} catch (Throwable ignored) {
}
}

public void info(String msg) {
try {
logger.info(appendContextMessage(msg));
} catch (Throwable t) {
} catch (Throwable ignored) {
}
}

public void warn(String msg, Throwable e) {
try {
logger.warn(appendContextMessage(msg), e);
} catch (Throwable t) {
} catch (Throwable ignored) {
}
}

public void warn(String msg) {
try {
logger.warn(appendContextMessage(msg));
} catch (Throwable t) {
} catch (Throwable ignored) {
}
}

public void error(String msg, Throwable e) {
try {
logger.error(appendContextMessage(msg), e);
} catch (Throwable t) {
} catch (Throwable ignored) {
}
}

public void error(String msg) {
try {
logger.error(appendContextMessage(msg));
} catch (Throwable t) {
} catch (Throwable ignored) {
}
}

public void error(Throwable e) {
try {
logger.error(e);
} catch (Throwable t) {
} catch (Throwable ignored) {
}
}

public void info(Throwable e) {
try {
logger.info(e);
} catch (Throwable t) {
} catch (Throwable ignored) {
}
}

public void warn(Throwable e) {
try {
logger.warn(e);
} catch (Throwable t) {
} catch (Throwable ignored) {
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.lts.example.support;

import com.lts.core.commons.utils.JSONUtils;
import com.lts.core.domain.Job;
import com.lts.tasktracker.Result;
import com.lts.tasktracker.runner.JobRunner;
import com.lts.tasktracker.runner.JobRunnerTester;

/**
* @author Robert HG (254963746@qq.com) on 9/13/15.
*/
public class TestJobRunnerTester extends JobRunnerTester {

public static void main(String[] args) throws Throwable {
// Mock Job 数据
Job job = new Job();
job.setTaskId("2313213");
// 运行测试
TestJobRunnerTester tester = new TestJobRunnerTester();
Result result = tester.run(job);
System.out.println(JSONUtils.toJSONString(result));
}

@Override
protected void initContext() {
// TODO 初始化Spring容器
}

@Override
protected JobRunner newJobRunner() {
return new TestJobRunner();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public class BizLoggerFactory {
* 保证一个TaskTracker只能有一个Logger, 因为一个jvm可以有多个TaskTracker
*/
public static BizLogger getLogger(Level level, RemotingClientDelegate remotingClient, TaskTrackerApplication application) {

// 单元测试的时候返回 Mock
if (Environment.UNIT_TEST == LTSConfig.getEnvironment()) {
return new MockBizLogger(level);
}

String key = application.getConfig().getIdentity();
BizLogger logger = BIZ_LOGGER_CONCURRENT_HASH_MAP.get(key);
if (logger == null) {
Expand All @@ -27,22 +33,12 @@ public static BizLogger getLogger(Level level, RemotingClientDelegate remotingCl
if (logger != null) {
return logger;
}
logger = create(level, remotingClient, application);
logger = new BizLoggerImpl(level, remotingClient, application);

BIZ_LOGGER_CONCURRENT_HASH_MAP.put(key, logger);
}
}
return logger;
}

/**
* 单元测试的时候返回 Mock
*/
private static BizLogger create(Level level, RemotingClientDelegate remotingClient, TaskTrackerApplication application) {
if (Environment.UNIT_TEST == LTSConfig.getEnvironment()) {
return new MockBizLogger(level);
}
return new BizLoggerImpl(level, remotingClient, application);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.lts.tasktracker.runner;

import com.lts.core.cluster.LTSConfig;
import com.lts.core.constant.Environment;
import com.lts.core.constant.Level;
import com.lts.core.domain.Job;
import com.lts.tasktracker.Result;
import com.lts.tasktracker.logger.BizLoggerFactory;

/**
* 为了方便JobRunner测试设计的
*
* @author Robert HG (254963746@qq.com) on 9/13/15.
*/
public abstract class JobRunnerTester {

public Result run(Job job) throws Throwable {
// 1. 设置LTS环境为 UNIT_TEST
LTSConfig.setEnvironment(Environment.UNIT_TEST);
// 设置 BizLogger
LtsLoggerFactory.setLogger(BizLoggerFactory.getLogger(Level.INFO, null, null));
// 2. load context (Spring Context 或者其他的)
initContext();
// 3. new jobRunner
JobRunner jobRunner = newJobRunner();
// 4. run job
return jobRunner.run(job);
}

/**
* 初始化上下文 (Spring Context等),准备运行环境
*/
protected abstract void initContext();

/**
* 创建JobRunner
*/
protected abstract JobRunner newJobRunner();

}