verifyFunc false tests

This commit is contained in:
Michael Peters 2022-10-19 22:13:25 -07:00
parent 5df8948886
commit 46e0d4c1a7
2 changed files with 117 additions and 3 deletions

View File

@ -444,7 +444,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(result).toBeNull(); expect(result).toBeNull();
expect(av.primaryPromise).toBe(null); expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedPromise).toBe(null);
expect(av.trustedStatus).toBe('none'); expect(av.trustedStatus).toBe('none');
await disjoint(); // sanity check await disjoint(); // sanity check
@ -497,7 +497,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
await disjoint(); await disjoint();
expect(av.primaryPromise).toBe(null); expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedPromise).toBe(null);
expect(av.trustedStatus).toBe('none'); expect(av.trustedStatus).toBe('none');
await disjoint(); // sanity check await disjoint(); // sanity check
@ -894,6 +894,63 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(trustedFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1);
expect(verifyFunc).toHaveBeenCalledTimes(0); expect(verifyFunc).toHaveBeenCalledTimes(0);
}); });
test('ab, a: primary with null, b: primary dedup, ab: trusted dedup with value, verifyFunc false', async () => {
const {
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
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(primaryFunc).toHaveBeenCalled();
expect(trustedFunc).toHaveBeenCalled();
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(false);
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(null);
expect(av.trustedStatus).toBe('none');
await disjoint(); // sanity check
// 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('a: primary with null, b: primary re-fetch null, a: trusted with value, b: dedup', async () => { test('a: primary with null, b: primary re-fetch null, a: trusted with value, b: dedup', async () => {
const { const {
@ -1278,6 +1335,63 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(verifyFunc).toHaveBeenCalledTimes(1); expect(verifyFunc).toHaveBeenCalledTimes(1);
}); });
test('ab: a: primary with null, b: primary dedup, ab: verifyFunc false, ab: resolve with trusted', async () => {
const {
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
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(primaryFunc).toHaveBeenCalled();
expect(trustedFunc).toHaveBeenCalled();
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(false);
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(null);
expect(av.trustedStatus).toBe('none');
await disjoint(); // sanity check
// 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);
});
// TODO: Why not have fetcher and a simple dedup wrapper to remove some complexity from this function? // TODO: Why not have fetcher and a simple dedup wrapper to remove some complexity from this function?
// This would likely make it possible to get rid of the "else" block in tryResolveTrustedPromise // This would likely make it possible to get rid of the "else" block in tryResolveTrustedPromise
// Make sure it would work properly for retry functionality // Make sure it would work properly for retry functionality
@ -1287,7 +1401,6 @@ describe('fetchAndVerifyIfNeeded tests', () => {
// Fetching after verified doesn't re-fetch trusted // Fetching after verified doesn't re-fetch trusted
// Unverify during verify w/ dedups // Unverify during verify w/ dedups
// verifyFunc returns false // verifyFunc returns false
// verifyFunc returns false while different trusted succeeds? - this is not possible
// verifyFunc rejects // verifyFunc rejects
// not possible (and therefore not tested) list // not possible (and therefore not tested) list

View File

@ -272,6 +272,7 @@ export class AutoVerifier<T> {
} else { } else {
// we have to re-fetch the trusted promise again next fetch // we have to re-fetch the trusted promise again next fetch
this.trustedStatus = 'none'; this.trustedStatus = 'none';
this.trustedPromise = null;
} }
} else { } else {
// this actually could be quite common // this actually could be quite common