retab again (need to figure out a way to make this auto)

This commit is contained in:
Michael Peters 2022-10-06 23:00:15 -07:00
parent 5326f067be
commit 5f1e9fddc0

View File

@ -186,33 +186,33 @@ describe('fetchAndVerifyIfNeeded tests', () => {
} }
} }
function fetchAndVerifyIfNeededManually<T>( function fetchAndVerifyIfNeededManually<T>(
av: AutoVerifier<T>, av: AutoVerifier<T>,
nextPrimary: () => ManualPromise<T | null>, nextPrimary: () => ManualPromise<T | null>,
nextTrusted: () => ManualPromise<T | null>, nextTrusted: () => ManualPromise<T | null>,
nextVerify: () => ManualPromise<boolean>, nextVerify: () => ManualPromise<boolean>,
lazyVerify: boolean = false, lazyVerify: boolean = false,
) { ) {
const state: { const state: {
primary: ManualPromise<T | null>, primary: ManualPromise<T | null>,
trusted: ManualPromise<T | null>, trusted: ManualPromise<T | null>,
verify: ManualPromise<boolean>, verify: ManualPromise<boolean>,
result: T | null | undefined, result: T | null | undefined,
rejection: Error | undefined, rejection: Error | undefined,
} = { } = {
primary: nextPrimary(), primary: nextPrimary(),
trusted: nextTrusted(), trusted: nextTrusted(),
verify: nextVerify(), verify: nextVerify(),
result: undefined, result: undefined,
rejection: undefined, rejection: undefined,
} }
const resultPromise = av.fetchAndVerifyIfNeeded(lazyVerify); const resultPromise = av.fetchAndVerifyIfNeeded(lazyVerify);
resultPromise.then((value) => { state.result = value; }); resultPromise.then((value) => { state.result = value; });
resultPromise.catch((value) => { state.rejection = value; }); resultPromise.catch((value) => { state.rejection = value; });
return state; return state;
} }
async function disjoint() { async function disjoint() {
await Util.sleep(0); await Util.sleep(0);
@ -649,121 +649,121 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(verifyFunc).toHaveBeenCalledTimes(0); expect(verifyFunc).toHaveBeenCalledTimes(0);
}); });
test('ab, a: primary with value, b: primary dedup, ab: trusted dedup with value', async () => { test('ab, a: primary with value, b: primary dedup, ab: trusted dedup with value', async () => {
const { const {
nextPrimary, nextTrusted, nextVerify, nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks(); } = getManualsAndMocks();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
const a = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify); const a = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify);
expect(av.primaryPromise).toBe(a.primary.promise); expect(av.primaryPromise).toBe(a.primary.promise);
expect(av.trustedPromise).toBe(a.trusted.promise); expect(av.trustedPromise).toBe(a.trusted.promise);
expect(av.trustedStatus).toBe('fetching'); expect(av.trustedStatus).toBe('fetching');
expect(primaryFunc).toHaveBeenCalled(); expect(primaryFunc).toHaveBeenCalled();
expect(trustedFunc).toHaveBeenCalled(); expect(trustedFunc).toHaveBeenCalled();
const b = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify); const b = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify);
expect(av.primaryPromise).toBe(a.primary.promise); // doesn't change from a expect(av.primaryPromise).toBe(a.primary.promise); // doesn't change from a
expect(av.trustedPromise).toBe(a.trusted.promise); // doesn't change from a expect(av.trustedPromise).toBe(a.trusted.promise); // doesn't change from a
expect(av.trustedStatus).toBe('fetching'); expect(av.trustedStatus).toBe('fetching');
expect(a.result).toBeUndefined(); expect(a.result).toBeUndefined();
expect(b.result).toBeUndefined(); expect(b.result).toBeUndefined();
a.primary.resolve(new BasicWE(2)); a.primary.resolve(new BasicWE(2));
await disjoint(); await disjoint();
expect(a.result).toEqual(new BasicWE(2)); expect(a.result).toEqual(new BasicWE(2));
expect(b.result).toEqual(new BasicWE(2)); expect(b.result).toEqual(new BasicWE(2));
expect(av.primaryPromise).toBe(null); expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(a.trusted.promise); expect(av.trustedPromise).toBe(a.trusted.promise);
expect(av.trustedStatus).toBe('verifying'); expect(av.trustedStatus).toBe('verifying');
expect(verifyFunc).toHaveBeenCalledTimes(0); expect(verifyFunc).toHaveBeenCalledTimes(0);
a.trusted.resolve(new BasicWE(2)); a.trusted.resolve(new BasicWE(2));
await disjoint(); await disjoint();
expect(verifyFunc).toHaveBeenCalledWith(new BasicWE(2), new BasicWE(2)); expect(verifyFunc).toHaveBeenCalledWith(new BasicWE(2), new BasicWE(2));
expect(av.primaryPromise).toBe(null); expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(a.trusted.promise); expect(av.trustedPromise).toBe(a.trusted.promise);
expect(av.trustedStatus).toBe('verifying'); expect(av.trustedStatus).toBe('verifying');
a.verify.resolve(true); a.verify.resolve(true);
await disjoint(); await disjoint();
expect(av.primaryPromise).toBe(null); expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(a.trusted.promise); expect(av.trustedPromise).toBe(a.trusted.promise);
expect(av.trustedStatus).toBe('verified'); expect(av.trustedStatus).toBe('verified');
await disjoint(); // sanity check await disjoint(); // sanity check
// Even though there were two fetchAnd... calls, they were deduped such that each func was only called once // Even though there were two fetchAnd... calls, they were deduped such that each func was only called once
expect(primaryFunc).toHaveBeenCalledTimes(1); expect(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1);
expect(verifyFunc).toHaveBeenCalledTimes(1); expect(verifyFunc).toHaveBeenCalledTimes(1);
}); });
test('ab, a: primary with null, b: primary dedup, ab: trusted dedup with value', async () => { test('ab, a: primary with null, b: primary dedup, ab: trusted dedup with value', async () => {
const { const {
nextPrimary, nextTrusted, nextVerify, nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks(); } = getManualsAndMocks();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
const a = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify); const a = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify);
expect(av.primaryPromise).toBe(a.primary.promise); expect(av.primaryPromise).toBe(a.primary.promise);
expect(av.trustedPromise).toBe(a.trusted.promise); expect(av.trustedPromise).toBe(a.trusted.promise);
expect(av.trustedStatus).toBe('fetching'); expect(av.trustedStatus).toBe('fetching');
expect(primaryFunc).toHaveBeenCalled(); expect(primaryFunc).toHaveBeenCalled();
expect(trustedFunc).toHaveBeenCalled(); expect(trustedFunc).toHaveBeenCalled();
const b = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify); const b = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify);
expect(av.primaryPromise).toBe(a.primary.promise); // doesn't change from a
expect(av.trustedPromise).toBe(a.trusted.promise); // doesn't change from a
expect(av.trustedStatus).toBe('fetching');
a.primary.resolve(null);
await disjoint();
expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(a.trusted.promise);
expect(av.trustedStatus).toBe('verifying');
expect(verifyFunc).toHaveBeenCalledTimes(0);
a.trusted.resolve(new BasicWE(2));
await disjoint();
expect(verifyFunc).toHaveBeenCalledWith(null, new BasicWE(2));
expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(a.trusted.promise);
expect(av.trustedStatus).toBe('verifying');
expect(a.result).toBeUndefined();
expect(b.result).toBeUndefined();
a.verify.resolve(true);
await disjoint();
expect(a.result).toEqual(new BasicWE(2)); expect(av.primaryPromise).toBe(a.primary.promise); // doesn't change from a
expect(b.result).toEqual(new BasicWE(2)); expect(av.trustedPromise).toBe(a.trusted.promise); // doesn't change from a
expect(av.primaryPromise).toBe(null); expect(av.trustedStatus).toBe('fetching');
a.primary.resolve(null);
await disjoint();
expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(a.trusted.promise);
expect(av.trustedStatus).toBe('verifying');
expect(verifyFunc).toHaveBeenCalledTimes(0);
a.trusted.resolve(new BasicWE(2));
await disjoint();
expect(verifyFunc).toHaveBeenCalledWith(null, new BasicWE(2));
expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(a.trusted.promise);
expect(av.trustedStatus).toBe('verifying');
expect(a.result).toBeUndefined();
expect(b.result).toBeUndefined();
a.verify.resolve(true);
await disjoint();
expect(a.result).toEqual(new BasicWE(2));
expect(b.result).toEqual(new BasicWE(2));
expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(a.trusted.promise); expect(av.trustedPromise).toBe(a.trusted.promise);
expect(av.trustedStatus).toBe('verified'); expect(av.trustedStatus).toBe('verified');
await disjoint(); // sanity check await disjoint(); // sanity check
// Even though there were two fetchAnd... calls, they were deduped such that each func was only called once // Even though there were two fetchAnd... calls, they were deduped such that each func was only called once
expect(primaryFunc).toHaveBeenCalledTimes(1); expect(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1);
expect(verifyFunc).toHaveBeenCalledTimes(1); expect(verifyFunc).toHaveBeenCalledTimes(1);
}); });
// Make sure to do try/catch errors/rejections, verification failures, unverifies in the middle // Make sure to do try/catch errors/rejections, verification failures, unverifies in the middle
// Multiple tryResolveTrustedPromises - multiple fetchAndVerifyIfNeeded at the same time // Multiple tryResolveTrustedPromises - multiple fetchAndVerifyIfNeeded at the same time
// Trusted resolves *BEFORE* ensureTrustedFuncReady // Trusted resolves *BEFORE* ensureTrustedFuncReady
// Fetching after verified doesn't re-fetch trusted // Fetching after verified doesn't re-fetch trusted
}); });