2

Could someone shed some light on how multi threaded functionality could be developed in a pure TDD fashion. (possibly with some examples in C# or Java)

Thanks in Advance.

4

1 回答 1

2

In short,

  • First assert that the code that runs on a thread works. Functionality wise. Simple unit test - no threads. You can extract a type (sample name Executor) that abstracts away the threading aspect. e.g. Execute(codeblock) that spawns a new thread in production on which the codeblock is executed. In tests, codeblock is executed on the calling thread - like a function call.
  • Next write a stress test that uses the production Executor. Spawn multiple threads and assert on your invariants. i.e. things that must/must not happen irrespective of how many threads are spawned.

There are some other nuances to this - based on whether you are spawning multiple threads (active) or whether you are being called on different threads (passive). I'd recommend you read up the chapter related to threading in the GOOS book by SteveFreeman and NatPryce

于 2012-06-13T05:23:56.433 回答