unit test write_one() thread safety

I have moved the write_one() code and removed some dead code so that it
can be conveniently unit tested, but otherwise I have not refactored (
yet! )

This is to help the discussion of ticket #40