Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
test: update to match new zoe/tools/manualTimer.js
Browse files Browse the repository at this point in the history
In Agoric/agoric-sdk#5847 , Zoe's
`tools/manualTimer.js` is updated to align with the new SwingSet
TimerService. In this new version, when the ManualTimer is configured
with `eventLoopIteration`, an `await tick()` will properly drain the
promise queue before proceeding. In addition, the TimerService will
fire a `setWakeup(now)` immediately, rather than requiring a `tick()`
first.

This changes the behavior of tests which were not correctly
synchronizing before, especially the timer-based
fakePriceAuthority. Where previously you could set a price sequence of
`[20, 55]` and use a single `await tick()` to get to `time=1` and
`price=20`, in the new version, you use no ticks, and start out with
`time=0` and `price=20`. A single `await tick()` gets you `time=1` and
`price=55`.

This requires changes to the unit tests, in general removing one
`tick()` and decrementing the expected timestamp by one.
  • Loading branch information
warner committed Aug 19, 2022
1 parent b6f1de8 commit 160ba1e
Showing 1 changed file with 23 additions and 32 deletions.
55 changes: 23 additions & 32 deletions api/test/test-fakePriceAuthority.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '@agoric/zoe/tools/prepare-test-env.js';
import test from 'ava';
import { E } from '@agoric/far';
import buildManualTimer from '@agoric/zoe/tools/manualTimer.js';
import { eventLoopIteration } from '@agoric/zoe/tools/eventLoopIteration.js';

