UNPKG

8.68 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.TestContext = exports.newTestExecutionContext = exports.newTestExecutionConfig = exports.newTestJobConfig = exports.newTestSlice = void 0;
7const path_1 = __importDefault(require("path"));
8const events_1 = require("events");
9const utils_1 = require("@terascope/utils");
10function newId(prefix) {
11 return `${prefix}-${utils_1.random(10000, 99999)}`;
12}
13function newTestSlice(request = {}) {
14 return {
15 slice_id: newId('slice-id'),
16 slicer_id: utils_1.random(0, 99999),
17 slicer_order: utils_1.random(0, 99999),
18 request,
19 _created: utils_1.makeISODate(),
20 };
21}
22exports.newTestSlice = newTestSlice;
23function newTestJobConfig(defaults = {}) {
24 return Object.assign({
25 name: 'test-job',
26 apis: [],
27 operations: [],
28 analytics: false,
29 autorecover: false,
30 assets: [],
31 lifecycle: 'once',
32 max_retries: 0,
33 probation_window: 30000,
34 slicers: 1,
35 workers: 1,
36 env_vars: {}
37 }, defaults);
38}
39exports.newTestJobConfig = newTestJobConfig;
40function newTestExecutionConfig(jobConfig = {}) {
41 const exConfig = newTestJobConfig(jobConfig);
42 exConfig.slicer_hostname = 'example.com';
43 exConfig.slicer_port = utils_1.random(8000, 60000);
44 exConfig.ex_id = newId('ex-id');
45 exConfig.job_id = newId('job-id');
46 if (!exConfig.metadata)
47 exConfig.metadata = {};
48 return exConfig;
49}
50exports.newTestExecutionConfig = newTestExecutionConfig;
51/**
52 * Create a new Execution Context
53 * @deprecated use the new WorkerExecutionContext and SlicerExecutionContext
54 */
55function newTestExecutionContext(type, config) {
56 if (type === 'execution_controller') {
57 return {
58 config,
59 queue: [],
60 reader: null,
61 slicer: () => { },
62 dynamicQueueLength: false,
63 queueLength: 10000,
64 };
65 }
66 return {
67 config,
68 queue: config.operations.map(() => () => { }),
69 reader: () => { },
70 slicer: () => { },
71 dynamicQueueLength: false,
72 queueLength: 10000,
73 };
74}
75exports.newTestExecutionContext = newTestExecutionContext;
76function getKey(opts) {
77 const { type, endpoint = 'default' } = opts;
78 if (!utils_1.isString(type))
79 throw new Error('A type must be specified when registering a Client');
80 return `${type}:${endpoint}`;
81}
82function setConnectorConfig(sysconfig, opts, config, override = true) {
83 const { type, endpoint = 'default' } = opts;
84 const { connectors } = sysconfig.terafoundation;
85 if (connectors[type] == null)
86 connectors[type] = {};
87 if (connectors[type][endpoint] == null) {
88 connectors[type][endpoint] = config;
89 }
90 else if (override) {
91 connectors[type][endpoint] = config;
92 }
93 return connectors[type][endpoint];
94}
95const _cachedClients = new WeakMap();
96const _createClientFns = new WeakMap();
97class TestContext {
98 constructor(testName, options = {}) {
99 this.assignment = 'worker';
100 this.platform = process.platform;
101 this.arch = process.arch;
102 const logger = utils_1.debugLogger(testName);
103 const events = new events_1.EventEmitter();
104 this.name = testName;
105 if (options.assignment) {
106 this.assignment = options.assignment;
107 }
108 this.logger = logger;
109 this.cluster = {
110 worker: {
111 id: newId('id'),
112 },
113 };
114 const sysconfig = {
115 terafoundation: {
116 connectors: {
117 elasticsearch: {
118 default: {},
119 },
120 },
121 },
122 teraslice: {
123 action_timeout: 10000,
124 analytics_rate: 10000,
125 assets_directory: path_1.default.join(process.cwd(), 'assets'),
126 cluster_manager_type: 'native',
127 hostname: 'localhost',
128 index_rollover_frequency: {
129 analytics: 'yearly',
130 state: 'montly',
131 },
132 master_hostname: 'localhost',
133 master: false,
134 name: testName,
135 network_latency_buffer: 100,
136 node_disconnect_timeout: 5000,
137 node_state_interval: 5000,
138 port: 55678,
139 shutdown_timeout: 10000,
140 slicer_allocation_attempts: 1,
141 slicer_port_range: '55679:56678',
142 slicer_timeout: 10000,
143 env_vars: {},
144 state: {
145 connection: 'default',
146 },
147 worker_disconnect_timeout: 3000,
148 workers: 1,
149 },
150 _nodeName: `${newId(testName)}__${this.cluster.worker.id}`,
151 };
152 this.sysconfig = sysconfig;
153 // eslint-disable-next-line @typescript-eslint/no-this-alias
154 const ctx = this;
155 _cachedClients.set(this, {});
156 _createClientFns.set(this, {});
157 this.apis = {
158 foundation: {
159 makeLogger(...params) {
160 return logger.child(params[0]);
161 },
162 getConnection(opts) {
163 const { cached } = opts;
164 const cachedClients = _cachedClients.get(ctx) || {};
165 const key = getKey(opts);
166 if (cached && cachedClients[key] != null) {
167 return cachedClients[key];
168 }
169 const clientFns = _createClientFns.get(ctx) || {};
170 const create = clientFns[key];
171 if (!create)
172 throw new Error(`No client was found for connection "${key}"`);
173 if (!utils_1.isFunction(create)) {
174 const actual = utils_1.getTypeOf(create);
175 throw new Error(`Registered Client for connection "${key}" is not a function, got ${actual}`);
176 }
177 const config = setConnectorConfig(sysconfig, opts, {}, false);
178 const client = create(config, logger, opts);
179 cachedClients[key] = client;
180 _cachedClients.set(ctx, cachedClients);
181 return client;
182 },
183 getSystemEvents() {
184 return events;
185 },
186 },
187 registerAPI(namespace, apis) {
188 this[namespace] = apis;
189 },
190 setTestClients(clients = []) {
191 clients.forEach((clientConfig) => {
192 const { create, config = {} } = clientConfig;
193 const clientFns = _createClientFns.get(ctx) || {};
194 const key = getKey(clientConfig);
195 if (!utils_1.isFunction(create)) {
196 const actual = utils_1.getTypeOf(create);
197 throw new Error(`Test Client for connection "${key}" is not a function, got ${actual}`);
198 }
199 logger.trace(`Setting test client for connection "${key}"`, config);
200 clientFns[key] = create;
201 _createClientFns.set(ctx, clientFns);
202 const cachedClients = _cachedClients.get(ctx) || {};
203 delete cachedClients[key];
204 _cachedClients.set(ctx, cachedClients);
205 setConnectorConfig(sysconfig, clientConfig, config, true);
206 });
207 },
208 getTestClients() {
209 const cachedClients = _cachedClients.get(ctx) || {};
210 const clients = {};
211 Object.keys(cachedClients).forEach((key) => {
212 const [type, endpoint] = key.split(':');
213 if (clients[type] == null) {
214 clients[type] = {};
215 }
216 clients[type][endpoint] = cachedClients[key];
217 });
218 return clients;
219 },
220 };
221 this.foundation = {
222 getConnection: this.apis.foundation.getConnection,
223 getEventEmitter: this.apis.foundation.getSystemEvents,
224 makeLogger: this.apis.foundation.makeLogger,
225 };
226 this.apis.setTestClients(options.clients);
227 }
228}
229exports.TestContext = TestContext;
230//# sourceMappingURL=test-helpers.js.map
\No newline at end of file