Skip to content

Commit

Permalink
fix(azure-iot-device): fix issues where token wouldn't refresh if sto…
Browse files Browse the repository at this point in the history
…p() was called previously (#1051)

* fix(azure-iot-device): fix issues where token wouldn't refresh if stop() was called previously

* fix typo
  • Loading branch information
vishnureddy17 authored Oct 12, 2021
1 parent 29c9957 commit b9c0eeb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion device/core/src/device_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class Client extends InternalClient {
if (err) {
debugErrors('transport disconnect event: ' + err);
} else {
debug('transport disconenct event: no error');
debug('transport disconnect event: no error');
}
if (err && this._retryPolicy.shouldRetry(err)) {
debugErrors('reconnect policy specifies a reconnect on error');
Expand Down
1 change: 1 addition & 0 deletions device/core/src/sak_authentication_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class SharedAccessKeyAuthenticationProvider extends EventEmitter implemen
if (this._renewalTimeout) {
clearTimeout(this._renewalTimeout);
this._renewalTimeout = null;
this._currentTokenExpiryTimeInSeconds = undefined;
}
}

Expand Down
15 changes: 15 additions & 0 deletions device/core/test/_sak_authentication_provider_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,21 @@ describe('SharedAccessKeyAuthenticationProvider', function () {
sakAuthProvider.stop();
});
});

it('clears _currentTokenExpiryTimeInSeconds so that _shouldRenewToken returns true', async function () {
const fakeCredentials = {
deviceId: 'fakeDeviceId',
host: 'fake.host.name',
sharedAccessKey: 'fakeKey'
};
const sakAuthProvider = new SharedAccessKeyAuthenticationProvider(fakeCredentials, 10, 1);
await sakAuthProvider.getDeviceCredentials();
assert.isFalse(isNaN(sakAuthProvider._currentTokenExpiryTimeInSeconds), 'isNaN() returned true when it should have been false');
assert.isFalse(sakAuthProvider._shouldRenewToken(), '_shouldRenewToken() returned true when it should have been false');
sakAuthProvider.stop();
assert.isTrue(isNaN(sakAuthProvider._currentTokenExpiryTimeInSeconds), 'isNaN() returned false when it should have been true');
assert.isTrue(sakAuthProvider._shouldRenewToken(), '_shouldRenewToken() returned false when it should have been true');
});
});

describe('setTokenRenewalValues', function () {
Expand Down

0 comments on commit b9c0eeb

Please sign in to comment.