We've got a large and complex function which needs to be deterministic. It is one of the workhorses at our company and covers a large amount of code. This code often becomes non-deterministic due to python's dict iterator. This has happened many times, and it is very difficult to track down, and often not noticed immediately. We would like to write an automated test to detect non-determinism, but I'm not sure how to do it.
We have tried running the function in a loop, and testing the results are always the same, but sometimes, even though the function is non-deterministic, the function will pass this test due to the arbitrary but somewhat consistent ordering of the dict iterator.
Is there a way to write an automated test to catch this kind of bug?
Perhaps there is a way to hack python's dict so that the iterators are random rather than arbitrary during this test? That way repeated calls to function would be more likely to diverge? This seems like a fairly involved method, but I can't think of any other way.
EDIT:
We are currently using Python 2.7.
We have unit tests of the various sub-modules, however they often don't expose the non-determinism due the arbitrary, but consistent nature of the dict order.
Also, perhaps non-deterministic is not the right way to describe this problem. This function takes {id : data}, however the value of the ids should not affect the result of the code, however due to python dict ordering, it sometimes does. Perhaps the best way to test this would be to replace the ids with random values and check to see if the output is the same after many runs with different ids.