postchain-client
Version:
Client library for accessing a Postchain node through REST.
84 lines • 5.2 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { FailoverStrategy, Method } from "../../src/blockchainClient/enums";
import { createNodeManager } from "../../src/blockchainClient/nodeManager";
import { setStatusPolling } from "../../src/blockchainClient/utils";
import { LOCAL_POOL, mockNodeUrls } from "../common/mocks";
import * as failoverStrategies from "../../src/blockchainClient/failoverStrategies";
import dotenv from "dotenv";
import path from "path";
import { requestWithFailoverStrategy } from "../../src/blockchainClient/requestWithFailoverStrategy";
import { MERKLE_HASH_VERSIONS } from "../../src/utils/constants";
dotenv.config({ path: path.resolve(__dirname, ".env") });
describe("requestWithFailoverStrategy", () => {
const config = {
merkleHashVersion: MERKLE_HASH_VERSIONS.ONE,
nodeManager: createNodeManager({
nodeUrls: [LOCAL_POOL, LOCAL_POOL],
}),
endpointPool: [
{ url: LOCAL_POOL, whenAvailable: 0 },
{ url: LOCAL_POOL, whenAvailable: 0 },
],
blockchainRid: process.env.DAPP_BLOCKCHAIN_RID,
dappStatusPolling: setStatusPolling({ interval: 100, count: 1 }),
clusterAnchoringStatusPolling: setStatusPolling(),
systemAnchoringStatusPolling: setStatusPolling(),
failoverStrategy: null,
attemptsPerEndpoint: 3,
attemptInterval: 100,
unreachableDuration: 100,
directoryChainRid: process.env.DIRECTORY_BLOCKCHAIN_RID,
};
it("overrides QueryMajority failover strategy if forceSingleEndpoint is set to true", () => __awaiter(void 0, void 0, void 0, function* () {
const _config = Object.assign(Object.assign({}, config), { failoverStrategy: FailoverStrategy.QueryMajority });
const singleEndpointSpy = jest.spyOn(failoverStrategies, "singleEndpoint");
const queryMajoritySpy = jest.spyOn(failoverStrategies, "queryMajority");
expect(singleEndpointSpy).toHaveBeenCalledTimes(0);
yield requestWithFailoverStrategy(Method.GET, "/path", _config, undefined, true);
expect(queryMajoritySpy).toHaveBeenCalledTimes(0);
expect(singleEndpointSpy).toHaveBeenCalledTimes(1);
}));
it("tries all nodes when bft majority cannot be assured", () => __awaiter(void 0, void 0, void 0, function* () {
const abortOnErrorSpy = jest.spyOn(failoverStrategies, "abortOnError");
const currentDate = new Date();
const _config = Object.assign(Object.assign({}, config), { nodeUrls: mockNodeUrls, endpointPool: [
{ url: mockNodeUrls[0], whenAvailable: 0 },
{ url: mockNodeUrls[1], whenAvailable: 0 },
{ url: mockNodeUrls[2], whenAvailable: 0 },
{ url: mockNodeUrls[3], whenAvailable: currentDate.getTime() + 10000 },
{ url: mockNodeUrls[4], whenAvailable: currentDate.getTime() + 10000 },
], failoverStrategy: FailoverStrategy.AbortOnError, dappStatusPolling: setStatusPolling({ interval: 100, count: 10 }), attemptsPerEndpoint: 1 });
expect(abortOnErrorSpy).toHaveBeenCalledTimes(0);
yield requestWithFailoverStrategy(Method.GET, "/path", _config);
expect(abortOnErrorSpy).toHaveBeenCalledTimes(1);
}));
it("does not call an unreachable node", () => __awaiter(void 0, void 0, void 0, function* () {
const abortOnErrorSpy = jest.spyOn(failoverStrategies, "abortOnError");
const currentDate = new Date();
const _config = Object.assign(Object.assign({}, config), { nodeManager: createNodeManager({
nodeUrls: [mockNodeUrls[0], mockNodeUrls[1]],
}), endpointPool: [
{ url: mockNodeUrls[0], whenAvailable: currentDate.getTime() + 10000 },
{ url: mockNodeUrls[1], whenAvailable: 0 },
], failoverStrategy: FailoverStrategy.AbortOnError, dappStatusPolling: setStatusPolling({ interval: 100, count: 10 }), attemptsPerEndpoint: 1 });
expect(abortOnErrorSpy).toHaveBeenCalledTimes(0);
yield requestWithFailoverStrategy(Method.GET, "/path", _config);
expect(abortOnErrorSpy).toHaveBeenCalledTimes(1);
expect(abortOnErrorSpy).toHaveBeenCalledWith(expect.objectContaining({
config: expect.objectContaining({
endpointPool: expect.arrayContaining([
expect.objectContaining({ url: mockNodeUrls[1], whenAvailable: 0 }),
]),
}),
}));
}));
});
//# sourceMappingURL=requestWithFailoverStrategy.test.js.map