feat: 新增仅用于单元测试的日志输出.
This commit is contained in:
8
pom.xml
8
pom.xml
@@ -21,5 +21,13 @@
|
||||
<version>4.13.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- 日志输出 (仅用于测试) -->
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.2.13</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -6,9 +6,12 @@ import com.serliunx.statemanagement.machine.StateMachineBuilder;
|
||||
import com.serliunx.statemanagement.support.PrinterEvent;
|
||||
import com.serliunx.statemanagement.support.PrinterState;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 状态机测试
|
||||
@@ -19,6 +22,8 @@ import java.util.concurrent.Executors;
|
||||
*/
|
||||
public class MachineTest {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MachineTest.class);
|
||||
|
||||
private final ExecutorService executor = Executors.newFixedThreadPool(5);
|
||||
|
||||
@Test
|
||||
@@ -29,15 +34,15 @@ public class MachineTest {
|
||||
.withInitial(PrinterState.STOPPING)
|
||||
.build();
|
||||
|
||||
System.out.println(stateMachine.current());
|
||||
log.info("{}", stateMachine.current());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConcurrentStateMachine() throws Exception {
|
||||
ConcurrentStateMachine<PrinterState> stateMachine = StateMachineBuilder.from(PrinterState.values())
|
||||
.async()
|
||||
.async(false)
|
||||
.whenEntry(PrinterState.STOPPING, h -> {
|
||||
System.out.println(1111);
|
||||
log.info("enter stopping~");
|
||||
})
|
||||
.whenHappened(PrinterEvent.TURN_OFF, l -> {
|
||||
if (l.switchTo(PrinterState.STOPPING))
|
||||
@@ -47,8 +52,7 @@ public class MachineTest {
|
||||
.concurrent()
|
||||
.build();
|
||||
|
||||
// stateMachine.publish(PrinterEvent.TURN_OFF);
|
||||
System.out.println(stateMachine.current());
|
||||
log.info("{}", stateMachine.current());
|
||||
stateMachine.close();
|
||||
}
|
||||
|
||||
@@ -58,15 +62,16 @@ public class MachineTest {
|
||||
.async(false)
|
||||
.concurrent()
|
||||
.whenEntry(PrinterState.STOPPING, h -> {
|
||||
System.out.println("entering stopping...");
|
||||
log.info("stopping...");
|
||||
})
|
||||
.build();
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
executor.execute(() -> {
|
||||
System.out.println(stateMachine.compareAndSet(PrinterState.IDLE, PrinterState.STOPPING, true));
|
||||
log.info("{}", stateMachine.compareAndSet(PrinterState.IDLE, PrinterState.STOPPING, true));
|
||||
});
|
||||
}
|
||||
|
||||
TimeUnit.SECONDS.sleep(5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import com.serliunx.statemanagement.manager.DefaultUnidirectionalStateManager;
|
||||
import com.serliunx.statemanagement.manager.UnidirectionalStateManager;
|
||||
import com.serliunx.statemanagement.support.PrinterState;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 状态管理器测试
|
||||
@@ -15,6 +17,8 @@ import org.junit.Test;
|
||||
*/
|
||||
public class ManagerTest {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ManagerTest.class);
|
||||
|
||||
@Test
|
||||
public void testUnidirectionalStateManager() {
|
||||
UnidirectionalStateManager<PrinterState> unidirectionalStateManager =
|
||||
|
||||
@@ -5,6 +5,8 @@ import com.serliunx.statemanagement.machine.StateMachine;
|
||||
import com.serliunx.statemanagement.machine.support.StateMachines;
|
||||
import com.serliunx.statemanagement.support.PrinterState;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 状态机工具类测试
|
||||
@@ -14,21 +16,23 @@ import org.junit.Test;
|
||||
*/
|
||||
public class StateMachinesTest {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(StateMachinesTest.class);
|
||||
|
||||
@Test
|
||||
public void testConcurrentStateMachines() throws Exception {
|
||||
ConcurrentStateMachine<PrinterState> machine = StateMachines.concurrentStateMachine(PrinterState.values());
|
||||
System.out.println(machine.current());
|
||||
System.out.println(machine.switchPrevAndGet());
|
||||
System.out.println(machine.current());
|
||||
log.info("{}", machine.current());
|
||||
log.info("{}", machine.switchPrevAndGet());
|
||||
log.info("{}", machine.current());
|
||||
machine.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStateMachines() throws Exception {
|
||||
StateMachine<PrinterState> machine = StateMachines.defaultStateMachine(PrinterState.values());
|
||||
System.out.println(machine.current());
|
||||
System.out.println(machine.switchNextAndGet());
|
||||
System.out.println(machine.current());
|
||||
log.info("{}", machine.current());
|
||||
log.info("{}", machine.switchPrevAndGet());
|
||||
log.info("{}", machine.current());
|
||||
machine.close();
|
||||
}
|
||||
}
|
||||
|
||||
15
src/test/resources/logback.xml
Normal file
15
src/test/resources/logback.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>
|
||||
%boldGreen(%d{yyyy-MM-dd HH:mm:ss(SSS)}) %magenta([%25.25thread]) %highlight([%-6level]) %boldCyan(%-36logger{32}): %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="com.serliunx" level="DEBUG"/>
|
||||
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user