Problem
Some things that affect the software are not under the control of your code or any of the libraries you are using.
Examples
Context switching between threads
Network errors
Solution
Minimize the use of code that relies on these inputs.
If possible, put something between you and the external system and inject faults to test the SUT.
From
Udacity's
Software Testing
course Unit I.