0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-25 11:17:50 +01:00
posthog/plugin-server/tests/http-server.test.ts
Harry Waye 7ba6fa7148
chore(plugin-server): remove piscina workers (#15327)
* chore(plugin-server): remove piscina workers

Using Piscina workers introduces complexity that would rather be
avoided. It does offer the ability to scale work across multiple CPUs,
but we can achieve this via starting multiple processes instead. It may
also provide some protection from deadlocking the worker process, which
I believe Piscina will handle by killing worker processes and
respawning, but we have K8s liveness checks that will also handle this.

This should simplify 1. prom metrics exporting, and 2. using
node-rdkafka.

* remove piscina from package.json

* use createWorker

* wip

* wip

* wip

* wip

* fix export test

* wip

* wip

* fix server stop tests

* wip

* mock process.exit everywhere

* fix health server tests

* Remove collectMetrics

* wip
2023-05-03 14:42:16 +00:00

83 lines
2.4 KiB
TypeScript

import http from 'http'
import { startPluginsServer } from '../src/main/pluginsServer'
import { HTTP_SERVER_PORT } from '../src/main/services/http-server'
import { makePiscina } from '../src/worker/piscina'
import { resetTestDatabase } from './helpers/sql'
jest.mock('../src/utils/status')
jest.mock('../src/main/utils', () => {
const actual = jest.requireActual('../src/main/utils')
return {
...actual,
kafkaHealthcheck: async () => {
await Promise.resolve()
return [true, null]
},
}
})
jest.setTimeout(60000) // 60 sec timeout
describe('http server', () => {
// these should simply pass under normal conditions
describe('health and readiness checks', () => {
test('_health', async () => {
const testCode = `
async function processEvent (event) {
return event
}
`
await resetTestDatabase(testCode)
const pluginsServer = await startPluginsServer(
{
WORKER_CONCURRENCY: 0,
},
makePiscina,
{ http: true }
)
await new Promise((resolve) =>
http.get(`http://localhost:${HTTP_SERVER_PORT}/_health`, (res) => {
const { statusCode } = res
expect(statusCode).toEqual(200)
resolve(null)
})
)
await pluginsServer.stop()
})
test('_ready', async () => {
const testCode = `
async function processEvent (event) {
return event
}
`
await resetTestDatabase(testCode)
const pluginsServer = await startPluginsServer(
{
WORKER_CONCURRENCY: 0,
},
makePiscina,
{ http: true, ingestion: true }
)
await new Promise((resolve) =>
http.get(`http://localhost:${HTTP_SERVER_PORT}/_ready`, (res) => {
const { statusCode } = res
expect(statusCode).toEqual(200)
resolve(null)
})
)
expect(pluginsServer.queue?.consumerReady).toBeTruthy()
await pluginsServer.stop()
})
})
})