From f2ec9cfd14da164be60f856acba040c669fdb0cd Mon Sep 17 00:00:00 2001 From: Michael Peters Date: Wed, 5 Oct 2022 20:57:45 -0700 Subject: [PATCH] primary with value, trusted with null --- src/client/tests/webapp/auto-verifier.test.ts | 91 +++++++++++++------ src/client/webapp/auto-verifier.ts | 1 + 2 files changed, 64 insertions(+), 28 deletions(-) diff --git a/src/client/tests/webapp/auto-verifier.test.ts b/src/client/tests/webapp/auto-verifier.test.ts index 79b023e..fc205f9 100644 --- a/src/client/tests/webapp/auto-verifier.test.ts +++ b/src/client/tests/webapp/auto-verifier.test.ts @@ -193,31 +193,7 @@ describe('fetchAndVerifyIfNeeded tests', () => { verifyFunc: jest.fn((_primaryResult: BasicWE | null, _trustedResult: BasicWE | null) => manuals.verify[calls.verify++]!.promise), } } - function getGeneralMocks(): { - primary: ManualPromise, - trusted: ManualPromise, - ensureTrustedFuncReady: ManualPromise, - verify: ManualPromise - primaryFunc: () => Promise, - trustedFunc: () => Promise, - ensureTrustedFuncReadyFunc: () => Promise, - verifyFunc: () => Promise, - } { - const primary = new ManualPromise(); - const trusted = new ManualPromise(); - const ensureTrustedFuncReady = new ManualPromise(); - const verify = new ManualPromise(); - return { - primary, - trusted, - ensureTrustedFuncReady, - verify, - primaryFunc: () => primary.promise, - trustedFunc: () => trusted.promise, - ensureTrustedFuncReadyFunc: () => ensureTrustedFuncReady.promise, - verifyFunc: () => verify.promise, - }; - } + async function disjoint() { await Util.sleep(0); } @@ -408,8 +384,67 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(verifyFunc).toHaveBeenCalledTimes(1); }); - /* test('primary with value, then trusted with null - cache hit, server deleted', async () => { */ - /* expect(false).toBe(true); */ - /* }); */ + test('primary with value, then trusted with null - cache hit, server deleted', async () => { + const { + nextPrimary, nextTrusted, nextEnsureTrustedFuncReady, nextVerify, + primaryFunc, trustedFunc, ensureTrustedFuncReadyFunc, verifyFunc + } = getManualsAndMocks(); + + const primary = nextPrimary(); + const trusted = nextTrusted(); + const ensureTrustedFuncReady = nextEnsureTrustedFuncReady(); + const verify = nextVerify(); + + const av = new AutoVerifier(primaryFunc, trustedFunc, ensureTrustedFuncReadyFunc, 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(trusted.promise); + expect(av.trustedStatus).toBe('fetching'); + expect(primaryFunc).toHaveBeenCalled(); + expect(trustedFunc).toHaveBeenCalled(); + + expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(0); + expect(result).toBeUndefined(); + primary.resolve(new BasicWE(2)); + await disjoint(); + + expect(ensureTrustedFuncReadyFunc).toHaveBeenCalled(); + expect(result).toEqual(new BasicWE(2)); + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(trusted.promise); + expect(av.trustedStatus).toBe('verifying'); + + ensureTrustedFuncReady.resolve(); + await disjoint(); + + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(trusted.promise); + expect(av.trustedStatus).toBe('verifying'); + + expect(verifyFunc).toHaveBeenCalledTimes(0); + trusted.resolve(null); + await disjoint(); + + expect(verifyFunc).toHaveBeenCalledWith(new BasicWE(2), null); + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(trusted.promise); + expect(av.trustedStatus).toBe('verifying'); + + verify.resolve(true); + await disjoint(); + + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(trusted.promise); + expect(av.trustedStatus).toBe('none'); + + expect(primaryFunc).toHaveBeenCalledTimes(1); + expect(trustedFunc).toHaveBeenCalledTimes(1); + expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1); + expect(verifyFunc).toHaveBeenCalledTimes(1); + }); // Make sure to do try/catch errors, verification failures, unverifies in the middle + // Lazy verify - primary null and primary with value }); diff --git a/src/client/webapp/auto-verifier.ts b/src/client/webapp/auto-verifier.ts index 80eb9eb..77ff7a0 100644 --- a/src/client/webapp/auto-verifier.ts +++ b/src/client/webapp/auto-verifier.ts @@ -202,6 +202,7 @@ export class AutoVerifier { /* istanbul ignore next */ if (debug) LOG.debug(fetchId + ": created trusted promise, set to 'fetching'"); this.trustedStatus = 'fetching'; + // TODO: The solution may be to merge the ensureTrustedFuncReady into the trustedPromise this.trustedPromise = this.trustedFunc(); }