Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

$interval does not get flushed #11912

Closed
vytautas-pranskunas- opened this issue May 20, 2015 · 11 comments
Closed

$interval does not get flushed #11912

vytautas-pranskunas- opened this issue May 20, 2015 · 11 comments

Comments

@vytautas-pranskunas-
Copy link

Hello,

because of protractor some issues i switched from $timeout to $interval but there i have an issue - $interval does not get flushed.

I modified plunker code which was originally created for other purposes but there i cannot get interval flushed too. Can you look at it please?

Just go to: http://plnkr.co/edit/tBth2b?p=preview
to module.js and replace

module.controller('ViewCtrl',
[         '$scope','$routeParams','TestService',
  function($scope,  $routeParams,  TestService) {

    $scope.increaseValue = function(){
      TestService.increaseValue();
    };

  }
]);

to

module.controller('ViewCtrl',
[         '$scope','$routeParams','TestService', '$interval',
  function($scope,  $routeParams,  TestService, $interval) {

    $scope.increaseValue = function(){

       var int = $interval(function(){
            console.log("interval flushed");
            TestService.increaseValue();
            $interval.cancel(int);
        }, 5000);

      $interval.flush(0);


    };

  }
]);

press on button 'Increase shared value' and see that value gets increased after 5 seconds but not immediately. This is basically issue in my ui tests.

any ideas?

Thanks

@hege91
Copy link

hege91 commented May 21, 2015

In tests you can use $interval.flush(millis) to move forward by millis milliseconds and trigger any functions scheduled to run in that time.

Try changing $interval.flush(0) to $interval.flush(10000)

@vytautas-pranskunas-
Copy link
Author

I will try this a bit later
Why it is different from $timeout? because basically flush skips witting time for both. Not very intuitive.

Plus I noticed that $timeout.flush not documented.
https://docs.angularjs.org/api/ng/service/$timeout

@vytautas-pranskunas-
Copy link
Author

I have tried $interval.flush(10000) when $interval waiting time is 5000 - it does not work for me

@vytautas-pranskunas-
Copy link
Author

Is there any progress or thoughts on this?

@caitp
Copy link
Contributor

caitp commented May 27, 2015

The plunkr isn't including the ngMock module, which provides the decorated $interval service with a "flush" method. The ngMock services are intended for unit testing (to enable you to write fast, non-flaky, synchronous unit tests). Your application code should never be calling $interval.flush() itself, and I'm not sure protractor specs can trigger fake timeouts (though WebDriver may have some ability to do this, I wouldn't bet on it). @juliemr would know better about that.

@vytautas-pranskunas-
Copy link
Author

You have misunderstood me. I had issue with protractor with $timeout so i swithced to $interval witch is ignored by protractor and works as expected. But after switching to $interval i faced problem in my UI tests where I am testing controllers (ngMock included) and $interval.flush does not work there.

Do you have any example in plunker with tests included ngMock to try it your self?

@juliemr
Copy link
Member

juliemr commented May 27, 2015

Here is an example of tests using $interval.flush: https://github.com/angular/angular.js/blob/master/test/ngMock/angular-mocksSpec.js#L286

Does that help?

@vytautas-pranskunas-
Copy link
Author

Hmm I tried once more and now it seems working. Only thing I can think of is some Chrome's super cache when it was $timeout service injected in tests and $interval in code. But it is wired because i have tired it on 3 developers machines. Any way as far as it works the only issue left is protractor browser.wait + $timeout angular/protractor#169 (comment)
@juliemr

p.s. thanks

@Narretz Narretz added this to the Purgatory milestone Jun 11, 2015
@Narretz
Copy link
Contributor

Narretz commented Jun 11, 2015

@vytautas-pranskunas- is this now resolved on the angular side?

@vytautas-pranskunas-
Copy link
Author

Yes, but it sad that angular/protractor#169 (comment) gets ignored.

@pkozlowski-opensource
Copy link
Member

OK, could we take to the protractor, then? @vytautas-pranskunas- could you please open a new issue on the protractor side with the latest details / reproduce scenario? Thnx!

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

No branches or pull requests

6 participants