From 5013890b3516cca4a12797aa8a510ee832322c0c Mon Sep 17 00:00:00 2001 From: Yura Bondarenko Date: Mon, 11 Jul 2022 18:25:05 +0200 Subject: [PATCH 1/2] 93011: Add test for invalidateByHref cache state tracking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This test is expected to fail and is be fixed in the next commit. Currently, invalidateByHref calls keep tracking the cache and may cause objects to be invalidated a second time as soon as the cache is updated. Practical example: 1. DELETE request → object is invalidated 2. POST request related to the same object → UUID of pending request is added to the object's cache entry → gets picked up by the previous invalidateByHref call → the request is set to stale even before it's completed --- src/app/core/data/data.service.spec.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/app/core/data/data.service.spec.ts b/src/app/core/data/data.service.spec.ts index 64efd58418..dc661e12d7 100644 --- a/src/app/core/data/data.service.spec.ts +++ b/src/app/core/data/data.service.spec.ts @@ -894,6 +894,26 @@ describe('DataService', () => { expectObservable(done$).toBe('------(t|)', BOOLEAN); }); }); + + it('should only fire for the current state of the object (instead of tracking it)', () => { + testScheduler.run(({ cold, flush }) => { + getByHrefSpy.and.returnValue(cold('a---b---c---', { + a: { requestUUIDs: ['request1'] }, // this is the state at the moment we're invalidating the cache + b: { requestUUIDs: ['request2'] }, // we shouldn't keep tracking the state + c: { requestUUIDs: ['request3'] }, // because we may invalidate when we shouldn't + })); + + service.invalidateByHref('some-href'); + flush(); + + // requests from the first state are marked as stale + expect(requestService.setStaleByUUID).toHaveBeenCalledWith('request1'); + + // request from subsequent states are ignored + expect(requestService.setStaleByUUID).not.toHaveBeenCalledWith('request2'); + expect(requestService.setStaleByUUID).not.toHaveBeenCalledWith('request3'); + }); + }); }); describe('delete', () => { From 517aee0e8cd0a8f2c8d891dfe215804818b00d38 Mon Sep 17 00:00:00 2001 From: Yura Bondarenko Date: Mon, 11 Jul 2022 18:25:41 +0200 Subject: [PATCH 2/2] 93011: Make sure invalidateByHref doesn't track cache state --- src/app/core/data/data.service.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/app/core/data/data.service.ts b/src/app/core/data/data.service.ts index 1cd9731a65..6176694d9d 100644 --- a/src/app/core/data/data.service.ts +++ b/src/app/core/data/data.service.ts @@ -595,6 +595,7 @@ export abstract class DataService implements UpdateDa const done$ = new AsyncSubject(); this.objectCache.getByHref(href).pipe( + take(1), switchMap((oce: ObjectCacheEntry) => observableFrom(oce.requestUUIDs).pipe( mergeMap((requestUUID: string) => this.requestService.setStaleByUUID(requestUUID)), toArray(),