Skip to content

Commit

Permalink
feat(auth): delay client cache reset (#2495)
Browse files Browse the repository at this point in the history
this should mitigate the race conditions referenced by #2477
  • Loading branch information
griest024 committed Jul 5, 2023
1 parent 5c7669d commit 33c3c9b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
36 changes: 26 additions & 10 deletions libs/auth/state/src/effects/auth.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Observable ,
of,
} from 'rxjs';
import { TestScheduler } from 'rxjs/testing';

import { DaffAuthStorageService } from '@daffodil/auth';
import {
Expand Down Expand Up @@ -38,10 +39,15 @@ import {

import { DaffAuthEffects } from './auth.effects';

const getScheduler = () => new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});

describe('@daffodil/auth/state | DaffAuthEffects', () => {
let actions$: Observable<any>;
let effects: DaffAuthEffects;

let scheduler: TestScheduler;
let daffAuthStorageService: DaffAuthStorageService;
let daffAuthDriver: jasmine.SpyObj<DaffAuthServiceInterface>;
let clientCacheSpy: jasmine.SpyObj<DaffDriverHttpClientCacheServiceInterface>;
Expand Down Expand Up @@ -204,13 +210,18 @@ describe('@daffodil/auth/state | DaffAuthEffects', () => {

beforeEach(() => {
revokeAction = new DaffAuthCheckFailure(null);
actions$ = hot('--a', { a: revokeAction });
expected = cold('---');
});

it('should reset the client cache', () => {
expect(effects.clearClientCache$).toBeObservable(expected);
expect(clientCacheSpy.reset).toHaveBeenCalledWith();
it('should reset the client cache after a delay', () => {
scheduler = getScheduler();
scheduler.run(({ expectObservable, time, flush, hot: createHot }) => {
actions$ = createHot('--a-', { a: revokeAction });
expectObservable(effects.clearClientCache$(10, scheduler)).toBe('---');
expect(clientCacheSpy.reset).not.toHaveBeenCalled();
time('10|');
flush();
expect(clientCacheSpy.reset).toHaveBeenCalledWith();
});
});
});

Expand All @@ -219,13 +230,18 @@ describe('@daffodil/auth/state | DaffAuthEffects', () => {

beforeEach(() => {
revokeAction = new DaffAuthGuardLogout(null);
actions$ = hot('--a', { a: revokeAction });
expected = cold('---');
});

it('should reset the client cache', () => {
expect(effects.clearClientCache$).toBeObservable(expected);
expect(clientCacheSpy.reset).toHaveBeenCalledWith();
it('should reset the client cache after a delay', () => {
scheduler = getScheduler();
scheduler.run(({ expectObservable, time, flush, hot: createHot }) => {
actions$ = createHot('--a-', { a: revokeAction });
expectObservable(effects.clearClientCache$(10, scheduler)).toBe('---');
expect(clientCacheSpy.reset).not.toHaveBeenCalled();
time('10|');
flush();
expect(clientCacheSpy.reset).toHaveBeenCalledWith();
});
});
});
});
Expand Down
5 changes: 4 additions & 1 deletion libs/auth/state/src/effects/auth.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
of,
EMPTY,
asyncScheduler,
} from 'rxjs';
import {
switchMap,
Expand All @@ -18,6 +19,7 @@ import {
repeat,
filter,
tap,
delay,
} from 'rxjs/operators';

import {
Expand Down Expand Up @@ -111,12 +113,13 @@ export class DaffAuthEffects {
}),
));

clearClientCache$ = createEffect(() => this.actions$.pipe(
clearClientCache$ = createEffect(() => (delayTime = 10, scheduler = asyncScheduler) => this.actions$.pipe(
ofType(
DaffAuthActionTypes.AuthCheckFailureAction,
DaffAuthActionTypes.AuthGuardLogoutAction,
DaffAuthLoginActionTypes.LogoutSuccessAction,
),
delay(delayTime, scheduler),
tap(() => {
this.clientCache.reset();
}),
Expand Down

0 comments on commit 33c3c9b

Please sign in to comment.