Skip to content

Commit

Permalink
fix: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptodev-2s committed Jul 22, 2024
1 parent 3243082 commit 87543b0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 81 deletions.
24 changes: 12 additions & 12 deletions src/block-ref.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
stubProviderRequests,
buildStubForBlockNumberRequest,
buildStubForGenericRequest,
buildFinalMiddlewareWithDefaultResponse,
buildFinalMiddlewareWithDefaultResult,
buildMockParamsWithoutBlockParamAt,
expectProviderRequestNotToHaveBeenMade,
} from '../test/util/helpers';
Expand Down Expand Up @@ -106,7 +106,7 @@ describe('createBlockRefMiddleware', () => {
'0x100',
),
},
response: async () => 'something',
result: async () => 'something',
}),
]);

Expand All @@ -123,7 +123,7 @@ describe('createBlockRefMiddleware', () => {
});

it('does not proceed to the next middleware after making a request through the provider', async () => {
const finalMiddleware = buildFinalMiddlewareWithDefaultResponse();
const finalMiddleware = buildFinalMiddlewareWithDefaultResult();

await withTestSetup(
{
Expand Down Expand Up @@ -157,7 +157,7 @@ describe('createBlockRefMiddleware', () => {
'0x100',
),
},
response: async () => 'something',
result: async () => 'something',
}),
]);

Expand Down Expand Up @@ -199,7 +199,7 @@ describe('createBlockRefMiddleware', () => {
'0x100',
),
},
response: async () => 'something',
result: async () => 'something',
}),
]);

Expand All @@ -216,7 +216,7 @@ describe('createBlockRefMiddleware', () => {
});

it('does not proceed to the next middleware after making a request through the provider', async () => {
const finalMiddleware = buildFinalMiddlewareWithDefaultResponse();
const finalMiddleware = buildFinalMiddlewareWithDefaultResult();

await withTestSetup(
{
Expand Down Expand Up @@ -247,7 +247,7 @@ describe('createBlockRefMiddleware', () => {
'0x100',
),
},
response: async () => 'something',
result: async () => 'something',
}),
]);

Expand All @@ -263,7 +263,7 @@ describe('createBlockRefMiddleware', () => {
'if the block param is something other than "latest", like %o',
(blockParam) => {
it('does not make a direct request through the provider', async () => {
const finalMiddleware = buildFinalMiddlewareWithDefaultResponse();
const finalMiddleware = buildFinalMiddlewareWithDefaultResult();

await withTestSetup(
{
Expand Down Expand Up @@ -299,7 +299,7 @@ describe('createBlockRefMiddleware', () => {
});

it('proceeds to the next middleware', async () => {
const finalMiddleware = buildFinalMiddlewareWithDefaultResponse();
const finalMiddleware = buildFinalMiddlewareWithDefaultResult();

await withTestSetup(
{
Expand Down Expand Up @@ -350,7 +350,7 @@ describe('createBlockRefMiddleware', () => {

describe('when the RPC method does not take a block parameter', () => {
it('does not make a direct request through the provider', async () => {
const finalMiddleware = buildFinalMiddlewareWithDefaultResponse();
const finalMiddleware = buildFinalMiddlewareWithDefaultResult();

await withTestSetup(
{
Expand Down Expand Up @@ -383,7 +383,7 @@ describe('createBlockRefMiddleware', () => {
});

it('proceeds to the next middleware', async () => {
const finalMiddleware = buildFinalMiddlewareWithDefaultResponse();
const finalMiddleware = buildFinalMiddlewareWithDefaultResult();

await withTestSetup(
{
Expand Down Expand Up @@ -449,7 +449,7 @@ async function withTestSetup<T>(

const {
middlewareUnderTest,
otherMiddleware = [buildFinalMiddlewareWithDefaultResponse()],
otherMiddleware = [buildFinalMiddlewareWithDefaultResult()],
} = configureMiddleware({ engine, provider, blockTracker });

for (const middleware of [middlewareUnderTest, ...otherMiddleware]) {
Expand Down
2 changes: 1 addition & 1 deletion src/block-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function createBlockRefMiddleware({

// perform child request
log('Performing another request %o', childRequest);
// copy child response onto original response
// copy child result onto original response
try {
const childResult = await provider.request<JsonRpcParams, Block>(
childRequest,
Expand Down
19 changes: 7 additions & 12 deletions src/providerAsMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';
import {
createAsyncMiddleware,
type JsonRpcMiddleware,
} from '@metamask/json-rpc-engine';
import type {
Json,
JsonRpcParams,
Expand All @@ -9,17 +12,9 @@ import type {
export function providerAsMiddleware(
provider: SafeEventEmitterProvider,
): JsonRpcMiddleware<JsonRpcParams, Json> {
return (req, res, _next, end) => {
provider
.request(req)
.then((result) => {
res.result = result;
end();
})
.catch((error) => {
end(error);
});
};
return createAsyncMiddleware(async (req, res) => {
res.result = await provider.request(req);
});
}

export function ethersProviderAsMiddleware(
Expand Down
40 changes: 20 additions & 20 deletions src/retryOnEmpty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { providerFromEngine } from '@metamask/eth-json-rpc-provider';
import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import { errorCodes, JsonRpcError, rpcErrors } from '@metamask/rpc-errors';
import { errorCodes, providerErrors, rpcErrors } from '@metamask/rpc-errors';
import type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';

import { createRetryOnEmptyMiddleware } from '.';
import type { ProviderRequestStub } from '../test/util/helpers';
import {
buildFinalMiddlewareWithDefaultResponse,
buildFinalMiddlewareWithDefaultResult,
buildMockParamsWithBlockParamAt,
buildMockParamsWithoutBlockParamAt,
buildSimpleFinalMiddleware,
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('createRetryOnEmptyMiddleware', () => {
stubRequestThatFailsThenFinallySucceeds({
request,
numberOfTimesToFail: 9,
successfulResponse: async () => 'something',
successfulResult: async () => 'something',
}),
]);

Expand Down Expand Up @@ -195,8 +195,8 @@ describe('createRetryOnEmptyMiddleware', () => {
buildStubForBlockNumberRequest(blockNumber),
stubGenericRequest({
request,
response: () => {
throw new JsonRpcError(-1, 'oops');
result: () => {
throw providerErrors.custom({ code: -1, message: 'oops' });
},
remainAfterUse: true,
}),
Expand Down Expand Up @@ -252,7 +252,7 @@ describe('createRetryOnEmptyMiddleware', () => {
buildStubForBlockNumberRequest(blockNumber),
stubGenericRequest({
request,
response: async () => 'success',
result: async () => 'success',
}),
]);

Expand Down Expand Up @@ -636,7 +636,7 @@ describe('createRetryOnEmptyMiddleware', () => {
buildStubForBlockNumberRequest(),
{
request,
response: () => {
result: () => {
throw rpcErrors.invalidInput('execution reverted');
},
},
Expand Down Expand Up @@ -680,7 +680,7 @@ async function withTestSetup<T>(

const {
middlewareUnderTest,
otherMiddleware = [buildFinalMiddlewareWithDefaultResponse()],
otherMiddleware = [buildFinalMiddlewareWithDefaultResult()],
} = configureMiddleware({ engine, provider, blockTracker });

for (const middleware of [middlewareUnderTest, ...otherMiddleware]) {
Expand All @@ -694,9 +694,9 @@ async function withTestSetup<T>(
}

/**
* Builds a canned response for a request made to `provider.request`. Intended
* Builds a canned result for a request made to `provider.request`. Intended
* to be used in conjunction with `stubProviderRequests`. Although not strictly
* necessary, it helps to assign a proper type to a request/response pair.
* necessary, it helps to assign a proper type to a request/result pair.
*
* @param requestStub - The request/response pair.
* @returns The request/response pair, properly typed.
Expand All @@ -708,45 +708,45 @@ function stubGenericRequest<T extends JsonRpcParams, U extends Json>(
}

/**
* Builds a canned response for a request made to `provider.request` which
* Builds a canned result for a request made to `provider.request` which
* will error for the first N instances and then succeed on the last instance.
* Intended to be used in conjunction with `stubProviderRequests`.
*
* @param request - The request matcher for the stub.
* @param numberOfTimesToFail - The number of times the request is expected to
* be called until it returns a successful response.
* @param successfulResponse - The response that `provider.request` will
* be called until it returns a successful result.
* @param successfulResult - The result that `provider.request` will
* return when called past `numberOfTimesToFail`.
* @returns The request/response pair, properly typed.
* @returns The request/result pair, properly typed.
*/
function stubRequestThatFailsThenFinallySucceeds<
T extends JsonRpcParams,
U extends Json,
>({
request,
numberOfTimesToFail,
successfulResponse,
successfulResult,
}: {
request: ProviderRequestStub<T, U>['request'];
numberOfTimesToFail: number;
successfulResponse: ProviderRequestStub<T, U>['response'];
successfulResult: ProviderRequestStub<T, U>['result'];
}): ProviderRequestStub<T, U> {
return stubGenericRequest({
request,
response: async (callNumber) => {
result: async (callNumber) => {
if (callNumber <= numberOfTimesToFail) {
throw new JsonRpcError(-1, 'oops');
throw providerErrors.custom({ code: -1, message: 'oops' });
}

return await successfulResponse(callNumber);
return await successfulResult(callNumber);
},
remainAfterUse: true,
});
}

/**
* The `retryOnEmpty` middleware, as its name implies, uses the provider to make
* the given request, retrying said request up to 10 times if the response is
* the given request, retrying said request up to 10 times if the result is
* empty before failing. Upon retrying, it will wait a brief time using
* `setTimeout`. Because we are using Jest's fake timers, we have to manually
* trigger the callback passed to `setTimeout` atfter it is called. The problem
Expand Down
6 changes: 3 additions & 3 deletions src/retryOnEmpty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function createRetryOnEmptyMiddleware({
const childRequest = klona(req);
// attempt child request until non-empty response is received
try {
const childResponse = await retry(10, async () => {
const childResult = await retry(10, async () => {
log('Performing request %o', childRequest);
const result = await provider.request<JsonRpcParams, Block>(
childRequest,
Expand All @@ -115,8 +115,8 @@ export function createRetryOnEmptyMiddleware({
}
return result;
});
log('Copying result %o', childResponse.result);
res.result = childResponse;
log('Copying result %o', childResult);
res.result = childResult;
res.error = undefined;
} catch (error: any) {
log('Copying error %o', error);
Expand Down
Loading

0 comments on commit 87543b0

Please sign in to comment.