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>(
av: AutoVerifier<T>,
nextPrimary: () => ManualPromise<T | null>,
nextTrusted: () => ManualPromise<T | null>,
nextVerify: () => ManualPromise<boolean>,
lazyVerify: boolean = false,
) {
const state: {
primary: ManualPromise<T | null>,
trusted: ManualPromise<T | null>,
verify: ManualPromise<boolean>,
result: T | null | undefined,
rejection: Error | undefined,
} = {
primary: nextPrimary(),
trusted: nextTrusted(),
verify: nextVerify(),
result: undefined,
rejection: undefined,
}
function fetchAndVerifyIfNeededManually<T>(
av: AutoVerifier<T>,
nextPrimary: () => ManualPromise<T | null>,
nextTrusted: () => ManualPromise<T | null>,
nextVerify: () => ManualPromise<boolean>,
lazyVerify: boolean = false,
) {
const state: {
primary: ManualPromise<T | null>,
trusted: ManualPromise<T | null>,
verify: ManualPromise<boolean>,
result: T | null | undefined,
rejection: Error | undefined,
} = {
primary: nextPrimary(),
trusted: nextTrusted(),
verify: nextVerify(),
result: undefined,
rejection: undefined,
}
const resultPromise = av.fetchAndVerifyIfNeeded(lazyVerify);
resultPromise.then((value) => { state.result = value; });
resultPromise.catch((value) => { state.rejection = value; });
const resultPromise = av.fetchAndVerifyIfNeeded(lazyVerify);
resultPromise.then((value) => { state.result = value; });
resultPromise.catch((value) => { state.rejection = value; });
return state;
}
return state;
}
async function disjoint() {
await Util.sleep(0);
@ -649,121 +649,121 @@ describe('fetchAndVerifyIfNeeded tests', () => {
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 {
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = 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.trustedPromise).toBe(a.trusted.promise);
expect(av.trustedStatus).toBe('fetching');
expect(av.primaryPromise).toBe(a.primary.promise);
expect(av.trustedPromise).toBe(a.trusted.promise);
expect(av.trustedStatus).toBe('fetching');
expect(primaryFunc).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');
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');
expect(a.result).toBeUndefined();
expect(b.result).toBeUndefined();
a.primary.resolve(new BasicWE(2));
await disjoint();
expect(a.result).toBeUndefined();
expect(b.result).toBeUndefined();
a.primary.resolve(new BasicWE(2));
await disjoint();
expect(a.result).toEqual(new BasicWE(2));
expect(b.result).toEqual(new BasicWE(2));
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.trustedStatus).toBe('verifying');
expect(verifyFunc).toHaveBeenCalledTimes(0);
a.trusted.resolve(new BasicWE(2));
await disjoint();
expect(verifyFunc).toHaveBeenCalledTimes(0);
a.trusted.resolve(new BasicWE(2));
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.trustedPromise).toBe(a.trusted.promise);
expect(av.trustedStatus).toBe('verifying');
a.verify.resolve(true);
await disjoint();
a.verify.resolve(true);
await disjoint();
expect(av.primaryPromise).toBe(null);
expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(a.trusted.promise);
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(trustedFunc).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 {
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = 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.trustedPromise).toBe(a.trusted.promise);
expect(av.trustedStatus).toBe('fetching');
expect(av.primaryPromise).toBe(a.primary.promise);
expect(av.trustedPromise).toBe(a.trusted.promise);
expect(av.trustedStatus).toBe('fetching');
expect(primaryFunc).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');
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();
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).toHaveBeenCalledTimes(0);
a.trusted.resolve(new BasicWE(2));
await disjoint();
expect(verifyFunc).toHaveBeenCalledWith(null, new BasicWE(2));
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).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(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.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(trustedFunc).toHaveBeenCalledTimes(1);
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
// Trusted resolves *BEFORE* ensureTrustedFuncReady
// Fetching after verified doesn't re-fetch trusted
});