[TIP] How do you unit test threading classes?

Javier Domingo Cansino javier.domingo at fon.com
Fri Jun 6 01:18:03 PDT 2014


> Generally, I've found that if I start thinking about writing locks or am
> having a problem finding the right tools to communicate between threads
> or processes, there is a good chance I'm not writing very threadsafe
> code.
>
> Also, threads and processes are not that scary when it gets down to
> it. The most important thing to pay attention to is when you reuse your
> data. For example, a really simple rule of thumb is to try to pass
> native python types between threads and instantiate objects for every
> message. This is what I was doing in the `URLWorker` class by passing in
> a string of the URL and creating a separate `URLProcessor` object for
> each URL.

I have just a design that makes something like that, but the problem
comes when I want to test thread safety. Also, checking the flows is
not really easy.

In my case, I have a queue of jobs and a queue of results (really
using a dict for sync), so that the client adds a job, if no results
runs the job he has just added, gets a result, and triggers a sleeping
thread with a simple Event.

I have had to use locks for job queue, and for result queue. I don't
really know how to check that the function is threadsafe.

I would love if there was a Mock or something that executed a function
line by line, letting you do checks. There would be no easier way than
that to assert you are doing things correctly.

Javier Domingo Cansino
Research & Development Junior Engineer
Fon Labs Workgroup, Getxo - Spain.



More information about the testing-in-python mailing list