UNPKG

@muirglacier/testcontainers

Version:

A collection of TypeScript + JavaScript tools and libraries for DeFi Blockchain developers to build decentralized finance for Bitcoin

124 lines 5.44 kB
"use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContainerGroup = void 0; const dockerode_1 = __importDefault(require("dockerode")); const utils_1 = require("../../utils"); class ContainerGroup { constructor(containers = [], name = `testcontainers-${Math.floor(Math.random() * 10000000)}`, options) { this.containers = containers; this.name = name; this.docker = new dockerode_1.default(options); } /** * @param {number} index of masternode in group * @return {MasterNodeRegTestContainer} casted as MN convenience but it might be just a RegTestContainer */ get(index) { return this.containers[index]; } start() { return __awaiter(this, void 0, void 0, function* () { this.network = yield new Promise((resolve, reject) => { return this.docker.createNetwork({ Name: this.name, IPAM: { Driver: 'default', Config: [] } }, (err, data) => { if (err instanceof Error || data === undefined) { return reject(err); } return resolve(data); }); }); // Removing all predefined containers and adding it to group for (const container of this.containers.splice(0)) { yield container.start(); yield this.add(container); } }); } /** * Require network, else error exceptionally. * Not a clean design, but it keep the complexity of this implementation low. */ requireNetwork() { if (this.network !== undefined) { return this.network; } throw new Error('network not yet started'); } /** * @param {DeFiDContainer} container to add into container group with addnode */ add(container) { return __awaiter(this, void 0, void 0, function* () { yield this.requireNetwork().connect({ Container: container.id }); for (const each of this.containers) { yield container.addNode(yield each.getIp(this.name)); } this.containers.push(container); }); } /** * Wait for all container to receive the same txid in mempool * * @param {string} txid to wait for in mempool * @param {number} [timeout=20000] in millis */ waitForMempoolSync(txid, timeout = 20000) { return __awaiter(this, void 0, void 0, function* () { yield utils_1.waitForCondition(() => __awaiter(this, void 0, void 0, function* () { const txns = yield Promise.all(Object.values(this.containers).map((container) => __awaiter(this, void 0, void 0, function* () { return yield container.call('getrawtransaction', [txid, false]); }))); return txns.every(value => value === txns[0]); }), timeout, 200, 'waitForMempoolSync'); }); } /** * Wait for all container to sync up * @param {number} [timeout=20000] in millis */ waitForSync(timeout = 20000) { return __awaiter(this, void 0, void 0, function* () { yield utils_1.waitForCondition(() => __awaiter(this, void 0, void 0, function* () { const hashes = yield Promise.all(Object.values(this.containers).map((container) => __awaiter(this, void 0, void 0, function* () { return yield container.getBestBlockHash(); }))); return hashes.every(value => value === hashes[0]); }), timeout, 200, 'waitForSync'); }); } /** * Stop container group and all containers associated with it */ stop() { return __awaiter(this, void 0, void 0, function* () { for (const container of this.containers) { yield this.requireNetwork().disconnect({ Container: container.id }); yield container.stop(); } yield this.requireNetwork().remove(); // NOTE: for anyone have RPC timeout issue, esp newer version docker, v3.6.x / v4.x.x // enable the following line to ensure docker network pruned correctly between tests // global containers prune may cause multithreaded tests clashing each other // await this.docker.pruneContainers() }); } } exports.ContainerGroup = ContainerGroup; //# sourceMappingURL=ContainerGroup.js.map