Ember Timer Leaks: The Bad Apples in Your Test Infrastructure

Evan Farina
7 min readbeginner
--
View Original

Overview

The article discusses the challenges of managing asynchronous timers in Ember applications, particularly focusing on timer leaks that can lead to test instability. It provides insights into identifying and fixing these leaks to enhance the reliability of automated tests in single-page applications (SPAs).

What You'll Learn

1

How to identify timer leaks in your Ember application tests

2

Why managing asynchronous timers is crucial for SPA stability

3

How to use Ember.run.cancel to clean up timers in the willDestroy hook

4

When to implement ember-lifeline for managing async timers

Prerequisites & Requirements

  • Understanding of asynchronous programming concepts
  • Familiarity with Ember.js testing framework

Key Questions Answered

What are timer leaks in Ember applications?
Timer leaks occur when asynchronous timers are set up during the application's lifecycle but are not properly cleaned up when the application is destroyed. This can lead to test timeouts and instability in single-page applications, particularly when using the wait helper in tests.
How can I identify leaking timers in my Ember tests?
You can identify leaking timers by using the hasScheduledTimers method from the Ember.run namespace in conjunction with QUnit's testDone method. This combination allows you to confirm the presence of async leaks and trace their sources in your test suite.
What is the purpose of the wait helper in Ember testing?
The wait helper, now renamed to settled, is a utility function that pauses the test runner until all asynchronous operations in the application are completed. It ensures that tests only proceed once all async code has finished executing, which is critical for reliable test outcomes.
When should I use ember-lifeline in my Ember applications?
You should consider using ember-lifeline when your application has many asynchronous timers to manage. This addon simplifies the cleanup process by automatically canceling timers in the willDestroy hook, reducing the risk of leaks and improving test stability.

Technologies & Tools

Frontend
Ember.js
Used as the framework for building single-page applications at LinkedIn.
Testing
Qunit
Utilized for testing Ember applications and identifying async leaks.
Addon
Ember-lifeline
Helps manage asynchronous timers and prevents leaks in Ember applications.

Key Actionable Insights

1
Implement the hasScheduledTimers method to check for async leaks in your tests.
This method allows you to verify if any timers are still running at the end of your tests, helping you identify potential issues early in the development process.
2
Utilize the ember-lifeline addon to manage async timers more effectively.
By using ember-lifeline, you can automate the cleanup of timers, which minimizes the risk of leaks and enhances the reliability of your test suite.
3
Always clean up timers in the willDestroy hook to prevent leaks.
This practice ensures that any timers set during a component's lifecycle are properly canceled when the component is destroyed, maintaining application stability.

Common Pitfalls

1
Failing to clean up asynchronous timers can lead to test timeouts and instability.
This happens because timers may continue to run even after the components that created them have been destroyed, causing unexpected behavior in tests.

Related Concepts

Asynchronous Programming
Single-page Applications
Ember.js Testing Practices