|
4 | 4 | isError, makeError,
|
5 | 5 |
|
6 | 6 | AbstractProvider, FallbackProvider, Network,
|
| 7 | + ZeroAddress |
7 | 8 | } from "../index.js";
|
8 | 9 |
|
9 | 10 | import type {
|
@@ -93,3 +94,68 @@ describe("Test Fallback broadcast", function() {
|
93 | 94 | });
|
94 | 95 | });
|
95 | 96 | });
|
| 97 | + |
| 98 | +describe("Test Inflight Quorum", function() { |
| 99 | + // Fires the %%actions%% as providers which will delay before returning, |
| 100 | + // and returns an array of arrays, where each sub-array indicates which |
| 101 | + // providers were inflight at once. |
| 102 | + async function test(actions: Array<{ delay: number, stallTimeout: number, priority: number, weight: number }>, quorum: number): Promise<Array<Array<number>>> { |
| 103 | + const inflights: Array<Array<number>> = [ [ ] ]; |
| 104 | + |
| 105 | + const configs = actions.map(({ delay, stallTimeout, priority, weight }, index) => ({ |
| 106 | + provider: new MockProvider(async (r) => { |
| 107 | + if (r.method === "getBlockNumber") { return 1; } |
| 108 | + if (r.method === "getBalance") { |
| 109 | + // Add this as inflight |
| 110 | + let last = inflights.pop(); |
| 111 | + if (last == null) { throw new Error("no elements"); } |
| 112 | + inflights.push(last); |
| 113 | + last = last.slice(); |
| 114 | + last.push(index); |
| 115 | + inflights.push(last); |
| 116 | + |
| 117 | + // Do the thing |
| 118 | + await stall(delay); |
| 119 | + |
| 120 | + // Remove as inflight |
| 121 | + last = inflights.pop(); |
| 122 | + if (last == null) { throw new Error("no elements"); } |
| 123 | + inflights.push(last); |
| 124 | + last = last.filter((v) => (v !== index)); |
| 125 | + inflights.push(last); |
| 126 | + |
| 127 | + return 0; |
| 128 | + } |
| 129 | + console.log(r); |
| 130 | + throw new Error(`unhandled method: ${ r.method }`); |
| 131 | + }), |
| 132 | + stallTimeout, priority, weight |
| 133 | + })); |
| 134 | + |
| 135 | + const provider = new FallbackProvider(configs, network, { |
| 136 | + cacheTimeout: -1, pollingInterval: 100, |
| 137 | + quorum |
| 138 | + }); |
| 139 | + await provider.getBalance(ZeroAddress); |
| 140 | + |
| 141 | + return inflights; |
| 142 | + } |
| 143 | + |
| 144 | + // See: #4298 |
| 145 | + it("applies weights against inflight requests", async function() { |
| 146 | + this.timeout(2000); |
| 147 | + |
| 148 | + const inflights = await test([ |
| 149 | + { delay: 50, stallTimeout: 1000, priority: 1, weight: 2 }, |
| 150 | + { delay: 50, stallTimeout: 1000, priority: 1, weight: 2 }, |
| 151 | + ], 2); |
| 152 | + |
| 153 | + // Make sure there is never more than 1 inflight provider at once |
| 154 | + for (const running of inflights) { |
| 155 | + assert.ok(running.length <= 1, `too many inflight requests: ${ JSON.stringify(inflights) }`); |
| 156 | + } |
| 157 | + }); |
| 158 | + |
| 159 | + // @TODO: add lots more tests, checking on priority, weight and stall |
| 160 | + // configurations |
| 161 | +}); |
0 commit comments