增加“后面请求依赖于前面请求的执行结果”的测试案例

This commit is contained in:
wuweifeng10 2019-12-26 15:37:28 +08:00
parent b706edc231
commit f0ea3f1e1c
9 changed files with 213 additions and 15 deletions

View File

@ -1,6 +1,5 @@
package com.tianyalei.async.group;
import com.tianyalei.async.callback.DefaultCallback;
import com.tianyalei.async.callback.ICallback;
import com.tianyalei.async.callback.IWorker;
@ -56,7 +55,7 @@ public class WorkerWrapper<T, V> {
/**
* 也是个钩子变量用来存临时的结果
*/
private volatile WorkResult<V> workResult;
private volatile WorkResult<V> workResult = WorkResult.defaultResult();
private static final int FINISH = 1;
private static final int ERROR = 2;
@ -205,7 +204,7 @@ public class WorkerWrapper<T, V> {
WorkerWrapper workerWrapper = dependWrapper.getDependWrapper();
WorkResult tempWorkResult = workerWrapper.getWorkResult();
//为null或者isWorking说明它依赖的某个任务还没执行到或没执行完
if (tempWorkResult == null || workerWrapper.getState() == WORKING) {
if (workerWrapper.getState() == INIT || workerWrapper.getState() == WORKING) {
existNoFinish = true;
break;
}
@ -276,7 +275,8 @@ public class WorkerWrapper<T, V> {
return false;
}
if (workResult == null) {
//尚未处理过结果
if (checkIsNullResult()) {
if (e == null) {
workResult = defaultResult();
} else {
@ -293,7 +293,7 @@ public class WorkerWrapper<T, V> {
*/
private WorkResult<V> workerDoJob() {
//避免重复执行
if (workResult != null) {
if (!checkIsNullResult()) {
return workResult;
}
try {
@ -307,20 +307,20 @@ public class WorkerWrapper<T, V> {
//执行耗时操作
V resultValue = worker.action(getParam());
WorkResult<V> tempResult = new WorkResult<>(resultValue, ResultState.SUCCESS);
//如果状态不是在working,说明别的地方已经修改了
if (!compareAndSetState(WORKING, FINISH)) {
return workResult;
}
workResult.setResultState(ResultState.SUCCESS);
workResult.setResult(resultValue);
//回调成功
callback.result(true, getParam(), tempResult);
workResult = tempResult;
callback.result(true, getParam(), workResult);
return workResult;
} catch (Exception e) {
//避免重复回调
if (workResult != null) {
if (!checkIsNullResult()) {
return workResult;
}
fastFail(WORKING, e);
@ -328,6 +328,10 @@ public class WorkerWrapper<T, V> {
}
}
private boolean checkIsNullResult() {
return ResultState.DEFAULT == workResult.getResultState();
}
public WorkerWrapper addNext(WorkerWrapper<?, ?>... nextWrappers) {
if (nextWrappers == null) {
@ -385,11 +389,16 @@ public class WorkerWrapper<T, V> {
}
private WorkResult<V> defaultResult() {
return new WorkResult<>(getWorker().defaultValue(), ResultState.TIMEOUT);
workResult.setResultState(ResultState.TIMEOUT);
workResult.setResult(getWorker().defaultValue());
return workResult;
}
private WorkResult<V> defaultExResult(Exception ex) {
return new WorkResult<>(getWorker().defaultValue(), ResultState.EXCEPTION, ex);
workResult.setResultState(ResultState.EXCEPTION);
workResult.setResult(getWorker().defaultValue());
workResult.setEx(ex);
return workResult;
}
private WorkResult<V> getNoneNullWorkResult() {

View File

@ -7,5 +7,6 @@ package com.tianyalei.async.worker;
public enum ResultState {
SUCCESS,
TIMEOUT,
EXCEPTION
EXCEPTION,
DEFAULT //默认状态
}

View File

@ -25,7 +25,7 @@ public class WorkResult<V> {
}
public static WorkResult defaultResult() {
return new WorkResult<>(null, ResultState.TIMEOUT);
return new WorkResult<>(null, ResultState.DEFAULT);
}
@Override

View File

@ -0,0 +1,40 @@
package com.tianyalei.test.depend;
import com.tianyalei.async.callback.ICallback;
import com.tianyalei.async.callback.IWorker;
import com.tianyalei.async.executor.timer.SystemClock;
import com.tianyalei.async.worker.WorkResult;
/**
* @author wuweifeng wrote on 2019-11-20.
*/
public class ParWorker implements IWorker<String, User>, ICallback<String, User> {
@Override
public User action(String object) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new User("user0");
}
@Override
public User defaultValue() {
return new User("default User");
}
@Override
public void begin() {
//System.out.println(Thread.currentThread().getName() + "- start --" + System.currentTimeMillis());
}
@Override
public void result(boolean success, String param, WorkResult<User> workResult) {
System.out.println("worker0 的结果是:" + workResult.getResult());
}
}

View File

@ -0,0 +1,40 @@
package com.tianyalei.test.depend;
import com.tianyalei.async.callback.ICallback;
import com.tianyalei.async.callback.IWorker;
import com.tianyalei.async.worker.WorkResult;
/**
* @author wuweifeng wrote on 2019-11-20.
*/
public class ParWorker1 implements IWorker<WorkResult<User>, User>, ICallback<WorkResult<User>, User> {
@Override
public User action(WorkResult<User> result) {
System.out.println("par1的入参来自于par0 " + result.getResult());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new User("user1");
}
@Override
public User defaultValue() {
return new User("default User");
}
@Override
public void begin() {
//System.out.println(Thread.currentThread().getName() + "- start --" + System.currentTimeMillis());
}
@Override
public void result(boolean success, WorkResult<User> param, WorkResult<User> workResult) {
System.out.println("worker1 的结果是:" + workResult.getResult());
}
}

View File

@ -0,0 +1,40 @@
package com.tianyalei.test.depend;
import com.tianyalei.async.callback.ICallback;
import com.tianyalei.async.callback.IWorker;
import com.tianyalei.async.worker.WorkResult;
/**
* @author wuweifeng wrote on 2019-11-20.
*/
public class ParWorker2 implements IWorker<WorkResult<User>, String>, ICallback<WorkResult<User>, String> {
@Override
public String action(WorkResult<User> result) {
System.out.println("par2的入参来自于par1 " + result.getResult());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return result.getResult().getName();
}
@Override
public String defaultValue() {
return "default";
}
@Override
public void begin() {
//System.out.println(Thread.currentThread().getName() + "- start --" + System.currentTimeMillis());
}
@Override
public void result(boolean success, WorkResult<User> param, WorkResult<String> workResult) {
System.out.println("worker2 的结果是:" + workResult.getResult());
}
}

View File

@ -0,0 +1,39 @@
package com.tianyalei.test.depend;
import com.tianyalei.async.executor.Async;
import com.tianyalei.async.group.WorkerWrapper;
import com.tianyalei.async.worker.WorkResult;
import java.util.concurrent.ExecutionException;
/**
* 后面请求依赖于前面请求的执行结果
* @author wuweifeng wrote on 2019-12-26
* @version 1.0
*/
public class Test {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ParWorker w = new ParWorker();
ParWorker1 w1 = new ParWorker1();
ParWorker2 w2 = new ParWorker2();
WorkerWrapper<String, User> workerWrapper = new WorkerWrapper<>(w, "0", w);
//虽然尚未执行但是也可以先取得结果的引用作为下一个任务的入参
WorkResult<User> result = workerWrapper.getWorkResult();
WorkerWrapper<WorkResult<User>, User> workerWrapper1 = new WorkerWrapper<>(w1, result, w1);
WorkResult<User> result1 = workerWrapper1.getWorkResult();
WorkerWrapper<WorkResult<User>, String> workerWrapper2 = new WorkerWrapper<>(w2, result1, w2);
workerWrapper.addNext(workerWrapper1);
workerWrapper1.addNext(workerWrapper2);
Async.beginWork(3500, workerWrapper);
System.out.println(workerWrapper2.getWorkResult());
Async.shutDown();
}
}

View File

@ -0,0 +1,29 @@
package com.tianyalei.test.depend;
/**
* 一个包装类
* @author wuweifeng wrote on 2019-12-26
* @version 1.0
*/
public class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}

View File

@ -10,7 +10,7 @@ import java.util.concurrent.ExecutionException;
import static com.tianyalei.async.executor.Async.getThreadCount;
/**
* 行测试
* 行测试
*
* @author wuweifeng wrote on 2019-11-20.
*/