UNPKG

@muirglacier/testcontainers

Version:

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

90 lines 4.03 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PersistentMNRegTestContainer = void 0; const Masternode_1 = require("./Masternode"); const DeFiDContainer_1 = require("../DeFiDContainer"); function getContainerInfoByName(docker, name) { return __awaiter(this, void 0, void 0, function* () { return yield new Promise((resolve, reject) => { const opts = { limit: 1, filters: `{"name": ["${name}"]}` }; docker.listContainers(opts, function (err, containers) { if (err === null && containers !== undefined) { resolve(containers[0]); } else { reject(err); } }); }); }); } /** * PersistentMNRegTestContainer container is a RegTest container with MasterNode minting preconfigured. * The container configuration is persistent and can be used consistently. * If you do not stop the container, the same container can be used for all tests. * However, you must be cognizant of race conditions. * * This container should not be used for finished work, it merely a dev tool to speed up test-driven development. * Once you are done with your dev work, you should swap this out for MasterNodeRegTestContainer. */ class PersistentMNRegTestContainer extends Masternode_1.MasterNodeRegTestContainer { /** * Init the required container instances for start/stop operation */ init() { return __awaiter(this, void 0, void 0, function* () { const info = yield getContainerInfoByName(this.docker, this.generateName()); this.container = (info === null || info === void 0 ? void 0 : info.Id) !== undefined ? this.docker.getContainer(info.Id) : undefined; }); } /** * This will only start a persistent container if it's not yet already started. * @param {StartOptions} [startOptions={}] to start the container with * @see {generateName()} for the name of container */ start(startOptions = {}) { const _super = Object.create(null, { stop: { get: () => super.stop }, start: { get: () => super.start } }); return __awaiter(this, void 0, void 0, function* () { this.startOptions = Object.assign(DeFiDContainer_1.DeFiDContainer.DefaultStartOptions, startOptions); try { yield this.init(); this.requireContainer(); yield this.waitForReady(3000); } catch (e) { // Attempt clean up before starting yield _super.stop.call(this); yield _super.start.call(this, startOptions); } }); } /** * @return {string} name of persistent container that is always consistent. */ generateName() { return `${DeFiDContainer_1.DeFiDContainer.PREFIX}-${this.network}-persistent`; } stop() { const _super = Object.create(null, { stop: { get: () => super.stop } }); return __awaiter(this, void 0, void 0, function* () { yield this.init(); yield _super.stop.call(this); }); } } exports.PersistentMNRegTestContainer = PersistentMNRegTestContainer; //# sourceMappingURL=Persistent.js.map