我需要一些想法来对这个任务方法进行单元测试

发布于 2024-12-11 14:41:33 字数 740 浏览 1 评论 0原文

这是有问题的方法:

public void StartBatchProcessing(IFileBatch fileBatch)
{
    var dataWarehouseFactsMerger = m_dataWarehouseFactsMergerFactory.Create(fileBatch);
    dataWarehouseFactsMerger.Merge();

    if(!m_isTaskStarted)
    {
        m_isTaskStarted = true;
        m_lastQueuedBatchProcessingTask = new TaskFactory().StartNew(() => ProcessBatch(dataWarehouseFactsMerger));
    }
    else
    {
        m_lastQueuedBatchProcessingTask = m_lastQueuedBatchProcessingTask.ContinueWith(previous => ProcessBatch(dataWarehouseFactsMerger));
    }
}

正如您所看到的,我正在使用 TPL 将任务一个接一个地排队,并且我想测试任务是否会在前一个任务完成后立即按照它们到达的顺序执行。

ProcessBatch 方法受到保护,因此我认为它可以在派生类中被覆盖,并用于设置某些标志或某些内容并断言。

所有想法都受到欢迎和赞赏。

This is the method in question:

public void StartBatchProcessing(IFileBatch fileBatch)
{
    var dataWarehouseFactsMerger = m_dataWarehouseFactsMergerFactory.Create(fileBatch);
    dataWarehouseFactsMerger.Merge();

    if(!m_isTaskStarted)
    {
        m_isTaskStarted = true;
        m_lastQueuedBatchProcessingTask = new TaskFactory().StartNew(() => ProcessBatch(dataWarehouseFactsMerger));
    }
    else
    {
        m_lastQueuedBatchProcessingTask = m_lastQueuedBatchProcessingTask.ContinueWith(previous => ProcessBatch(dataWarehouseFactsMerger));
    }
}

As you can see I'm using TPL to queue tasks one after the other and I would like to test that the tasks will execute in the order they arrive as soon as the previous one finishes.

The ProcessBatch method is protected so I think it could be overwritten in a derived class and be used to set some flag or something and assert that.

All ideas are welcome and appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁轨上的流浪者 2024-12-18 14:41:33

您可以创建一个 DataWarehouseFactsMergerFactory 的实现,它创建能够记录输入的 fileBatch 以及每个任务的开始时间的 DataWarehouseFactsMerger 的实现,但对于其余的,实际上什么也不做。

You could create an implementation of DataWarehouseFactsMergerFactory that creates implementations of DataWarehouseFactsMerger that are capable of logging which fileBatch was entered and the start time of each task, but for the rest don't really do anything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文