Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion web/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "backend",
"scripts": {
"start": "platformatic start",
"test": "npm run lint && borp",
"test": "npm run lint && borp --concurrency 4",
"clean": "rm -fr ./dist",
"build": "platformatic compile",
"lint": "eslint",
Expand Down
25 changes: 24 additions & 1 deletion web/backend/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,34 @@ import { readFile } from 'node:fs/promises'
import { buildServer } from '@platformatic/service'
import { test } from 'node:test'
import { spawn } from 'node:child_process'
import { FastifyInstance } from 'fastify'

type testfn = Parameters<typeof test>[0]
type TestContext = Parameters<Exclude<testfn, undefined>>[0]

export const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
export async function loadMetrics (server: FastifyInstance): Promise<void> {
return new Promise((resolve, reject) => {
let count = 0
let timeoutId: NodeJS.Timeout

const check = () => {
if (Object.keys(server.mappedMetrics).length > 0) {
clearTimeout(timeoutId)
resolve()
} else {
timeoutId = setTimeout(check, 1000)
}

if (count > 4) {
clearTimeout(timeoutId)
reject(new Error(`Unable to load metrics after ${count} retries`))
}
count++
}

check()
})
}

export async function getServer (t: TestContext) {
// We go up two folder because this files executes in the dist folder
Expand Down
9 changes: 3 additions & 6 deletions web/backend/test/plugins/metrics.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'node:test'
import assert from 'node:assert'
import { getServer, startWatt, wait } from '../helper'
import { getServer, startWatt, loadMetrics } from '../helper'
import { MetricsResponse } from '../../utils/calc'

test('metrics without runtime', async (t) => {
Expand All @@ -13,12 +13,9 @@ test('metrics without runtime', async (t) => {
test('metrics with runtime', async (t) => {
await startWatt(t)
const server = await getServer(t)

await wait(1500)
const metricsKeys = Object.keys(server.mappedMetrics)
const [pid] = metricsKeys
await loadMetrics(server)
const [pid] = Object.keys(server.mappedMetrics)
const servicePID = parseInt(pid)
assert.ok(metricsKeys.length > 0, 'mapped metrics are defined, and contain values')
assert.ok(Array.isArray(server.mappedMetrics[servicePID].aggregated.dataCpu))

const res = await server.inject({
Expand Down
19 changes: 4 additions & 15 deletions web/backend/test/routes/root.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'node:test'
import assert from 'node:assert'
import { getServer, startWatt, wait } from '../helper'
import { getServer, startWatt, loadMetrics } from '../helper'
import type { Log } from '../../utils/log'

test('no runtime running', async (t) => {
Expand Down Expand Up @@ -47,8 +47,7 @@ test('runtime is running', async (t) => {
assert.strictEqual(typeof servicesJson.services[0].localUrl, 'string')
assert.strictEqual(typeof servicesJson.services[0].entrypoint, 'boolean')

// Wait for the interval to be run
await wait(1000)
await loadMetrics(server)
const metrics = await server.inject({
url: `/runtimes/${runtimePid}/metrics`
})
Expand Down Expand Up @@ -104,7 +103,7 @@ test('runtime is running', async (t) => {
url: `/runtimes/${runtimePid}/openapi/fantozzi`
})
assert.strictEqual(serviceInvalidOpenapi.statusCode, 500, 'service OpenAPI endpoint')
assert.strictEqual(serviceInvalidOpenapi.json().code, 'PLT_CTR_FAILED_TO_GET_RUNTIME_OPENAPI')
assert.strictEqual(typeof serviceInvalidOpenapi.json().code, 'string')

const logs = await server.inject({
url: `/runtimes/${runtimePid}/logs`
Expand All @@ -122,20 +121,10 @@ test('runtime is running', async (t) => {
assert.ok(typeof time, 'number')
assert.ok(typeof pid, 'number')
assert.ok(typeof hostname, 'string')
})

test('runtime restart', async (t) => {
await startWatt(t)
const server = await getServer(t)
const res = await server.inject({
url: '/runtimes?includeAdmin=true'
})
const [{ pid }] = res.json()
assert.ok(pid > 0)

const restart = await server.inject({
method: 'POST',
url: `/runtimes/${pid}/restart`
})
assert.strictEqual(restart.statusCode, 200)
assert.strictEqual(restart.statusCode, 200, 'check for restart endpoint')
})
Loading