简体   繁体   English

如何单元测试ExecutorService为任务生成新线程?

[英]How to unit test that ExecutorService spawns new thread for task?

How does one unit test that a new thread was spawned for a Runnable task when using an ExecutorService? 在使用ExecutorService时,一个单元如何测试为Runnable任务生成新线程?

Basically, I have a static thread pool for my application. 基本上,我的应用程序有一个静态线程池。

public static final ExecutorService executorService = Executors.newCachedThreadPool();

I'd like to use this thread pool for the my unit tests, rather than mocking one out or injecting a new thread pool, since different thread pools can dramatically alter the behavior of my application (fixed vs. cached vs. scheduled, etc); 我想将此线程池用于我的单元测试,而不是模拟一个或注入新的线程池,因为不同的线程池可以显着改变我的应用程序的行为(固定与缓存与预定等) ; I want to ensure that I test the application's behavior with its run-time thread pool. 我想确保使用其运行时线程池测试应用程序的行为。

Cached thread pool seems to work best for me. 缓存线程池似乎最适合我。 The problem is that since it's static and the threads are cached for 60 seconds, only the first test will actually spawn a new thread in the pool, while subsequent tests reuse that thread. 问题是因为它是静态的并且线程被缓存了60秒,所以只有第一个测试实际上会在池中产生一个新线程,而后续测试会重用该线程。

Unit test code: 单元测试代码:

public void testDoCallExecutesTaskInAnotherThread() {    
    final Client client = this.createClient();
    final ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) client.getExecutorService(); // gets the static thread pool
    final int initPoolSize = threadPoolExecutor.getPoolSize();
    final Response response = client.doCall();
    Assert.assertEquals(initPoolSize + 1, threadPoolExecutor.getPoolSize());
}

Advice on how to get this working, or another approach altogether would be appreciated. 关于如何完成这项工作或其他方法的建议将不胜感激。

Mock the ThreadFactory : 模拟ThreadFactory

  ThreadFactory mock = new CustomObservableThreadFactory();
  ExecutorService executorService = Executors.newCachedThreadPool(mock);
  1. Inject the ExecutorService into the class under test as usual 像往常一样将ExecutorService注入到被测试的类中
  2. But use a custom ThreadFactory for creating the cached ThreadPool: The ThreadFactory will be called whenever a new Thread is to be called. 但是使用自定义ThreadFactory来创建缓存的ThreadPool:只要调用新的Thread,就会调用ThreadFactory。 You can then trace these instantiations as you like, egwith a listener or counter. 然后,您可以根据需要跟踪这些实例化,例如使用监听器或计数器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有一个 ExecutorService 为每个任务创建一个新线程? - Is there an ExecutorService that creates a new thread for every task? ExecutorService 为每个提交的任务指定使用新线程 - ExecutorService specify use new thread for each submitted task 活动产生一个线程,但是在子线程完成之前,该活动被销毁或创建了新的活动(在同一任务中) - Activity spawns a thread, but then the activity gets destroyed or an new activity (in the same task) gets created before child thread finishes 服务器如何在Spring框架中为单例对象生成新线程 - How server spawns new thread for a singleton object in spring framework 如何通知调用者线程ExecutorService已完成任务 - How to signal the caller thread that ExecutorService has finished a task mocking 单元测试中的Executorservice字段 - mocking Executorservice field in unit test 如何使用ExecutorService单元测试对测试用例进行排序 - How to sequence your test cases using ExecutorService unit testing 如何编写创建新线程的单元测试 - How to write unit test which creates new thread 如何使用 ExecutorService Java 在新线程上运行每个客户端? - How to run each client on new thread with ExecutorService Java? 如何等待一个产生它自己的线程的线程? - How to wait for a thread that spawns it's own thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM