单元测试并发代码 [英] Unit Testing Concurrent Code

查看:100
本文介绍了单元测试并发代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的周末项目包括编写跨平台的并发原语库(关键部分,读/写互斥,互锁整数,事件等),并想知道如何单元测试这个东西。我意识到这本身测试并发代码是很难的,但在测试的原语说的代码不能那么难,可能吗?

My weekend project consists of writing a cross-platform concurrency primitives library (critical sections, read/write mutexes, interlocked integers, events, etc) and was wondering how to unit test this stuff. I realize that testing concurrent code is hard in itself, but testing the primitives of said code couldn't be that hard, could it?

原来,它是很难。至少,对我来说是。

Turns out, it is that hard. At least, for me it is.

所以,你会如何去处理这个?只是作为一个例子,我甚至不知道从哪里开始测试关键的部分。

So how would you go about approaching this? Just as an example, I don't even know where to start with testing critical sections.

推荐答案

不要去想单位测试,想想要指定的行为。例如:

Don't think about unit tests, think about the behaviour you want to specify. For example:

Given_an_unlocked_lock
    It_should_be_possible_to_take_it
Given_a_locked_locked
    It_should_not_be_possible_to_take_it_from_another_thread
    It_should_be_possible_take_it_from_the_same_thread
Given_a_locked_locked_when_unlocked
    It_should_be_possible_to_take_it
Given_a_locked_locked_when_owning_thread_terminates
    It_should_be_possible_to_take_it

$ b Given_a_locked_locked_when_owning_thread_terminates
It_should_be_possible_to_take_it


我认为,将帮助你识别做什么。是的,你可能需要在单元测试中一个辅助线程来做到这一点。也许这个例子是有帮助的。

I think that will help you identify what to do. And yes probably you need a helper thread in your unit tests to make it happen. Maybe this example is helpful.

这篇关于单元测试并发代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