Skip to content

Commit

Permalink
fixup! test: use fine-grained expected unhandled rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Feb 18, 2025
1 parent 2b9ae71 commit 4e18904
Show file tree
Hide file tree
Showing 2 changed files with 250 additions and 1 deletion.
248 changes: 248 additions & 0 deletions packages/vow/test/unhandled-rejections.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
import {
annihilate,
startLife,
test,
} from '@agoric/swingset-vat/tools/prepare-strict-test-env-ava.js';

import { Far } from '@endo/far';
import { makeDurableZone } from '@agoric/zone/durable.js';

import { makeExpectUnhandledRejectionMacro } from '@agoric/internal/src/lib-nodejs/ava-unhandled-rejection.js';
import { makeGcAndFinalize } from '@agoric/internal/src/lib-nodejs/gc-and-finalize.js';
import engineGC from '@agoric/internal/src/lib-nodejs/engine-gc.js';
import { prepareVowTools } from '../vat.js';

const expectUnhandled = test.macro(
makeExpectUnhandledRejectionMacro(import.meta.url),
);

const gcAndFinalize = makeGcAndFinalize(engineGC);
test.afterEach.always(gcAndFinalize);

const makeRejectedVow = (
{ makeVowKit },
reason = Error('caught the rejection'),
) => {
const { resolver, vow } = makeVowKit();
resolver.reject(reason);
return vow;
};

const makeAndShortenRejectedVow = async ({ makeVowKit }) => {
const vow = makeRejectedVow({ makeVowKit });
await gcAndFinalize();
return vow.payload.vowV0.shorten();
};

test.serial('vow shortened, then rejected', async t => {
annihilate();

await startLife(async baggage => {
const zone = makeDurableZone(baggage, 'durableRoot');
const vowTools = prepareVowTools(zone);

const testVowKit = vowTools.makeVowKit();

void testVowKit.vow.payload.vowV0.shorten().then(
value => t.fail(value),
reason =>
t.throws(
() => {
throw reason;
},
{ message: 'caught the rejection' },
),
);
testVowKit.resolver.reject(Error('caught the rejection'));
});
});

test.serial('vow resolved to rejected promise, then shortened', async t => {
annihilate();

await startLife(async baggage => {
const zone = makeDurableZone(baggage, 'durableRoot');
const vowTools = prepareVowTools(zone);

const testVowKit = vowTools.makeVowKit();

const rejection = Promise.reject(Error('caught the rejection'));
testVowKit.resolver.resolve(rejection);
testVowKit.vow.payload.vowV0.shorten().then(
value => t.fail(value),
reason =>
t.throws(
() => {
throw reason;
},
{ message: 'caught the rejection' },
),
);
});
});

test.serial.failing(
expectUnhandled,
1,
'vow rejected, then shorten after status update',
async t => {
annihilate();

await startLife(async baggage => {
const zone = makeDurableZone(baggage, 'durableRoot');
const vowTools = prepareVowTools(zone);

const shorter = makeAndShortenRejectedVow(vowTools);
await gcAndFinalize();
shorter.then(
value => t.fail(value),
reason =>
t.throws(
() => {
throw reason;
},
{ message: 'caught the rejection' },
),
);
});
},
);

test.serial.failing(
expectUnhandled,
1,
'vow shortened, upgraded, then shortened again',
async t => {
annihilate();

await startLife(async baggage => {
const zone = makeDurableZone(baggage, 'durableRoot');
const vowTools = prepareVowTools(zone);

const testVowKit = zone.makeOnce('testVowKit', () =>
vowTools.makeVowKit(),
);

testVowKit.vow.payload.vowV0.shorten().then(
value => t.fail(value),
reason => t.fail(reason),
);
});

await startLife(async baggage => {
const zone = makeDurableZone(baggage, 'durableRoot');
prepareVowTools(zone);

const testVowKit = zone.makeOnce('testVowKit', () =>
t.fail('need testVowKit in baggage'),
);

testVowKit.resolver.reject(Error('caught the rejection'));

testVowKit.vow.payload.vowV0.shorten().then(
value => t.fail(value),
reason =>
t.throws(
() => {
throw reason;
},
{ message: 'caught the rejection' },
),
);
});
},
);

test.serial(expectUnhandled, 1, 'vow rejected, then dropped', async t => {
annihilate();

await startLife(async baggage => {
const zone = makeDurableZone(baggage, 'durableRoot');
const vowTools = prepareVowTools(zone);

makeRejectedVow(vowTools);
await gcAndFinalize();
});

await gcAndFinalize();
t.pass();
});

test.serial(
expectUnhandled,
2,
'vow rejected with storable reason, upgraded, then handled',
async t => {
annihilate();

await startLife(async baggage => {
const zone = makeDurableZone(baggage, 'durableRoot');
const vowTools = prepareVowTools(zone);

zone.makeOnce('testVow', () => makeRejectedVow(vowTools));
});

await startLife(async baggage => {
const zone = makeDurableZone(baggage, 'durableRoot');
prepareVowTools(zone);

const vow = zone.makeOnce('testVow', () =>
t.fail('need testVow in baggage'),
);

vow.payload.vowV0.shorten().then(
value => t.fail(value),
reason =>
t.throws(
() => {
throw reason;
},
{ message: 'caught the rejection' },
),
);
});
},
);

test.serial(
expectUnhandled,
2,
'vow rejected with non-storable reason, upgraded, then handled',
async t => {
annihilate();

await startLife(async baggage => {
const zone = makeDurableZone(baggage, 'durableRoot');
const vowTools = prepareVowTools(zone);

const nonStorable = Far('non-storable', {
toString() {
return 'is not storable';
},
});
zone.makeOnce('testVow', () => makeRejectedVow(vowTools, nonStorable));
});

await startLife(async baggage => {
const zone = makeDurableZone(baggage, 'durableRoot');
prepareVowTools(zone);

const vow = zone.makeOnce('testVow', () =>
t.fail('need testVow in baggage'),
);

vow.payload.vowV0.shorten().then(
value => t.fail(value),
reason =>
t.throws(
() => {
throw reason;
},
{
message: `Vow rejection reason was not stored: "[Alleged: non-storable]"`,
},
),
);
});
},
);
3 changes: 2 additions & 1 deletion packages/vow/test/watch-upgrade.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ test(expectUnhandled, 5, 'vow resolve across upgrade', async t => {

vowTools.watch(testVowKit3.vow, watcher2);
testVowKit3.resolver.resolve(42);
testVowKit4.resolver.reject(() => 'is not storable');
const nonStorable = () => 'is not storable';
testVowKit4.resolver.reject(nonStorable);
});

await startLife(
Expand Down

0 comments on commit 4e18904

Please sign in to comment.