cache miss, unverify during fetch, return from server

This commit is contained in:
Michael Peters 2022-10-11 06:35:23 -07:00
parent 877e7de745
commit bae579da86

View File

@ -826,8 +826,79 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(verifyFunc).toHaveBeenCalledTimes(1);
});
test('primary null, unverify during first trusted, then trusted with value - cache miss, unverify, return from server', async () => {
const {
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const primary = nextPrimary();
const trusted1 = nextTrusted();
const trusted2 = nextTrusted();
const verify = nextVerify();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
const resultPromise = av.fetchAndVerifyIfNeeded();
let result: BasicWE | null | undefined = undefined;
resultPromise.then(value => { result = value; });
expect(av.primaryPromise).toBe(primary.promise);
expect(av.trustedPromise).toBe(trusted1.promise);
expect(av.trustedStatus).toBe('fetching');
expect(primaryFunc).toHaveBeenCalled();
expect(trustedFunc).toHaveBeenCalledTimes(1);
primary.resolve(null);
await disjoint();
expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(trusted1.promise);
expect(av.trustedStatus).toBe('verifying');
av.unverify();
expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(null);
expect(av.trustedStatus).toBe('none');
const cwSpy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {}); // suppress the warning
trusted1.resolve(new BasicWE(2));
await disjoint();
expect(cwSpy).toHaveBeenCalled();
expect(trustedFunc).toHaveBeenCalledTimes(2);
expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(trusted2.promise);
expect(av.trustedStatus).toBe('verifying');
expect(verifyFunc).toHaveBeenCalledTimes(0);
trusted2.resolve(new BasicWE(3));
await disjoint();
expect(verifyFunc).toHaveBeenCalledWith(null, new BasicWE(3));
expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(trusted2.promise);
expect(av.trustedStatus).toBe('verifying');
expect(result).toBeUndefined();
verify.resolve(true);
await disjoint();
expect(result).toEqual(new BasicWE(3));
expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(trusted2.promise);
expect(av.trustedStatus).toBe('verified');
await disjoint(); // sanity check
expect(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(2);
expect(verifyFunc).toHaveBeenCalledTimes(1);
});
// Make sure to do try/catch errors/rejections, verification failures, unverifies in the middle
// Multiple tryResolveTrustedPromises - multiple fetchAndVerifyIfNeeded at the same time
// Trusted resolves *BEFORE* ensureTrustedFuncReady
// Fetching after verified doesn't re-fetch trusted
// Unverify during verify w/ dedups
});