change: 丰富测试用例; 并发性状态机CAS默认触发状态处理器.
This commit is contained in:
@@ -13,7 +13,7 @@ public interface ConcurrentStateMachine<S> extends StateMachine<S> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 尝试使用CAS更新状态
|
* 尝试使用CAS更新状态
|
||||||
* <li> 无论是否成功更新都不触发状态处理器
|
* <li> 成功更新时触发状态处理器
|
||||||
*
|
*
|
||||||
* @param expectedValue 前置状态
|
* @param expectedValue 前置状态
|
||||||
* @param newValue 更新的状态值
|
* @param newValue 更新的状态值
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ public class DefaultConcurrentStateMachine<S> extends AbstractStateMachine<S> im
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean compareAndSet(S expectedValue, S newValue) {
|
public boolean compareAndSet(S expectedValue, S newValue) {
|
||||||
return compareAndSet(expectedValue, newValue, false);
|
return compareAndSet(expectedValue, newValue, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ import com.serliunx.statemanagement.support.PrinterEvent;
|
|||||||
import com.serliunx.statemanagement.support.PrinterState;
|
import com.serliunx.statemanagement.support.PrinterState;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 状态机测试
|
* 状态机测试
|
||||||
*
|
*
|
||||||
@@ -16,6 +19,8 @@ import org.junit.Test;
|
|||||||
*/
|
*/
|
||||||
public class MachineTest {
|
public class MachineTest {
|
||||||
|
|
||||||
|
private final ExecutorService executor = Executors.newFixedThreadPool(5);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testStandardStateMachine() throws Exception {
|
public void testStandardStateMachine() throws Exception {
|
||||||
StateMachine<PrinterState> stateMachine = StateMachineBuilder.from(PrinterState.values())
|
StateMachine<PrinterState> stateMachine = StateMachineBuilder.from(PrinterState.values())
|
||||||
@@ -46,4 +51,22 @@ public class MachineTest {
|
|||||||
System.out.println(stateMachine.current());
|
System.out.println(stateMachine.current());
|
||||||
stateMachine.close();
|
stateMachine.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConcurrentStateMachine2() throws Exception {
|
||||||
|
ConcurrentStateMachine<PrinterState> stateMachine = StateMachineBuilder.from(PrinterState.values())
|
||||||
|
.async(false)
|
||||||
|
.concurrent()
|
||||||
|
.whenEntry(PrinterState.STOPPING, h -> {
|
||||||
|
System.out.println("entering stopping...");
|
||||||
|
})
|
||||||
|
.build();
|
||||||
|
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
executor.execute(() -> {
|
||||||
|
System.out.println(stateMachine.compareAndSet(PrinterState.IDLE, PrinterState.STOPPING, true));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user