Make tests faster and more robust.

This commit is contained in:
Sam Rose 2023-12-18 17:05:50 +00:00
parent 1c34147357
commit c25963bc6f
No known key found for this signature in database
1 changed files with 49 additions and 45 deletions

View File

@ -2089,51 +2089,48 @@ describe.each([
describe("Formula JS protection", () => { describe("Formula JS protection", () => {
it("should time out JS execution if a single cell takes too long", async () => { it("should time out JS execution if a single cell takes too long", async () => {
await config.withEnv( await config.withEnv({ JS_PER_EXECUTION_TIME_LIMIT_MS: 20 }, async () => {
{ JS_PER_EXECUTION_TIME_LIMIT_MS: 100 }, const js = Buffer.from(
async () => { `
const js = Buffer.from(
`
let i = 0; let i = 0;
while (true) { while (true) {
i++; i++;
} }
return i; return i;
` `
).toString("base64") ).toString("base64")
const table = await config.createTable({ const table = await config.createTable({
name: "table", name: "table",
type: "table", type: "table",
schema: { schema: {
text: { text: {
name: "text", name: "text",
type: FieldType.STRING, type: FieldType.STRING,
},
formula: {
name: "formula",
type: FieldType.FORMULA,
formula: `{{ js "${js}"}}`,
formulaType: FormulaTypes.DYNAMIC,
},
}, },
}) formula: {
name: "formula",
type: FieldType.FORMULA,
formula: `{{ js "${js}"}}`,
formulaType: FormulaTypes.DYNAMIC,
},
},
})
await config.api.row.save(table._id!, { text: "foo" }) await config.api.row.save(table._id!, { text: "foo" })
const { rows } = await config.api.row.search(table._id!) const { rows } = await config.api.row.search(table._id!)
expect(rows).toHaveLength(1) expect(rows).toHaveLength(1)
const row = rows[0] const row = rows[0]
expect(row.text).toBe("foo") expect(row.text).toBe("foo")
expect(row.formula).toBe("Timed out while executing JS") expect(row.formula).toBe("Timed out while executing JS")
} })
)
}) })
it("should time out JS execution if a multiple cells take too long", async () => { it("should time out JS execution if a multiple cells take too long", async () => {
await config.withEnv( await config.withEnv(
{ {
JS_PER_EXECUTION_TIME_LIMIT_MS: 100, JS_PER_EXECUTION_TIME_LIMIT_MS: 20,
JS_PER_REQUEST_TIME_LIMIT_MS: 200, JS_PER_REQUEST_TIME_LIMIT_MS: 40,
}, },
async () => { async () => {
const js = Buffer.from( const js = Buffer.from(
@ -2167,24 +2164,31 @@ describe.each([
await config.api.row.save(table._id!, { text: "foo" }) await config.api.row.save(table._id!, { text: "foo" })
} }
const { rows } = await config.api.row.search(table._id!) // Run this test 3 times to make sure that there's no cross-request
expect(rows).toHaveLength(10) // polution of the execution time tracking.
for (let reqs = 0; reqs < 3; reqs++) {
const { rows } = await config.api.row.search(table._id!)
expect(rows).toHaveLength(10)
let i = 0 let i = 0
for (; i < 10; i++) { for (; i < 10; i++) {
const row = rows[i] const row = rows[i]
if (row.formula !== "Timed out while executing JS") { if (row.formula !== "Timed out while executing JS") {
break break
}
} }
}
expect(i).toBeGreaterThan(0) // Given the execution times are not deterministic, we can't be sure
expect(i).toBeLessThan(5) // of the exact number of rows that were executed before the timeout
// but it should absolutely be at least 1.
expect(i).toBeGreaterThan(0)
expect(i).toBeLessThan(5)
for (; i < 10; i++) { for (; i < 10; i++) {
const row = rows[i] const row = rows[i]
expect(row.text).toBe("foo") expect(row.text).toBe("foo")
expect(row.formula).toBe("Request JS execution limit hit") expect(row.formula).toBe("Request JS execution limit hit")
}
} }
} }
) )