From d9556813fcded7b38e53be09b74a3e0918a4052b Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Mon, 15 Aug 2022 16:40:06 -0700 Subject: [PATCH] test: fix test-vaultfactory.js for new timer behavior The test-vaultFactory.js "price falls precipitously" test exercises the asset price changing in four steps: * initial conditions: t=0, price=2200 * tick(): t=1, price=19180 * tick(): t=2, price=1650 * tick(): t=3, price=150 A loan is taken out at t=0. The drop to price=1650 is not quite enough to trigger liquidation. The drop to price=150 does cause liquidation, moreover the price is so low that it falls underwater entirely, so all of the collateral is sold, the client gets nothing back, and the vault manager must tap the reserves to avoid insolvency. Previously, this test used *four* calls to `tick()`, asserting that liquidation did not happen after any of the first three. It appears that this only passed because the `await tick()` was unable to completely wait for all triggered activity to complete (it merely waited on the `wake()` result promise, and did not do a full `setImmediate` / `eventLoopIteration`). When I inserted `await eventLoopIteration()` calls after `tick()` in the original version, the test failed, as liquidation was happening (and completing) after the *third* `tick()`. Now that our `manualTimer.tick()` can be configured to completely flush the promise queue, I'm removing the extra `tick()` call, and the "liquidation has not happened" assertion that was being made too early. --- .../test/vaultFactory/test-vaultFactory.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/inter-protocol/test/vaultFactory/test-vaultFactory.js b/packages/inter-protocol/test/vaultFactory/test-vaultFactory.js index 2d062175959e..e0cf77a32bdf 100644 --- a/packages/inter-protocol/test/vaultFactory/test-vaultFactory.js +++ b/packages/inter-protocol/test/vaultFactory/test-vaultFactory.js @@ -714,6 +714,8 @@ test('price falls precipitously', async t => { undefined, 1500n, ); + // we start with time=0, price=2200 + const { vaultFactory, lender } = services.vaultFactory; const { reserveCreatorFacet } = services.reserveFacets; @@ -782,16 +784,14 @@ test('price falls precipitously', async t => { // @ts-expect-error type confusion const m = await subscriptionTracker(t, metricsSub); await m.assertInitial(reserveInitialState(run.makeEmpty())); - await manualTimer.tick(); - await assertDebtIs(debtAmount.value); - - await manualTimer.tick(); + await manualTimer.tick(); // t 0->1, p 2200->19180 await assertDebtIs(debtAmount.value); - await manualTimer.tick(); + await manualTimer.tick(); // t 1->2, p 19180->1650 await assertDebtIs(debtAmount.value); - await manualTimer.tick(); + await manualTimer.tick(); // t 2->3, p 1650->150, liquidates + await eventLoopIteration(); // shortfall 103n covered by the reserve t.deepEqual(