Skip to content

Commit

Permalink
Merge pull request #5606 from Agoric/5547-endEventLoop
Browse files Browse the repository at this point in the history
5547 end event loop
  • Loading branch information
mergify[bot] committed Jun 16, 2022
2 parents 1aa4565 + eae458d commit 986ae7f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 33 deletions.
2 changes: 1 addition & 1 deletion packages/run-protocol/test/reserve/test-reserve.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import { E } from '@endo/eventual-send';
import { makeIssuerKit, AmountMath } from '@agoric/ertp';
import buildManualTimer from '@agoric/zoe/tools/manualTimer.js';
import { eventLoopIteration } from '@agoric/zoe/tools/eventLoopIteration.js';

import { setupReserveServices } from './setup.js';
import { unsafeMakeBundleCache } from '../bundleTool.js';
import { subscriptionTracker } from '../metrics.js';
import { eventLoopIteration } from '../supports.js';

const addLiquidPool = async (
runPayment,
Expand Down
13 changes: 0 additions & 13 deletions packages/run-protocol/test/supports.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-check
/* global setImmediate */

import { AmountMath } from '@agoric/ertp';
import binaryVoteCounterBundle from '@agoric/governance/bundles/bundle-binaryVoteCounter.js';
Expand Down Expand Up @@ -34,18 +33,6 @@ export const provideBundle = (t, sourceRoot, bundleName) => {
};
harden(provideBundle);

/**
* A workaround for some issues with fake time in tests.
*
* Lines of test code can depend on async promises outside the test
* resolving before they run. Awaiting this function result ensures
* that all promises that can do resolve.
* Note that this doesn't mean all outstanding promises.
*/
export const eventLoopIteration = async () =>
new Promise(resolve => setImmediate(resolve));
harden(eventLoopIteration);

/**
* Returns promises for `zoe` and the `feeMintAccess`.
*
Expand Down
21 changes: 14 additions & 7 deletions packages/run-protocol/test/test-feeDistributor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';

import { AmountMath } from '@agoric/ertp';
import buildManualTimer from '@agoric/zoe/tools/manualTimer.js';
import { eventLoopIteration } from '@agoric/zoe/tools/eventLoopIteration.js';
import { setup } from '@agoric/zoe/test/unitTests/setupBasicMints.js';

import { assertPayoutAmount } from '@agoric/zoe/test/zoeTestHelpers.js';
import { E, Far } from '@endo/far';
import { makeFeeDistributor } from '../src/feeDistributor.js';
import { eventLoopIteration } from './supports.js';

/**
* @param {Issuer} feeIssuer
*/
function makeFakeFeeDepositFacetKit(feeIssuer) {
const makeFakeFeeDepositFacetKit = feeIssuer => {
const depositPayments = [];

const feeDepositFacet = {
Expand All @@ -29,17 +29,17 @@ function makeFakeFeeDepositFacetKit(feeIssuer) {
Promise.resolve(eventLoopIteration()).then(_ => depositPayments);

return { feeDepositFacet, getPayments };
}
};

function makeFakeFeeProducer(makeEmptyPayment = () => {}) {
const makeFakeFeeProducer = (makeEmptyPayment = () => {}) => {
const feePayments = [];
return Far('feeCollector', {
collectFees: () => feePayments.shift() || makeEmptyPayment(),

// tools for the fake:
pushFees: payment => feePayments.push(payment),
});
}
};
/**
*
* @param {*} t
Expand All @@ -49,7 +49,14 @@ function makeFakeFeeProducer(makeEmptyPayment = () => {}) {
* @param {Issuer} issuer
* @param {Brand} brand
*/
async function assertPaymentArray(t, paymentsP, count, values, issuer, brand) {
const assertPaymentArray = async (
t,
paymentsP,
count,
values,
issuer,
brand,
) => {
const payments = await paymentsP;
for (let i = 0; i < count; i += 1) {
// XXX https://github.com/Agoric/agoric-sdk/issues/5527
Expand All @@ -61,7 +68,7 @@ async function assertPaymentArray(t, paymentsP, count, values, issuer, brand) {
AmountMath.make(brand, values[i]),
);
}
}
};

test('fee distribution', async t => {
const { brands, moolaIssuer: issuer, moolaMint: runMint } = setup();
Expand Down
2 changes: 1 addition & 1 deletion packages/run-protocol/test/vaultFactory/test-liquidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { deeplyFulfilled } from '@endo/marshal';

import { makeIssuerKit, AssetKind, AmountMath } from '@agoric/ertp';
import buildManualTimer from '@agoric/zoe/tools/manualTimer.js';
import { eventLoopIteration } from '@agoric/zoe/tools/eventLoopIteration.js';
import {
makeRatioFromAmounts,
ceilMultiplyBy,
Expand All @@ -29,7 +30,6 @@ import {
setUpZoeForTest,
setupBootstrap,
installGovernance,
eventLoopIteration,
withAmountUtils,
} from '../supports.js';
import { unsafeMakeBundleCache } from '../bundleTool.js';
Expand Down
12 changes: 2 additions & 10 deletions packages/run-protocol/test/vaultFactory/test-vaultFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { deeplyFulfilled } from '@endo/marshal';

import { AmountMath, AssetKind, makeIssuerKit } from '@agoric/ertp';
import buildManualTimer from '@agoric/zoe/tools/manualTimer.js';
import { eventLoopIteration } from '@agoric/zoe/tools/eventLoopIteration.js';
import {
ceilMultiplyBy,
makeRatio,
Expand Down Expand Up @@ -38,7 +39,6 @@ import {
installGovernance,
setupBootstrap,
setUpZoeForTest,
eventLoopIteration,
withAmountUtils,
produceInstallations,
} from '../supports.js';
Expand Down Expand Up @@ -2725,15 +2725,7 @@ test('manager notifiers', async t => {
totalDebt: { value: DEBT1 },
});
m.addDebt(DEBT1);
const periods = 5n;
// FIXME test result relies on awaiting each tick
// timer.tickN() awaits only the last tick
for (let i = 0; i < periods; i += 1) {
// eslint-disable-next-line no-await-in-loop
await manualTimer.tick();
}
// wait to let interest charging catch up
await eventLoopIteration();
await manualTimer.tickN(5);
const interestAccrued = (await E(vault).getCurrentDebt()).value - DEBT1;
m.addDebt(interestAccrued);
t.is(interestAccrued, 10n);
Expand Down
17 changes: 17 additions & 0 deletions packages/zoe/tools/eventLoopIteration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @ts-check
/* global setImmediate */

import './types.js';
import './internal-types.js';

/**
* A workaround for some issues with fake time in tests.
*
* Lines of test code can depend on async promises outside the test
* resolving before they run. Awaiting this function result ensures
* that all promises that can do resolve.
* Note that this doesn't mean all outstanding promises.
*/
export const eventLoopIteration = async () =>
new Promise(resolve => setImmediate(resolve));
harden(eventLoopIteration);
6 changes: 5 additions & 1 deletion packages/zoe/tools/manualTimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import { E } from '@endo/eventual-send';
import { makeScalarMapStore } from '@agoric/store';
import { assert, details as X } from '@agoric/assert';
import { Nat } from '@agoric/nat';
import { Far } from '@endo/marshal';

import './types.js';
import './internal-types.js';
import { makeNotifierKit } from '@agoric/notifier';
import { makePromiseKit } from '@endo/promise-kit';
import { eventLoopIteration } from './eventLoopIteration.js';

const { details: X } = assert;

/**
* A fake clock that also logs progress.
Expand Down Expand Up @@ -102,6 +104,7 @@ export default function buildManualTimer(log, startValue = 0n, timeStep = 1n) {
for (let i = 0; i < nTimes - 1; i += 1) {
timer.tick(msg);
}
await eventLoopIteration();
// suffices that only the last be awaited
await timer.tick(msg);
},
Expand Down Expand Up @@ -189,3 +192,4 @@ export default function buildManualTimer(log, startValue = 0n, timeStep = 1n) {
});
return timer;
}
harden(buildManualTimer);

0 comments on commit 986ae7f

Please sign in to comment.