@ondc/automation-mock-runner 1.3.19 → 1.3.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,7 @@ export declare class MockRunner {
6
6
  private config;
7
7
  private static sharedRunner;
8
8
  logger: Logger;
9
+ private static getSharedRunner;
9
10
  constructor(config: MockPlaygroundConfigType, skipValidation?: boolean);
10
11
  getRunnerInstance(): BaseCodeRunner;
11
12
  getConfig(): {
@@ -12,6 +12,12 @@ const jsonpath_1 = __importDefault(require("jsonpath"));
12
12
  const function_registry_1 = require("./constants/function-registry");
13
13
  const uuid_1 = require("uuid");
14
14
  class MockRunner {
15
+ static getSharedRunner(logger) {
16
+ if (!MockRunner.sharedRunner) {
17
+ MockRunner.sharedRunner = runner_factory_1.RunnerFactory.createRunner({}, logger);
18
+ }
19
+ return MockRunner.sharedRunner;
20
+ }
15
21
  constructor(config, skipValidation = false) {
16
22
  this.logger = logger_1.Logger.getInstance();
17
23
  if (!skipValidation) {
@@ -38,12 +44,9 @@ class MockRunner {
38
44
  }
39
45
  getRunnerInstance() {
40
46
  this.logger.debug("Getting code runner instance");
41
- if (!MockRunner.sharedRunner) {
42
- MockRunner.sharedRunner = runner_factory_1.RunnerFactory.createRunner({}, this.logger);
43
- }
44
- this.logger.debug("Code runner instance obtained successfully: " +
45
- MockRunner.sharedRunner.toString());
46
- return MockRunner.sharedRunner;
47
+ const runner = MockRunner.getSharedRunner(this.logger);
48
+ this.logger.debug("Code runner instance obtained successfully: " + runner.toString());
49
+ return runner;
47
50
  }
48
51
  getConfig() {
49
52
  return this.config;
@@ -567,7 +570,7 @@ class MockRunner {
567
570
  }
568
571
  static async runGetSave(payload, expression) {
569
572
  const evalExpression = MockRunner.decodeBase64(expression);
570
- const runner = runner_factory_1.RunnerFactory.createRunner();
573
+ const runner = MockRunner.getSharedRunner();
571
574
  const schema = (0, function_registry_1.getFunctionSchema)("getSave");
572
575
  return await runner.execute(evalExpression, schema, [payload]);
573
576
  }
@@ -37,7 +37,7 @@ exports.NodeRunner = void 0;
37
37
  const worker_threads_1 = require("worker_threads");
38
38
  const code_validator_1 = require("../validators/code-validator");
39
39
  const path = __importStar(require("path"));
40
- const DEFAULT_POOL_SIZE = 10;
40
+ const DEFAULT_POOL_SIZE = 2;
41
41
  const MAX_EXECUTIONS_PER_WORKER = 100;
42
42
  const MAX_WORKER_AGE_MS = 10 * 60 * 1000; // 10 minutes
43
43
  class NodeRunner {
@@ -5,6 +5,7 @@
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const configHelper_1 = require("../lib/configHelper");
7
7
  const MockRunner_1 = require("../lib/MockRunner");
8
+ const runner_factory_1 = require("../lib/runners/runner-factory");
8
9
  describe("MockRunner", () => {
9
10
  let mockRunner;
10
11
  let mockConfig;
@@ -35,7 +36,12 @@ describe("MockRunner", () => {
35
36
  throw error;
36
37
  }
37
38
  });
38
- afterEach(() => {
39
+ afterEach(async () => {
40
+ const sharedRunner = MockRunner_1.MockRunner.sharedRunner;
41
+ if (sharedRunner?.terminate) {
42
+ await sharedRunner.terminate();
43
+ }
44
+ MockRunner_1.MockRunner.sharedRunner = undefined;
39
45
  jest.clearAllMocks();
40
46
  });
41
47
  describe("Constructor", () => {
@@ -68,6 +74,16 @@ describe("MockRunner", () => {
68
74
  const mockRunnerB = new MockRunner_1.MockRunner(optimizedConfig, true);
69
75
  expect(mockRunnerA.getRunnerInstance()).toBe(mockRunnerB.getRunnerInstance());
70
76
  });
77
+ it("should reuse singleton runner for runGetSave", async () => {
78
+ MockRunner_1.MockRunner.sharedRunner = undefined;
79
+ const createRunnerSpy = jest.spyOn(runner_factory_1.RunnerFactory, "createRunner");
80
+ mockRunner.getRunnerInstance();
81
+ const getSaveFunction = MockRunner_1.MockRunner.encodeBase64(`async function getSave(payload){ return payload.value; }`);
82
+ const result = await MockRunner_1.MockRunner.runGetSave({ value: "singleton-check" }, getSaveFunction);
83
+ expect(result.success).toBe(true);
84
+ expect(result.result).toBe("singleton-check");
85
+ expect(createRunnerSpy).toHaveBeenCalledTimes(1);
86
+ });
71
87
  });
72
88
  describe("Config Validation", () => {
73
89
  it("should validate correct config successfully", () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ondc/automation-mock-runner",
3
- "version": "1.3.19",
3
+ "version": "1.3.20",
4
4
  "description": "A TypeScript library for ONDC automation mock runner",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",