Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in tests #2030

Closed
gustiando opened this issue Dec 29, 2015 · 5 comments
Closed

Memory leak in tests #2030

gustiando opened this issue Dec 29, 2015 · 5 comments

Comments

@gustiando
Copy link

I'm getting the following error when running the entire suite of tests:

timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

After some investigation, I found out to be a memory leak issue. Looking at some heap profiling snapshot, objects still seem to be referenced and not getting garbaged collected.

Anyone know a solution that would prevent it from happening? There's some options such as going through each one of my 1000ish specs and adding afterEach to do some clean up, but that seems like a lot of work.

Here's a sample layout of how most of my tests look like

describe('MyClassCtrl', function() {

  var $httpBackend, $rootScope, ctrl;
  ctrl = $rootScope = $httpBackend = null;

  beforeEach(function() {
    module('myApp');
    inject(function($controller, $rootScope, _$httpBackend_, $stateParams) {
      var $scope;
      $stateParams.id = 1;
      $httpBackend = _$httpBackend_;
      $scope = $rootScope.$new();
      ctrl = $controller('MyClassCtrl', {
        $scope: $scope
      });
    });
  });

  describe('#_getMyList', function() {
    beforeEach(function() {
      $httpBackend.expectGET("/my/app/url").respond({
        my_list: [1, 2, 3]
      });
      ctrl._getMyList();
      $httpBackend.flush();
    });

    it('sets property document results', function() {
      expect(ctrl.my_list).to.eql([1, 2, 3]);
    });
  });
});

Below are some profiling screenshots:

enter image description here

enter image description here

enter image description here

Thanks!

@danielstjules
Copy link
Contributor

Is this an angular app? If so, you'll have a lot more luck asking in their IRC or related channels. Tracking down memory leaks specific to an application isn't really within the scope of mocha. I don't know how your inject function works, whether or not it plays with any globals that would make it tougher to clean up, etc. A few frameworks also have their own memory leaks: https://github.com/samccone/drool

Hopefully adding a few afterEach won't be too bad, since your 1000 specs are likely grouped in 100 or less suites? Also, random aside, but you don't need to return describe, it or expect.

Sorry we couldn't be of more help! And if this is angular, these might be of help:
http://stackoverflow.com/questions/22397484/how-to-reduce-remove-memory-leaks-in-angular-application
http://makandracards.com/makandra/31289-how-to-create-giant-memory-leaks-in-angularjs
http://www.dwmkerr.com/fixing-memory-leaks-in-angularjs-applications/

@gustiando
Copy link
Author

thanks @danielstjules I'll take a look! the returns were just because I transpiled the coffeescript code

@danieldram
Copy link

I had a similar issue and the compilation did add returns. When I removed the returns the tests ran without issue. Just wanted to post in case anyone in the future finds a similar issue.

@justmyway
Copy link

@danieldram check, i have got the similar issues

@danieldram
Copy link

I mean to post an update thanks @justmyway. It turns out that you do need the returns. When I pulled them out, my tests were passing even though they shouldn't have (I realized later). The main problem seems to be when I execute many HTTP requests or one right after another, in a promise chain.. Sometimes one would not fire, sometimes it would not, but still pass when the return isn't provided.

But always explicitly returning from chai-http in the it() at least gave me the errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants