Skip to content

Commit

Permalink
fix: Added test and documentation to the server request timeout setting.
Browse files Browse the repository at this point in the history
Signed-off-by: ebadiere <ebadiere@gmail.com>
  • Loading branch information
ebadiere committed May 21, 2024
1 parent 9eda6bf commit c775567
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import app from './server';
async function main() {
const server = await app.listen({ port: process.env.SERVER_PORT || 7546 });

// set request timeout to ensure sockets are closed after specified time
const requestTimeoutMs = parseInt(process.env.SERVER_REQUEST_TIMEOUT_MS!) || 30000;
// set request timeout to ensure sockets are closed after specified time of inactivity
const requestTimeoutMs = parseInt(process.env.SERVER_REQUEST_TIMEOUT_MS ?? '') ?? 30000;
server.setTimeout(requestTimeoutMs);
}

Expand Down
81 changes: 80 additions & 1 deletion packages/server/tests/acceptance/rpc_batch3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Hedera JSON RPC Relay
*
* Copyright (C) 2023 Hedera Hashgraph, LLC
* Copyright (C) 2023-2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -27,6 +27,8 @@ import chai, { expect } from 'chai';
import chaiExclude from 'chai-exclude';
import Constants from '@hashgraph/json-rpc-relay/dist/lib/constants';
import { ContractId } from '@hashgraph/sdk';
import app from '../../src/server';
import http from 'http';

// Assertions and constants from local resources
import Assertions from '../helpers/assertions';
Expand All @@ -49,9 +51,67 @@ import EstimateGasContract from '../contracts/EstimateGasContract.json';
// Helper functions/constants from local resources
import { EthImpl } from '../../../../packages/relay/src/lib/eth';
import { predefined } from '../../../../packages/relay';
import exp from 'constants';
import e from 'express';

chai.use(chaiExclude);

const sendJsonRpcRequestWithDelay = (
host: string,
port: number,
method: string,
params: any[],
delayMs: number,
): Promise<any> => {
const requestData = JSON.stringify({
jsonrpc: '2.0',
method: method,
params: params,
id: 1,
});

const options = {
hostname: host,
port: port,
path: '/',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(requestData),
},
timeout: delayMs,
};

return new Promise((resolve, reject) => {
const req = http.request(options, (res) => {
let data = '';

res.on('data', (chunk) => {
data += chunk;
});

res.on('end', () => {
resolve(JSON.parse(data));
});
});

req.on('timeout', () => {
req.destroy();
reject(new Error(`Request timed out after ${delayMs}ms`));
});

req.on('error', (err) => {
reject(err);
});

// Introduce a delay with inactivity, before sending the request
setTimeout(() => {
req.write(requestData);
req.end();
}, delayMs);
});
};

describe('@api-batch-3 RPC Server Acceptance Tests', function () {
this.timeout(240 * 1000); // 240 seconds

Expand Down Expand Up @@ -2021,4 +2081,23 @@ describe('@api-batch-3 RPC Server Acceptance Tests', function () {
}
});
});

describe.only('Koa Server Timeout', () => {
it.only('should timeout a request after the specified time', async () => {
const requestTimeoutMs: number = parseInt(process.env.SERVER_REQUEST_TIMEOUT_MS || '3000');

const host = 'localhost';
const port = parseInt(process.env.SERVER_PORT || '7546');
const method = 'eth_blockNumber';
const params: any[] = [];

try {
await sendJsonRpcRequestWithDelay(host, port, method, params, requestTimeoutMs + 1000);
throw new Error('Request did not timeout as expected'); // Force the test to fail if the request does not time out
} catch (err) {
expect(err.code).to.equal('ECONNRESET');
expect(err.message).to.equal('socket hang up');
}
});
});
});
3 changes: 2 additions & 1 deletion packages/server/tests/localAcceptance.env
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ BATCH_REQUESTS_ENABLED=true
TEST_GAS_PRICE_DEVIATION=0.2
WS_NEW_HEADS_ENABLED=false
INITIAL_BALANCE='5000000000'
LIMIT_DURATION=90000
LIMIT_DURATION=90000,
SERVER_REQUEST_TIMEOUT_MS=3000
1 change: 1 addition & 0 deletions packages/server/tests/previewnetAcceptance.env
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ FILTER_API_ENABLED=true
DEBUG_API_ENABLED=true
TEST_GAS_PRICE_DEVIATION=0.2
WS_NEW_HEADS_ENABLED=true
SERVER_REQUEST_TIMEOUT_MS=3000

1 change: 1 addition & 0 deletions packages/server/tests/testnetAcceptance.env
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ SUBSCRIPTIONS_ENABLED=true
FILTER_API_ENABLED=true
DEBUG_API_ENABLED=true
TEST_GAS_PRICE_DEVIATION=0.2
SERVER_REQUEST_TIMEOUT_MS=3000

0 comments on commit c775567

Please sign in to comment.