Skip to content

Commit

Permalink
feat(transform-metering): add balance-getters to refillFacet
Browse files Browse the repository at this point in the history
  • Loading branch information
warner committed Jun 25, 2020
1 parent 0404bb0 commit 8200188
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/transform-metering/src/meter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const makeCounter = initBalance => {
};
counter.reset = (newBalance = undefined) =>
(balance = newBalance === undefined ? initBalance : newBalance);
counter.getBalance = () => balance;
return counter;
};

Expand Down Expand Up @@ -202,6 +203,9 @@ export function makeMeter(budgets = {}) {
stack: makeResetter(stackCounter),
compute: makeResetter(computeCounter),
combined: makeResetter(combinedCounter),
getAllocateBalance: allocateCounter.getBalance,
getComputeBalance: computeCounter.getBalance,
getCombinedBalance: combinedCounter.getBalance,
};

// Create the internal meter object.
Expand Down
15 changes: 15 additions & 0 deletions packages/transform-metering/test/test-meter.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,18 @@ test('meter running', async t => {
t.end();
}
});

test('getBalance', async t => {
try {
const { meter, refillFacet } = makeMeter({ budgetCompute: 10 });
t.equal(refillFacet.getComputeBalance(), 10);
meter[c.METER_COMPUTE](3);
t.equal(refillFacet.getComputeBalance(), 7);
t.equal(refillFacet.getAllocateBalance(), c.DEFAULT_COMBINED_METER);
t.equal(refillFacet.getCombinedBalance(), c.DEFAULT_COMBINED_METER);
} catch (e) {
t.isNot(e, e, 'unexpected exception');
} finally {
t.end();
}
});

0 comments on commit 8200188

Please sign in to comment.