When your metrics report that a Python component is experiencing random restarts and instability after a few days of uptime, what do you do? In our case, looking at memory usage showed that a component's memory footprint increased monotonically and continuously, indicating a memory leak. A powerful memory tracking tool in the Python standard library, tracemalloc, made it possible to diagnose and fix the leak quickly. In this post, we'll look at how we used it.
In Python, mocking is accomplished through the unittest.mock module. The module contains a number of useful classes and functions, the most important of which are the patch function (as decorator and context manager) and the MagicMock class. Mocking in Python is largely accomplished through the use of these two powerful components.