fix: 强化工作流与插件运行状态处理

- 显式传播工作流缓存故障并补充仓储测试

- 防止状态轮询重入和过期响应,规范执行记录无权限响应
This commit is contained in:
2026-07-27 19:40:40 +08:00
parent 567fd12706
commit 2892a7eddc
6 changed files with 351 additions and 58 deletions

View File

@@ -0,0 +1,185 @@
package tech.easyflow.ai.easyagentsflow.repository;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.CacheException;
import com.alicp.jetcache.CacheGetResult;
import com.alicp.jetcache.CacheResult;
import com.alicp.jetcache.CacheResultCode;
import com.alicp.jetcache.support.CacheEncodeException;
import com.easyagents.flow.core.chain.ChainState;
import org.junit.Assert;
import org.junit.Test;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.concurrent.TimeUnit;
/**
* {@link ChainStateRepositoryImpl} 缓存异常处理回归测试。
*/
public class ChainStateRepositoryImplTest {
/**
* 验证缓存解码失败时抛出异常且不创建空工作流状态。
*
* @throws Exception 缓存依赖注入失败时抛出
*/
@Test
public void loadShouldFailWithoutOverwritingStateWhenCacheDecodeFails() throws Exception {
String instanceId = "decode-failed-instance";
CacheGetResult<Object> failure = new CacheGetResult<>(new CacheEncodeException(
"decode error",
new ClassNotFoundException("com.alicp.jetcache.CacheValueHolder")
));
RecordingCache cache = new RecordingCache(failure, CacheResult.SUCCESS_WITHOUT_MSG);
ChainStateRepositoryImpl repository = repository(cache.asCache());
try {
repository.load(instanceId);
Assert.fail("cache decode failure should be propagated");
} catch (CacheException expected) {
Assert.assertTrue(expected.getMessage().contains("工作流状态缓存读取失败"));
Assert.assertTrue(expected.getMessage().contains(instanceId));
}
Assert.assertEquals(0, cache.getPutCount());
}
/**
* 验证缓存未命中时创建并持久化新的工作流状态。
*
* @throws Exception 缓存依赖注入失败时抛出
*/
@Test
public void loadShouldCreateStateWhenCacheDoesNotExist() throws Exception {
String instanceId = "new-instance";
RecordingCache cache = new RecordingCache(
new CacheGetResult<>(CacheResultCode.NOT_EXISTS, null, null),
CacheResult.SUCCESS_WITHOUT_MSG
);
ChainStateRepositoryImpl repository = repository(cache.asCache());
ChainState state = repository.load(instanceId);
Assert.assertEquals(instanceId, state.getInstanceId());
Assert.assertEquals(1, cache.getPutCount());
Assert.assertSame(state, cache.getLastPutValue());
}
/**
* 验证缓存写入失败时不会返回未持久化的工作流状态。
*
* @throws Exception 缓存依赖注入失败时抛出
*/
@Test
public void loadShouldFailWhenNewStateCannotBePersisted() throws Exception {
RecordingCache cache = new RecordingCache(
new CacheGetResult<>(CacheResultCode.NOT_EXISTS, null, null),
new CacheResult(new IllegalStateException("redis unavailable"))
);
ChainStateRepositoryImpl repository = repository(cache.asCache());
try {
repository.load("write-failed-instance");
Assert.fail("cache write failure should be propagated");
} catch (CacheException expected) {
Assert.assertTrue(expected.getMessage().contains("工作流状态缓存写入失败"));
}
Assert.assertEquals(1, cache.getPutCount());
}
/**
* 创建工作流状态仓储并注入缓存。
*
* @param cache 测试缓存
* @return 已完成依赖注入的工作流状态仓储
* @throws Exception 反射注入失败时抛出
*/
private ChainStateRepositoryImpl repository(Cache<String, Object> cache) throws Exception {
ChainStateRepositoryImpl repository = new ChainStateRepositoryImpl();
Field field = BaseRepository.class.getDeclaredField("cache");
field.setAccessible(true);
field.set(repository, cache);
return repository;
}
/**
* 仅实现当前仓储测试所需操作的 JetCache 调用记录器。
*/
private static final class RecordingCache implements InvocationHandler {
private final CacheGetResult<Object> getResult;
private final CacheResult putResult;
private int putCount;
private Object lastPutValue;
/**
* 创建缓存调用记录器。
*
* @param getResult 读取操作结果
* @param putResult 写入操作结果
*/
private RecordingCache(CacheGetResult<Object> getResult, CacheResult putResult) {
this.getResult = getResult;
this.putResult = putResult;
}
/**
* 创建实现 JetCache 接口的 JDK 动态代理。
*
* @return JetCache 测试代理
*/
@SuppressWarnings("unchecked")
private Cache<String, Object> asCache() {
return (Cache<String, Object>) Proxy.newProxyInstance(
Cache.class.getClassLoader(),
new Class<?>[]{Cache.class},
this
);
}
/**
* 处理仓储发起的缓存读写操作。
*
* @param proxy 代理对象
* @param method 被调用方法
* @param args 调用参数
* @return 预设的缓存操作结果
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) {
if ("GET".equals(method.getName())) {
return getResult;
}
if ("PUT".equals(method.getName()) && args != null && args.length == 4) {
putCount++;
lastPutValue = args[1];
Assert.assertEquals(3L, args[2]);
Assert.assertEquals(TimeUnit.DAYS, args[3]);
return putResult;
}
throw new UnsupportedOperationException("unsupported cache method: " + method.getName());
}
/**
* 获取写入调用次数。
*
* @return 写入调用次数
*/
private int getPutCount() {
return putCount;
}
/**
* 获取最后一次写入的缓存值。
*
* @return 最后一次写入的缓存值
*/
private Object getLastPutValue() {
return lastPutValue;
}
}
}