Skip to content

Commit 79d012f

Browse files
committed
Add unit test for retries
Signed-off-by: Matthew Whitehead <matthew1001@gmail.com>
1 parent a1ddb32 commit 79d012f

2 files changed

Lines changed: 63 additions & 4 deletions

File tree

src/tokens/tokens.service.spec.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ describe('TokensService', () => {
196196
);
197197
};
198198

199+
const mockECONNErrors = (count: number) => {
200+
for (let i = 0; i < count; i++) {
201+
http.post.mockImplementationOnce(() => {
202+
throw new Error('connect ECONNREFUSED 10.1.2.3');
203+
});
204+
}
205+
};
206+
199207
beforeEach(async () => {
200208
http = {
201209
get: jest.fn(),
@@ -234,8 +242,8 @@ describe('TokensService', () => {
234242

235243
let blockchainRetryCfg: RetryConfiguration = {
236244
retryBackOffFactor: 2,
237-
retryBackOffLimit: 10000,
238-
retryBackOffInitial: 100,
245+
retryBackOffLimit: 500,
246+
retryBackOffInitial: 50,
239247
retryCondition: '.*ECONN.*',
240248
retriesMax: 15,
241249
};
@@ -1050,6 +1058,49 @@ describe('TokensService', () => {
10501058
expect(http.post).toHaveBeenCalledWith(BASE_URL, mockEthConnectRequest, { headers });
10511059
});
10521060

1061+
it('should mint ERC721WithData token with correct abi, custom uri, and inputs after 6 ECONNREFUSED retries', async () => {
1062+
const ctx = newContext();
1063+
const headers = {
1064+
'x-firefly-request-id': ctx.requestId,
1065+
};
1066+
1067+
const request: TokenMint = {
1068+
tokenIndex: '721',
1069+
signer: IDENTITY,
1070+
poolLocator: ERC721_WITH_DATA_V1_POOL_ID,
1071+
to: '0x123',
1072+
uri: 'ipfs://CID',
1073+
};
1074+
1075+
const response: EthConnectAsyncResponse = {
1076+
id: 'responseId',
1077+
sent: true,
1078+
};
1079+
1080+
const mockEthConnectRequest: EthConnectMsgRequest = {
1081+
headers: {
1082+
type: 'SendTransaction',
1083+
},
1084+
from: IDENTITY,
1085+
to: CONTRACT_ADDRESS,
1086+
method: ERC721WithDataV1ABI.abi.find(abi => abi.name === MINT_WITH_URI) as IAbiMethod,
1087+
params: ['0x123', '721', '0x00', 'ipfs://CID'],
1088+
};
1089+
1090+
http.post.mockReturnValueOnce(
1091+
new FakeObservable(<EthConnectReturn>{
1092+
output: true,
1093+
}),
1094+
);
1095+
mockECONNErrors(6);
1096+
http.post.mockReturnValueOnce(new FakeObservable(response));
1097+
1098+
await service.mint(ctx, request);
1099+
1100+
expect(http.post).toHaveBeenCalledWith(BASE_URL, mockEthConnectRequest, { headers });
1101+
expect(http.post).toHaveBeenCalledTimes(8); // Expect initial submit OK, 6 ECONN errors, final call OK = 8 POSTs
1102+
});
1103+
10531104
it('should mint ERC721WithData token with correct abi, custom uri, auto-indexing, and inputs', async () => {
10541105
const ctx = newContext();
10551106
const headers = {

test/app.e2e-context.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { EventStreamService } from '../src/event-stream/event-stream.service';
1212
import { EventStreamProxyGateway } from '../src/eventstream-proxy/eventstream-proxy.gateway';
1313
import { TokensService } from '../src/tokens/tokens.service';
1414
import { requestIDMiddleware } from '../src/request-context/request-id.middleware';
15-
import { BlockchainConnectorService } from '../src/tokens/blockchain.service';
15+
import { BlockchainConnectorService, RetryConfiguration } from '../src/tokens/blockchain.service';
1616

1717
export const BASE_URL = 'http://eth';
1818
export const INSTANCE_PATH = '/tokens';
@@ -69,11 +69,19 @@ export class TestContext {
6969
this.app.use(requestIDMiddleware);
7070
await this.app.init();
7171

72+
let blockchainRetryCfg: RetryConfiguration = {
73+
retryBackOffFactor: 2,
74+
retryBackOffLimit: 500,
75+
retryBackOffInitial: 50,
76+
retryCondition: '.*ECONN.*',
77+
retriesMax: 15,
78+
};
79+
7280
this.app.get(EventStreamProxyGateway).configure('url', TOPIC);
7381
this.app.get(TokensService).configure(BASE_URL, TOPIC, '');
7482
this.app
7583
.get(BlockchainConnectorService)
76-
.configure(BASE_URL, '', '', '', [], 2, 1000, 250, '.*ECONN.*', 15);
84+
.configure(BASE_URL, '', '', '', [], blockchainRetryCfg);
7785

7886
(this.app.getHttpServer() as Server).listen();
7987
this.server = request(this.app.getHttpServer());

0 commit comments

Comments
 (0)