// import { makeFakePriceAuthority } from '@agoric/zoe/tools/fakePriceAuthority';
import { makeFakePriceAuthority } from '../priceAuthority/src/fake.js';
Expand All @@ -29,27 +30,25 @@ const makeTestPriceAuthority = ({ brands, priceList, tradeList, timer }) =>
test('priceAuthority quoteAtTime', async t => {
const { moola, bucks, brands } = setup();
const bucksBrand = brands.get('bucks');
const manualTimer = buildManualTimer(t.log, 0n);
const manualTimer = buildManualTimer(t.log, 0n, { eventLoopIteration });
const priceAuthority = await makeTestPriceAuthority({
brands,
priceList: [20n, 55n],
timer: manualTimer,
});

const done = E(priceAuthority)
.quoteAtTime(3n, moola(5n), bucksBrand)
.quoteAtTime(2n, moola(5n), bucksBrand)
.then(async quote => {
t.deepEqual(
moola(5n),
quote.quoteAmount.value[0].amountIn,
'amountIn match',
);
t.deepEqual(bucks(55n * 5n), quote.quoteAmount.value[0].amountOut);
t.is(3n, quote.quoteAmount.value[0].timestamp);
t.is(2n, quote.quoteAmount.value[0].timestamp);
});

await E(manualTimer).tick();
await E(manualTimer).tick();
await E(manualTimer).tick();
await E(manualTimer).tick();
await done;
Expand All @@ -58,34 +57,32 @@ test('priceAuthority quoteAtTime', async t => {
test('priceAuthority quoteGiven', async t => {
const { moola, brands, bucks } = setup();
const bucksBrand = brands.get('bucks');
const manualTimer = buildManualTimer(t.log, 0n);
const manualTimer = buildManualTimer(t.log, 0n, { eventLoopIteration });
const priceAuthority = await makeTestPriceAuthority({
brands,
priceList: [20n, 55n],
timer: manualTimer,
});

await E(manualTimer).tick();
const quote = await E(priceAuthority).quoteGiven(moola(37n), bucksBrand);
const quoteAmount = quote.quoteAmount.value[0];
t.is(1n, quoteAmount.timestamp);
t.is(0n, quoteAmount.timestamp);
t.deepEqual(bucks(37n * 20n), quoteAmount.amountOut);
});

test('priceAuthority quoteWanted', async t => {
const { moola, bucks, brands } = setup();
const moolaBrand = brands.get('moola');
const manualTimer = buildManualTimer(t.log, 0n);
const manualTimer = buildManualTimer(t.log, 0n, { eventLoopIteration });
const priceAuthority = await makeTestPriceAuthority({
brands,
priceList: [20n, 55n],
timer: manualTimer,
});

await E(manualTimer).tick();
const quote = await E(priceAuthority).quoteWanted(moolaBrand, bucks(400n));
const quoteAmount = quote.quoteAmount.value[0];
t.is(1n, quoteAmount.timestamp);
t.is(0n, quoteAmount.timestamp);
t.deepEqual(bucks(400n), quoteAmount.amountOut);
t.deepEqual(moola(20n), quoteAmount.amountIn);
});
Expand All @@ -94,7 +91,7 @@ test('priceAuthority paired quotes', async t => {
const { moola, bucks, brands } = setup();
const moolaBrand = brands.get('moola');
const bucksBrand = brands.get('bucks');
const manualTimer = buildManualTimer(t.log, 0n);
const manualTimer = buildManualTimer(t.log, 0n, { eventLoopIteration });
const priceAuthority = await makeTestPriceAuthority({
brands,
tradeList: [
Expand All @@ -104,24 +101,22 @@ test('priceAuthority paired quotes', async t => {
timer: manualTimer,
});

await E(manualTimer).tick();

const quoteOut = await E(priceAuthority).quoteWanted(moolaBrand, bucks(400n));
const quoteOutAmount = quoteOut.quoteAmount.value[0];
t.is(1n, quoteOutAmount.timestamp);
t.is(0n, quoteOutAmount.timestamp);
t.deepEqual(bucks((20n * 40n) / 2n), quoteOutAmount.amountOut);
t.deepEqual(moola(20n), quoteOutAmount.amountIn);

const quoteIn = await E(priceAuthority).quoteGiven(moola(22n), bucksBrand);
const quoteInAmount = quoteIn.quoteAmount.value[0];
t.is(1n, quoteInAmount.timestamp);
t.is(0n, quoteInAmount.timestamp);
t.deepEqual(bucks(20n * 22n), quoteInAmount.amountOut);
t.deepEqual(moola(22n), quoteInAmount.amountIn);
});

test('priceAuthority quoteWhenGTE', async t => {
const { moola, bucks, brands } = setup();
const manualTimer = buildManualTimer(t.log, 0n);
const manualTimer = buildManualTimer(t.log, 0n, { eventLoopIteration });
const priceAuthority = await makeTestPriceAuthority({
brands,
priceList: [20n, 30n, 25n, 40n],
Expand All @@ -132,22 +127,21 @@ test('priceAuthority quoteWhenGTE', async t => {
.quoteWhenGTE(moola(1n), bucks(40n))
.then(quote => {
const quoteInAmount = quote.quoteAmount.value[0];
t.is(4n, manualTimer.getCurrentTimestamp());
t.is(4n, quoteInAmount.timestamp);
t.is(3n, manualTimer.getCurrentTimestamp());
t.is(3n, quoteInAmount.timestamp);
t.deepEqual(bucks(40n), quoteInAmount.amountOut);
t.deepEqual(moola(1n), quoteInAmount.amountIn);
});

await E(manualTimer).tick();
await E(manualTimer).tick();
await E(manualTimer).tick();
await E(manualTimer).tick();
await expected;
});

test('priceAuthority quoteWhenLT', async t => {
const { moola, bucks, brands } = setup();
const manualTimer = buildManualTimer(t.log, 0n);
const manualTimer = buildManualTimer(t.log, 0n, { eventLoopIteration });
const priceAuthority = await makeTestPriceAuthority({
brands,
priceList: [40n, 30n, 29n],
Expand All @@ -158,21 +152,20 @@ test('priceAuthority quoteWhenLT', async t => {
.quoteWhenLT(moola(1n), bucks(30n))
.then(quote => {
const quoteInAmount = quote.quoteAmount.value[0];
t.is(3n, manualTimer.getCurrentTimestamp());
t.is(3n, quoteInAmount.timestamp);
t.is(2n, manualTimer.getCurrentTimestamp());
t.is(2n, quoteInAmount.timestamp);
t.deepEqual(bucks(29n), quoteInAmount.amountOut);
t.deepEqual(moola(1n), quoteInAmount.amountIn);
});

await E(manualTimer).tick();
await E(manualTimer).tick();
await E(manualTimer).tick();
await expected;
});

test('priceAuthority quoteWhenGT', async t => {
const { moola, bucks, brands } = setup();
const manualTimer = buildManualTimer(t.log, 0n);
const manualTimer = buildManualTimer(t.log, 0n, { eventLoopIteration });
const priceAuthority = await makeTestPriceAuthority({
brands,
priceList: [40n, 30n, 41n],
Expand All @@ -183,21 +176,20 @@ test('priceAuthority quoteWhenGT', async t => {
.quoteWhenGT(moola(1n), bucks(40n))
.then(quote => {
const quoteInAmount = quote.quoteAmount.value[0];
t.is(3n, manualTimer.getCurrentTimestamp());
t.is(3n, quoteInAmount.timestamp);
t.is(2n, manualTimer.getCurrentTimestamp());
t.is(2n, quoteInAmount.timestamp);
t.deepEqual(bucks(41n), quoteInAmount.amountOut);
t.deepEqual(moola(1n), quoteInAmount.amountIn);
});

await E(manualTimer).tick();
await E(manualTimer).tick();
await E(manualTimer).tick();
await expected;
});

test('priceAuthority quoteWhenLTE', async t => {
const { moola, bucks, brands } = setup();
const manualTimer = buildManualTimer(t.log, 0n);
const manualTimer = buildManualTimer(t.log, 0n, { eventLoopIteration });
const priceAuthority = await makeTestPriceAuthority({
brands,
priceList: [40n, 26n, 50n, 25n],
Expand All @@ -208,15 +200,14 @@ test('priceAuthority quoteWhenLTE', async t => {
.quoteWhenLTE(moola(1n), bucks(25n))
.then(quote => {
const quoteInAmount = quote.quoteAmount.value[0];
t.is(4n, quoteInAmount.timestamp);
t.is(4n, manualTimer.getCurrentTimestamp());
t.is(3n, quoteInAmount.timestamp);
t.is(3n, manualTimer.getCurrentTimestamp());
t.deepEqual(bucks(25n), quoteInAmount.amountOut);
t.deepEqual(moola(1n), quoteInAmount.amountIn);
});

await E(manualTimer).tick();
await E(manualTimer).tick();
await E(manualTimer).tick();
await E(manualTimer).tick();
await expected;
});

0 comments on commit 160ba1e

Please sign in to comment.