@ondc/automation-mock-runner 1.3.18 → 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.
@@ -4,8 +4,9 @@ import { Logger } from "./utils/logger";
4
4
  import { ExecutionResult } from "./types/execution-results";
5
5
  export declare class MockRunner {
6
6
  private config;
7
- private runner;
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,11 +44,9 @@ class MockRunner {
38
44
  }
39
45
  getRunnerInstance() {
40
46
  this.logger.debug("Getting code runner instance");
41
- if (!this.runner) {
42
- this.runner = runner_factory_1.RunnerFactory.createRunner({}, this.logger);
43
- }
44
- this.logger.debug("Code runner instance obtained successfully: " + this.runner.toString());
45
- return this.runner;
47
+ const runner = MockRunner.getSharedRunner(this.logger);
48
+ this.logger.debug("Code runner instance obtained successfully: " + runner.toString());
49
+ return runner;
46
50
  }
47
51
  getConfig() {
48
52
  return this.config;
@@ -566,7 +570,7 @@ class MockRunner {
566
570
  }
567
571
  static async runGetSave(payload, expression) {
568
572
  const evalExpression = MockRunner.decodeBase64(expression);
569
- const runner = runner_factory_1.RunnerFactory.createRunner();
573
+ const runner = MockRunner.getSharedRunner();
570
574
  const schema = (0, function_registry_1.getFunctionSchema)("getSave");
571
575
  return await runner.execute(evalExpression, schema, [payload]);
572
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", () => {
@@ -43,6 +49,41 @@ describe("MockRunner", () => {
43
49
  expect(mockRunner).toBeDefined();
44
50
  expect(mockRunner).toBeInstanceOf(MockRunner_1.MockRunner);
45
51
  });
52
+ it("should reuse the same BaseCodeRunner across MockRunner instances", async () => {
53
+ const baseConfig = {
54
+ meta: {
55
+ domain: "ONDC:TRV14",
56
+ version: "2.0.0",
57
+ flowId: "singleton-test",
58
+ },
59
+ transaction_data: {
60
+ transaction_id: "11111111-1111-1111-1111-111111111111",
61
+ latest_timestamp: "1970-01-01T00:00:00.000Z",
62
+ },
63
+ steps: [],
64
+ transaction_history: [],
65
+ validationLib: "",
66
+ helperLib: "",
67
+ };
68
+ const firstRunner = new MockRunner_1.MockRunner(baseConfig, true);
69
+ firstRunner
70
+ .getConfig()
71
+ .steps.push(firstRunner.getDefaultStep("search", "search_0"));
72
+ const optimizedConfig = await (0, configHelper_1.createOptimizedMockConfig)(firstRunner.getConfig());
73
+ const mockRunnerA = new MockRunner_1.MockRunner(optimizedConfig, true);
74
+ const mockRunnerB = new MockRunner_1.MockRunner(optimizedConfig, true);
75
+ expect(mockRunnerA.getRunnerInstance()).toBe(mockRunnerB.getRunnerInstance());
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
+ });
46
87
  });
47
88
  describe("Config Validation", () => {
48
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.18",
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",