From 9f4ff190c340fbd8eb28f6d5fefc5424479a71ca Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 11 Nov 2021 16:20:30 +0000 Subject: [PATCH] Fixing issues dsicovered by automation test cases, as well as disabling threading for test scenarios. --- packages/server/src/api/controllers/query.js | 40 +++++++++++-------- .../src/automations/tests/automation.spec.js | 6 +-- packages/server/src/integrations/base/sql.ts | 1 + packages/server/src/threads/index.js | 27 +++++++++---- 4 files changed, 47 insertions(+), 27 deletions(-) diff --git a/packages/server/src/api/controllers/query.js b/packages/server/src/api/controllers/query.js index 7323355c64..502ef5e67b 100644 --- a/packages/server/src/api/controllers/query.js +++ b/packages/server/src/api/controllers/query.js @@ -106,16 +106,20 @@ exports.preview = async function (ctx) { const { fields, parameters, queryVerb, transformer } = ctx.request.body const enrichedQuery = await enrichQueryFields(fields, parameters) - const { rows, keys } = await Runner.run({ - datasource, - queryVerb, - query: enrichedQuery, - transformer, - }) + try { + const { rows, keys } = await Runner.run({ + datasource, + queryVerb, + query: enrichedQuery, + transformer, + }) - ctx.body = { - rows, - schemaFields: [...new Set(keys)], + ctx.body = { + rows, + schemaFields: [...new Set(keys)], + } + } catch (err) { + ctx.throw(400, err) } } @@ -131,13 +135,17 @@ exports.execute = async function (ctx) { ) // call the relevant CRUD method on the integration class - const { rows } = await Runner.run({ - datasource, - queryVerb: query.queryVerb, - query: enrichedQuery, - transformer: query.transformer, - }) - ctx.body = rows + try { + const { rows } = await Runner.run({ + datasource, + queryVerb: query.queryVerb, + query: enrichedQuery, + transformer: query.transformer, + }) + ctx.body = rows + } catch (err) { + ctx.throw(400, err) + } } exports.destroy = async function (ctx) { diff --git a/packages/server/src/automations/tests/automation.spec.js b/packages/server/src/automations/tests/automation.spec.js index 81d34cea54..50c099cfb6 100644 --- a/packages/server/src/automations/tests/automation.spec.js +++ b/packages/server/src/automations/tests/automation.spec.js @@ -1,5 +1,5 @@ jest.mock("../../utilities/usageQuota") -jest.mock("../thread") +jest.mock("../../threads/automation") jest.mock("../../utilities/redis", () => ({ init: jest.fn(), checkTestFlag: () => { @@ -53,7 +53,7 @@ describe("Run through some parts of the automations system", () => { } }) await wait(100) - expect().toHaveBeenCalledWith(makePartial({ + expect(thread).toHaveBeenCalledWith(makePartial({ data: { event: { fields: { @@ -61,7 +61,7 @@ describe("Run through some parts of the automations system", () => { } } } - })) + }), expect.any(Function)) }) it("should be able to clean inputs with the utilities", () => { diff --git a/packages/server/src/integrations/base/sql.ts b/packages/server/src/integrations/base/sql.ts index 08bff54fae..64f03aea47 100644 --- a/packages/server/src/integrations/base/sql.ts +++ b/packages/server/src/integrations/base/sql.ts @@ -33,6 +33,7 @@ function parse(input: any) { if (isIsoDateString(input)) { return new Date(input) } + return input } function parseBody(body: any) { diff --git a/packages/server/src/threads/index.js b/packages/server/src/threads/index.js index 909f381fd8..e24b45bbfa 100644 --- a/packages/server/src/threads/index.js +++ b/packages/server/src/threads/index.js @@ -1,4 +1,5 @@ const workerFarm = require("worker-farm") +const env = require("../environment") const ThreadType = { QUERY: "query", @@ -22,19 +23,29 @@ function typeToFile(type) { class Thread { constructor(type, opts = { timeoutMs: null, count: 1 }) { - const workerOpts = { - autoStart: true, - maxConcurrentWorkers: opts.count ? opts.count : 1, + this.type = type + if (!env.isTest()) { + const workerOpts = { + autoStart: true, + maxConcurrentWorkers: opts.count ? opts.count : 1, + } + if (opts.timeoutMs) { + workerOpts.maxCallTime = opts.timeoutMs + } + this.workers = workerFarm(workerOpts, typeToFile(type)) } - if (opts.timeoutMs) { - workerOpts.maxCallTime = opts.timeoutMs - } - this.workers = workerFarm(workerOpts, typeToFile(type)) } run(data) { return new Promise((resolve, reject) => { - this.workers(data, (err, response) => { + let fncToCall + // if in test then don't use threading + if (env.isTest()) { + fncToCall = require(typeToFile(this.type)) + } else { + fncToCall = this.workers + } + fncToCall(data, (err, response) => { if (err) { reject(err) } else {