Refactored code for running producers and consumers in tests.