Attempting to write some unittests around a function which performs a map_async()
operation. More specifically, I want to confirm that some files get cleaned up in the event of an Exception occurring in one of the processes. Sample pseudo-code with intentions provided below.
foo.py
def write_chunk(chunk):
... create file from chunk
return created_filename
class Foo:
def write_parallel(chunks):
filenames = set()
try:
pool = Pool(processes=2)
pool.map_async(write_chunk, chunks, callback=filenames.add)
except Exception:
//handle exception
finally:
cleanup_files(filenames)
test_foo.py
@patch("foo.write_chunk")
def test_write_parallel_exception_cleanup(self, mock_write_chunk):
def mock_side_effect(chunk):
if "chunk_1" == chunk:
raise Exception
else:
return chunk
mock_write_chunk.side_effect = mock_side_effect
foo = Foo()
foo.write_parallel({"chunk_1", "chunk_2"})
//assert "chunk_2" cleaned up and exception is thrown.
However, when I go to perform the test, I get the following PicklingError: PicklingError: Can't pickle <class 'mock.MagicMock'>: it's not the same object as mock.MagicMock
.
Any ideas how to perform the desired result of replacing the mapped function with my own mock function?