postchain-client
Version:
Client library for accessing a Postchain node through REST.
196 lines • 9.16 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 { createNodeManager } from "../../src/blockchainClient/nodeManager";
import { mockNodeUrls } from "../common/mocks";
describe("NodeManager", () => {
const nodeUrls = ["http://localhost:3000", "http://localhost2:5000"];
beforeEach(() => {
jest.clearAllMocks();
});
describe("createNodeManager", () => {
it('should return a NodeManager object when "createNodeManager" is given correct params', () => {
const nodeManager = createNodeManager({
nodeUrls: ["http://localhost:3000"],
useStickyNode: false,
unavailableDuration: 5000,
});
expect(nodeManager).toHaveProperty("nodes");
expect(nodeManager).toHaveProperty("stickedNode");
expect(nodeManager).toHaveProperty("getAvailableNodes");
expect(nodeManager).toHaveProperty("setStickyNode");
expect(nodeManager).toHaveProperty("getNode");
expect(nodeManager).toHaveProperty("makeNodeUnavailable");
});
it('should set "stickedNode" to null when "useStickyNode" is set to false', () => {
const nodeManager = createNodeManager({
nodeUrls: ["http://localhost:3000"],
useStickyNode: false,
unavailableDuration: 5000,
});
expect(nodeManager.stickedNode).toBe(null);
});
});
describe("getAvailableNodes", () => {
it("should return an array of available nodes without unavailable nodes", () => {
const nodeManager = createNodeManager({
nodeUrls,
useStickyNode: false,
unavailableDuration: 5000,
});
const availableNodes = nodeManager.getAvailableNodes();
expect(availableNodes).toHaveLength(2);
expect(availableNodes.sort((a, b) => a.url.localeCompare(b.url))).toEqual([
{ url: nodeUrls[0], whenAvailable: 0, isAvailable: true },
{ url: nodeUrls[1], whenAvailable: 0, isAvailable: true },
]);
});
it("should return nodes in a shuffled order if sticky node is disabled", () => {
const nodeManager = createNodeManager({
nodeUrls: mockNodeUrls,
useStickyNode: false,
unavailableDuration: 5000,
});
const availableNodesSet = [
nodeManager.getAvailableNodes(),
nodeManager.getAvailableNodes(),
nodeManager.getAvailableNodes(),
];
expect(availableNodesSet.every(set => set === availableNodesSet[0])).toBe(false);
});
it("should return a shuffled array where the sticky node is first if enabled", () => {
const nodeManager = createNodeManager({
nodeUrls: mockNodeUrls,
useStickyNode: true,
unavailableDuration: 5000,
});
const availableNodesSet = [
nodeManager.getAvailableNodes(),
nodeManager.getAvailableNodes(),
nodeManager.getAvailableNodes(),
];
expect(availableNodesSet.every(set => set === availableNodesSet[0])).toBe(false);
const stickyNode = nodeManager.nodes[0];
nodeManager.setStickyNode(stickyNode);
const availableNodesSetWithStickyNode = [
nodeManager.getAvailableNodes(),
nodeManager.getAvailableNodes(),
nodeManager.getAvailableNodes(),
];
expect(availableNodesSetWithStickyNode.every(set => set[0] === stickyNode)).toBe(true);
});
});
describe("setStickyNode", () => {
it("should update the sticky node", () => {
const nodeManager = createNodeManager({
nodeUrls,
useStickyNode: true,
unavailableDuration: 5000,
});
expect(nodeManager.stickedNode).toBe(null);
nodeManager.setStickyNode(nodeManager.nodes[0]);
expect(nodeManager.stickedNode).toBe(nodeManager.nodes[0]);
});
});
describe("getNode", () => {
it("should return null when no nodes are available", () => {
const nodeManager = createNodeManager({
nodeUrls,
useStickyNode: false,
unavailableDuration: 5000,
});
nodeManager.getAvailableNodes = jest.fn().mockReturnValue([]);
expect(nodeManager.getNode()).toBe(null);
});
it("should return a random node when sticky node is disabled", () => {
const nodeManager = createNodeManager({
nodeUrls,
useStickyNode: false,
unavailableDuration: 5000,
});
expect(nodeManager.getNode()).not.toBe(null);
});
it("should return a random node and set it as the sticky node if enabled", () => {
const nodeManager = createNodeManager({
nodeUrls,
useStickyNode: true,
unavailableDuration: 5000,
});
expect(nodeManager.stickedNode).toBe(null);
const randomNode = nodeManager.getNode();
expect(randomNode).not.toBe(null);
expect(nodeManager.stickedNode).toBe(randomNode);
});
it("should return the sticky node if it's available", () => {
const nodeManager = createNodeManager({
nodeUrls,
useStickyNode: true,
unavailableDuration: 5000,
});
const stickyNode = nodeManager.nodes[0];
nodeManager.setStickyNode(stickyNode);
expect(nodeManager.getNode()).toBe(stickyNode);
});
});
describe("makeAllNodesAvailable", () => {
it("should set all nodes to available", () => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
const nodeUrls = [
"http://localhost1:1000",
"http://localhost2:2000",
"http://localhost3:3000",
"http://localhost4:4000",
"http://localhost5:5000",
];
const nodeManager = createNodeManager({
nodeUrls: nodeUrls,
useStickyNode: true,
unavailableDuration: 5000,
});
expect(nodeManager.getAvailableNodes()).toHaveLength(5);
nodeManager.makeNodeUnavailable((_b = (_a = nodeManager.getNode()) === null || _a === void 0 ? void 0 : _a.url) !== null && _b !== void 0 ? _b : "");
expect(nodeManager.getAvailableNodes().length).toBeLessThan(5);
nodeManager.makeAllNodesAvailable();
expect(nodeManager.getAvailableNodes()).toHaveLength(5);
}));
});
describe("makeNodeUnavailable", () => {
it("should set the sticky node to null if the node is the sticky node", () => {
const nodeManager = createNodeManager({
nodeUrls,
useStickyNode: true,
unavailableDuration: 5000,
});
const stickyNode = nodeManager.nodes[0];
nodeManager.setStickyNode(stickyNode);
expect(nodeManager.stickedNode).toBe(stickyNode);
nodeManager.makeNodeUnavailable(stickyNode.url);
expect(nodeManager.stickedNode).toBe(null);
});
it("should set the node with the provided node url, to unavailable for the configured duration", () => __awaiter(void 0, void 0, void 0, function* () {
const nodeUrls = [
"http://localhost:1000",
"http://localhost2:2000",
"http://localhost3:3000",
"http://localhost4:4000",
];
const unavailableDuration = 30000;
const nodeManager = createNodeManager({
nodeUrls: nodeUrls,
useStickyNode: true,
unavailableDuration,
});
nodeManager.makeNodeUnavailable(nodeManager.nodes[0].url);
const availableNodesByUrls = nodeManager.getAvailableNodes().map(node => node.url);
expect(nodeManager.getAvailableNodes()).toHaveLength(3);
expect(availableNodesByUrls).not.toContain(nodeUrls[0]);
}));
});
});
//# sourceMappingURL=nodeMananger.test.js.map