使用 QThreadPool 并行执行单元测试任务 运行

Unit test tasks run in parallel using QThreadPool

我在我的应用程序中并行使用 QThreadPool 到 运行 任务。任务将线程池作为参数,因此它们可以启动新任务。我如何为任务编写单元测试并断言正确的下一个任务已启动?

class MyTask: public QRunnable {
public:
    virtual void run() {
        m_threadPool.start(/*another task*/);
        m_threadPool.start(/*a third task*/);
    }
private:
    QThreadPool &m_threadPool;
};

我想测试MyTask:

QThreadPool threadPool;
threadPool.start(new MyTask(threadPool));
threadPool.waitForDone();

// Assert that another task and a third task is started.

我尝试扩展 QThreadPool 并记录启动的任务:

class MyThreadPool : public QThreadPool {
public:
    virtual void start(QRunnable *runnable, int priority = 0) {
        m_Queue.enqueue(runnable);
        // The task is not started, so I can manually start each task and test the outcome.
    }
    QQueue<QRunnable *> queue() const { return m_queue; }
private:
    QQueue<QRunnable *> m_queue;
};


MyThreadPool threadPool;
threadPool.start(new MyTask(threadPool));
threadPool.waitForDone();

QQueue<QRunnable *> Queue({ /*another task and a third task*/ });
Assert::IsEquavalent(threadPool.queue(), Queue);

但这不起作用,因为 QThreadPool::start() 不是虚拟的。编写测试的最佳方法是什么?

关于QThreadPool::start()不是虚函数的问题,可以这样做:

您可以在 MyTask 中使用您的子类并使用将调用 运行 的不同函数,而不是覆盖函数。像这样:

class MyThreadPool : public QThreadPool {
public:
    void enqueue_and_run(QRunnable *runnable, int priority = 0) {
        m_Queue.enqueue(runnable);
        QThreadPool::start(runnable, priority);
    }

    const QQueue<QRunnable *>& queue() const { 
         return m_queue; 
    }
private:
    QQueue<QRunnable *> m_queue;
};

class MyTask: public QRunnable {
public:
    virtual void run() {
        m_threadPool.enqueue_and_run(/*another task*/);
        m_threadPool.enqueue_and_run(/*a third task*/);
    }
private:
    MyThreadPool &m_threadPool;
};

然后运行同样的测试代码:

MyThreadPool threadPool;
threadPool.enqueue_and_run(new MyTask(threadPool));
threadPool.waitForDone();

QQueue<QRunnable *> Queue({ /*another task and a third task*/ });
Assert::IsEquavalent(threadPool.queue(), Queue);

这不是最优雅的方式,但可以明确您的意图。

作为替代方案,如果您想保留通用接口,可以使用基本函数重载:

template <class TPool>
void start(TPool* pool, QRunnable *runnable, int priority = 0) {
    pool->start(runnable, priority);
}

void start(MyThreadPool* pool, QRunnable *runnable, int priority = 0) {
    pool->enqueue_and_run(pool, runnable, priority);
}  

那么您的测试代码将与原始代码非常相似:

MyThreadPool threadPool;
start(threadPool, new MyTask(threadPool));
threadPool.waitForDone();
// ... rest of the code