Skip to content

Commit 33aa6f2

Browse files
committed
添加 JobRunner的快捷测试用例
1 parent 65dc574 commit 33aa6f2

File tree

5 files changed

+124
-27
lines changed

5 files changed

+124
-27
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,35 @@ class JobRunnerB implements JobRunner {
336336
return null;
337337
}
338338
}
339-
```
339+
```
340+
##TaskTracker的JobRunner测试
341+
一般在编写TaskTracker的时候,只需要测试JobRunner的实现逻辑是否正确,又不想启动LTS进行远程测试。为了方便测试,LTS提供了JobRunner的快捷测试方法。自己的测试类集成`com.lts.tasktracker.runner.JobRunnerTester`即可,并实现`initContext``newJobRunner`方法即可。如`lts-example`中的例子:
342+
343+
```java
344+
public class TestJobRunnerTester extends JobRunnerTester {
345+
346+
public static void main(String[] args) throws Throwable {
347+
// 1. Mock Job 数据
348+
Job job = new Job();
349+
job.setTaskId("2313213");
350+
// 2. 运行测试
351+
TestJobRunnerTester tester = new TestJobRunnerTester();
352+
Result result = tester.run(job);
353+
System.out.println(JSONUtils.toJSONString(result));
354+
}
355+
356+
@Override
357+
protected void initContext() {
358+
// TODO 初始化Spring容器等
359+
}
360+
361+
@Override
362+
protected JobRunner newJobRunner() {
363+
return new TestJobRunner();
364+
}
365+
}
366+
```
367+
340368
##SPI扩展说明
341369
###LTS-Logger扩展
342370
1. 引入`lts-logger-api-{version}.jar`

lts-core/src/main/java/com/lts/core/logger/support/FailsafeLogger.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,105 +27,105 @@ private String appendContextMessage(String msg) {
2727
public void trace(String msg, Throwable e) {
2828
try {
2929
logger.trace(appendContextMessage(msg), e);
30-
} catch (Throwable t) {
30+
} catch (Throwable ignored) {
3131
}
3232
}
3333

3434
public void trace(Throwable e) {
3535
try {
3636
logger.trace(e);
37-
} catch (Throwable t) {
37+
} catch (Throwable ignored) {
3838
}
3939
}
4040

4141
public void trace(String msg) {
4242
try {
4343
logger.trace(appendContextMessage(msg));
44-
} catch (Throwable t) {
44+
} catch (Throwable ignored) {
4545
}
4646
}
4747

4848
public void debug(String msg, Throwable e) {
4949
try {
5050
logger.debug(appendContextMessage(msg), e);
51-
} catch (Throwable t) {
51+
} catch (Throwable ignored) {
5252
}
5353
}
5454

5555
public void debug(Throwable e) {
5656
try {
5757
logger.debug(e);
58-
} catch (Throwable t) {
58+
} catch (Throwable ignored) {
5959
}
6060
}
6161

6262
public void debug(String msg) {
6363
try {
6464
logger.debug(appendContextMessage(msg));
65-
} catch (Throwable t) {
65+
} catch (Throwable ignored) {
6666
}
6767
}
6868

6969
public void info(String msg, Throwable e) {
7070
try {
7171
logger.info(appendContextMessage(msg), e);
72-
} catch (Throwable t) {
72+
} catch (Throwable ignored) {
7373
}
7474
}
7575

7676
public void info(String msg) {
7777
try {
7878
logger.info(appendContextMessage(msg));
79-
} catch (Throwable t) {
79+
} catch (Throwable ignored) {
8080
}
8181
}
8282

8383
public void warn(String msg, Throwable e) {
8484
try {
8585
logger.warn(appendContextMessage(msg), e);
86-
} catch (Throwable t) {
86+
} catch (Throwable ignored) {
8787
}
8888
}
8989

9090
public void warn(String msg) {
9191
try {
9292
logger.warn(appendContextMessage(msg));
93-
} catch (Throwable t) {
93+
} catch (Throwable ignored) {
9494
}
9595
}
9696

9797
public void error(String msg, Throwable e) {
9898
try {
9999
logger.error(appendContextMessage(msg), e);
100-
} catch (Throwable t) {
100+
} catch (Throwable ignored) {
101101
}
102102
}
103103

104104
public void error(String msg) {
105105
try {
106106
logger.error(appendContextMessage(msg));
107-
} catch (Throwable t) {
107+
} catch (Throwable ignored) {
108108
}
109109
}
110110

111111
public void error(Throwable e) {
112112
try {
113113
logger.error(e);
114-
} catch (Throwable t) {
114+
} catch (Throwable ignored) {
115115
}
116116
}
117117

118118
public void info(Throwable e) {
119119
try {
120120
logger.info(e);
121-
} catch (Throwable t) {
121+
} catch (Throwable ignored) {
122122
}
123123
}
124124

125125
public void warn(Throwable e) {
126126
try {
127127
logger.warn(e);
128-
} catch (Throwable t) {
128+
} catch (Throwable ignored) {
129129
}
130130
}
131131

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.lts.example.support;
2+
3+
import com.lts.core.commons.utils.JSONUtils;
4+
import com.lts.core.domain.Job;
5+
import com.lts.tasktracker.Result;
6+
import com.lts.tasktracker.runner.JobRunner;
7+
import com.lts.tasktracker.runner.JobRunnerTester;
8+
9+
/**
10+
* @author Robert HG ([email protected]) on 9/13/15.
11+
*/
12+
public class TestJobRunnerTester extends JobRunnerTester {
13+
14+
public static void main(String[] args) throws Throwable {
15+
// Mock Job 数据
16+
Job job = new Job();
17+
job.setTaskId("2313213");
18+
// 运行测试
19+
TestJobRunnerTester tester = new TestJobRunnerTester();
20+
Result result = tester.run(job);
21+
System.out.println(JSONUtils.toJSONString(result));
22+
}
23+
24+
@Override
25+
protected void initContext() {
26+
// TODO 初始化Spring容器
27+
}
28+
29+
@Override
30+
protected JobRunner newJobRunner() {
31+
return new TestJobRunner();
32+
}
33+
}

lts-tasktracker/src/main/java/com/lts/tasktracker/logger/BizLoggerFactory.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ public class BizLoggerFactory {
1919
* 保证一个TaskTracker只能有一个Logger, 因为一个jvm可以有多个TaskTracker
2020
*/
2121
public static BizLogger getLogger(Level level, RemotingClientDelegate remotingClient, TaskTrackerApplication application) {
22+
23+
// 单元测试的时候返回 Mock
24+
if (Environment.UNIT_TEST == LTSConfig.getEnvironment()) {
25+
return new MockBizLogger(level);
26+
}
27+
2228
String key = application.getConfig().getIdentity();
2329
BizLogger logger = BIZ_LOGGER_CONCURRENT_HASH_MAP.get(key);
2430
if (logger == null) {
@@ -27,22 +33,12 @@ public static BizLogger getLogger(Level level, RemotingClientDelegate remotingCl
2733
if (logger != null) {
2834
return logger;
2935
}
30-
logger = create(level, remotingClient, application);
36+
logger = new BizLoggerImpl(level, remotingClient, application);
3137

3238
BIZ_LOGGER_CONCURRENT_HASH_MAP.put(key, logger);
3339
}
3440
}
3541
return logger;
3642
}
3743

38-
/**
39-
* 单元测试的时候返回 Mock
40-
*/
41-
private static BizLogger create(Level level, RemotingClientDelegate remotingClient, TaskTrackerApplication application) {
42-
if (Environment.UNIT_TEST == LTSConfig.getEnvironment()) {
43-
return new MockBizLogger(level);
44-
}
45-
return new BizLoggerImpl(level, remotingClient, application);
46-
}
47-
4844
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.lts.tasktracker.runner;
2+
3+
import com.lts.core.cluster.LTSConfig;
4+
import com.lts.core.constant.Environment;
5+
import com.lts.core.constant.Level;
6+
import com.lts.core.domain.Job;
7+
import com.lts.tasktracker.Result;
8+
import com.lts.tasktracker.logger.BizLoggerFactory;
9+
10+
/**
11+
* 为了方便JobRunner测试设计的
12+
*
13+
* @author Robert HG ([email protected]) on 9/13/15.
14+
*/
15+
public abstract class JobRunnerTester {
16+
17+
public Result run(Job job) throws Throwable {
18+
// 1. 设置LTS环境为 UNIT_TEST
19+
LTSConfig.setEnvironment(Environment.UNIT_TEST);
20+
// 设置 BizLogger
21+
LtsLoggerFactory.setLogger(BizLoggerFactory.getLogger(Level.INFO, null, null));
22+
// 2. load context (Spring Context 或者其他的)
23+
initContext();
24+
// 3. new jobRunner
25+
JobRunner jobRunner = newJobRunner();
26+
// 4. run job
27+
return jobRunner.run(job);
28+
}
29+
30+
/**
31+
* 初始化上下文 (Spring Context等),准备运行环境
32+
*/
33+
protected abstract void initContext();
34+
35+
/**
36+
* 创建JobRunner
37+
*/
38+
protected abstract JobRunner newJobRunner();
39+
40+
}

0 commit comments

Comments
 (0)