Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
fix(RefreshTimer): Run refresh timer outside angular zone
Browse files Browse the repository at this point in the history
When running tests with protractor, protractor waits for all timeouts to be resolved.
At any given time, there is a timer for token refresh and so protractor waits indefinitely.
With this patch, protractor doesn't wait for token refresh timeout and hence it enables protractor
to run tests normally.
  • Loading branch information
jarifibrahim authored and joshuawilson committed Dec 13, 2017
1 parent 5314b1c commit b0d0449
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/app/auth/authentication.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Inject } from '@angular/core';
import { Injectable, Inject, NgZone } from '@angular/core';
import { Http, Response, Headers, RequestOptions, RequestOptionsArgs } from '@angular/http';

import { Observable, Subject } from 'rxjs';
Expand Down Expand Up @@ -32,7 +32,8 @@ export class AuthenticationService {
@Inject(AUTH_API_URL) apiUrl: string,
@Inject(SSO_API_URL) ssoUrl: string,
@Inject(REALM) realm: string,
private http: Http
private http: Http,
private ngZone: NgZone
) {
this.apiUrl = apiUrl;
this.ssoUrl = ssoUrl;
Expand Down Expand Up @@ -100,12 +101,15 @@ export class AuthenticationService {
let refreshInMs = Math.round(refreshInSeconds * .9) * 1000;
console.log('Refreshing token in: ' + refreshInMs + ' milliseconds.');
this.refreshInterval = refreshInMs;
if (process.env.ENV !== 'inmemory') {
// setTimeout() uses a 32 bit int to store the delay. So the max value allowed is 2147483647
// The bigger number will cause immediate refreshing
// but since we refresh in 10 minutes or in refreshInSeconds whatever is sooner we are good
this.clearTimeoutId = setTimeout(() => this.refreshToken(), refreshInMs);
}
// Run setTimeout outside angular zone. Without this, all protractor tests fail with
// "Waiting for Angular" timeout as protractor waits indefinitely.
// https://github.com/angular/protractor/blob/master/docs/timeouts.md#angular
this.ngZone.runOutsideAngular(() => {
// setTimeout() uses a 32 bit int to store the delay. So the max value allowed is 2147483647
// The bigger number will cause immediate refreshing
// but since we refresh in 10 minutes or in refreshInSeconds whatever is sooner we are good
this.clearTimeoutId = setTimeout(() => this.refreshToken(), refreshInMs);
});
}
}

Expand Down

0 comments on commit b0d0449

Please sign in to comment.