Skip to content

Commit

Permalink
added test for starting leaderElection service
Browse files Browse the repository at this point in the history
  • Loading branch information
denysoblohin-okta committed May 4, 2022
1 parent 8bfc3d0 commit 704aacf
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions test/spec/ServiceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,28 @@ describe('ServiceManager', () => {
jest.useRealTimers();
});

it('doesn\'t start leaderElection service if other services don\'t require leadership', () => {
const options = { tokenManager: { syncStorage: false, autoRenew: true } };
const client = createAuth(options);
client.serviceManager.start();
expect(client.serviceManager.isLeaderRequired()).toBeFalsy();
expect(client.serviceManager.getService('leaderElection')?.isStarted()).toBeFalsy();
client.serviceManager.stop();
});

it('starts leaderElection service if any service (autoRenew) requires leadership', () => {
const options = { tokenManager: { syncStorage: true, autoRenew: true } };
const client = createAuth(options);
client.serviceManager.start();
expect(client.serviceManager.isLeaderRequired()).toBeTruthy();
expect(client.serviceManager.getService('leaderElection')?.isStarted()).toBeTruthy();
client.serviceManager.stop();
});

it('starts syncStorage service for every tab, autoRenew service for leader tab (for syncStorage == true)', () => {
const options = { tokenManager: { syncStorage: true, autoRenew: true } };
let client1 = createAuth(options);
let client2 = createAuth(options);
const client1 = createAuth(options);
const client2 = createAuth(options);
util.disableLeaderElection();
jest.spyOn(client1.serviceManager, 'isLeader').mockReturnValue(true);
jest.spyOn(client2.serviceManager, 'isLeader').mockReturnValue(false);
Expand All @@ -84,8 +102,8 @@ describe('ServiceManager', () => {

it('starts autoRenew service for every tab (for syncStorage == false)', () => {
const options = { tokenManager: { syncStorage: false, autoRenew: true } };
let client1 = createAuth(options);
let client2 = createAuth(options);
const client1 = createAuth(options);
const client2 = createAuth(options);
util.disableLeaderElection();
jest.spyOn(client1.serviceManager, 'isLeader').mockReturnValue(true);
jest.spyOn(client2.serviceManager, 'isLeader').mockReturnValue(false);
Expand All @@ -101,8 +119,8 @@ describe('ServiceManager', () => {

it('starts no services for syncStorage == false and autoRenew == false', () => {
const options = { tokenManager: { syncStorage: false, autoRenew: false } };
let client1 = createAuth(options);
let client2 = createAuth(options);
const client1 = createAuth(options);
const client2 = createAuth(options);
util.disableLeaderElection();
jest.spyOn(client1.serviceManager, 'isLeader').mockReturnValue(true);
jest.spyOn(client2.serviceManager, 'isLeader').mockReturnValue(false);
Expand All @@ -118,7 +136,7 @@ describe('ServiceManager', () => {

it('starts autoRenew service after becoming leader (for syncStorage == true)', async () => {
const options = { tokenManager: { syncStorage: true, autoRenew: true } };
let client = createAuth(options);
const client = createAuth(options);
client.serviceManager.start();
expect(client.serviceManager.isLeader()).toBeFalsy();
expect(client.serviceManager.getService('autoRenew')?.isStarted()).toBeFalsy();
Expand Down

0 comments on commit 704aacf

Please sign in to comment.