The Daily WTF
Follow
CodeSOD: Wait Longer
Karen encountered flakiness in her specification tests, occurring a few times per thousand runs. These complex, timing-sensitive tests failed occasionally despite generous timing windows and passing unit tests. The issue stemmed from JavaScript's setTimeout, which, unlike its documentation suggesting it may wait longer, can actually invoke callbacks before the specified time elapses in Node.js. Karen's simple wait function initially used setTimeout to introduce delays. After extensive debugging, she discovered this timing inconsistency was the root cause of her flaky tests. To resolve this, she rewrote the wait function to continuously check the elapsed time every millisecond. This new function ensures the operation completes only after the target duration has been met, preventing premature timeouts. While effective, this solution is considered inefficient and highlights the problematic time sensitivity of functional tests. It also points to a deficiency in the Node.js runtime's scheduling guarantees. The author expresses dismay that such a workaround is necessary.