尝试围绕执行map_async()
操作的函数编写一些单元测试。更具体地说,我想确认如果其中一个进程发生异常,某些文件会被清理。示例伪代码,其意图如下。
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.
但是,当我去执行测试时,我得到以下 PicklingError: PicklingError: Can't pickle <class 'mock.MagicMock'>: it's not the same object as mock.MagicMock
。
任何想法如何执行用我自己的模拟函数替换映射函数的预期结果?