| | |
| | | import java.util.concurrent.BlockingQueue; |
| | | import java.util.concurrent.CopyOnWriteArrayList; |
| | | import java.util.concurrent.ExecutorService; |
| | | import java.util.concurrent.Executors; |
| | | import java.util.concurrent.LinkedBlockingQueue; |
| | | import java.util.concurrent.ThreadFactory; |
| | | import java.util.concurrent.ThreadPoolExecutor; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import java.util.function.Function; |
| | | |
| | | /** |
| | |
| | | |
| | | private final ServerSocket serverSocket; |
| | | private final int port; |
| | | private final ExecutorService executor = Executors.newSingleThreadExecutor(); |
| | | /** 连接服务线程显式创建并命名。该池只提交一个常驻服务任务,队列容量 1 只是声明不再接受更多任务。 */ |
| | | private final ExecutorService executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, |
| | | new LinkedBlockingQueue<>(1), simulatorThreadFactory()); |
| | | private final BlockingQueue<ScriptedAction> actions = new LinkedBlockingQueue<>(); |
| | | private final List<String> requests = new CopyOnWriteArrayList<>(); |
| | | private final Function<String, String> responseProvider; |
| | |
| | | executor.awaitTermination(2, TimeUnit.SECONDS); |
| | | } |
| | | |
| | | /** 模拟器连接服务线程统一命名并设为守护线程,避免测试退出时残留。 */ |
| | | private static ThreadFactory simulatorThreadFactory() |
| | | { |
| | | AtomicInteger sequence = new AtomicInteger(); |
| | | return runnable -> { |
| | | Thread thread = new Thread(runnable, "bts-simulator-pool-" + sequence.incrementAndGet()); |
| | | thread.setDaemon(true); |
| | | return thread; |
| | | }; |
| | | } |
| | | |
| | | private record ScriptedAction(Duration delay, byte[] payload, boolean disconnect) |
| | | { |
| | | } |