primary with value, trusted with null

This commit is contained in:
Michael Peters 2022-10-05 20:57:45 -07:00
parent 90ab1a0c6e
commit f2ec9cfd14
2 changed files with 64 additions and 28 deletions

View File

@ -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<BasicWE>,
trusted: ManualPromise<BasicWE>,
ensureTrustedFuncReady: ManualPromise<void>,
verify: ManualPromise<boolean>
primaryFunc: () => Promise<BasicWE>,
trustedFunc: () => Promise<BasicWE>,
ensureTrustedFuncReadyFunc: () => Promise<void>,
verifyFunc: () => Promise<boolean>,
} {
const primary = new ManualPromise<BasicWE>();
const trusted = new ManualPromise<BasicWE>();
const ensureTrustedFuncReady = new ManualPromise<void>();
const verify = new ManualPromise<boolean>();
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
});

View File

@ -202,6 +202,7 @@ export class AutoVerifier<T> {
/* 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();
}