When the magic fades
Earlier in the book, I challenged you to examine each method of DI presented in the book with a critical eye. With that in mind, we should consider the potential costs of monkey patching.
Data races—We saw in our examples that monkey patching is the process of replacing a global variable with a copy that performs in the way we need it to for a particular test. And that is perhaps the biggest problem. Swapping something global, and therefore shared, for something specific causes a data race on that variable.
To understand this data race a little more, we need to understand how Go runs tests. By default, tests within a package are executed sequentially. We can reduce our test execution time by marking our tests with t.Parallel() ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access