0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-25 11:17:50 +01:00
posthog/plugin-server/jest.setup.fetch-mock.js
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

49 lines
1.9 KiB
JavaScript

const { readFileSync } = require('fs')
const { DateTime } = require('luxon')
const { join } = require('path')
import fetch from 'node-fetch'
import { status } from './src/utils/status'
jest.mock('node-fetch')
beforeEach(() => {
const responsesToUrls = {
'https://google.com/results.json?query=fetched': { count: 2, query: 'bla', results: [true, true] },
'https://mmdbcdn.posthog.net/': readFileSync(join(__dirname, 'tests', 'assets', 'GeoLite2-City-Test.mmdb.br')),
'https://app.posthog.com/api/event?token=THIS+IS+NOT+A+TOKEN+FOR+TEAM+2': { hello: 'world' },
}
const headersToUrls = {
'https://mmdbcdn.posthog.net/': new Map([
['content-type', 'vnd.maxmind.maxmind-db'],
['content-disposition', `attachment; filename="GeoLite2-City-${DateTime.local().toISODate()}.mmdb"`],
]),
}
fetch.mockImplementation(
(url, options) =>
new Promise((resolve) =>
resolve({
buffer: () => new Promise((resolve) => resolve(responsesToUrls[url]) || Buffer.from('fetchmock')),
json: () => new Promise((resolve) => resolve(responsesToUrls[url]) || { fetch: 'mock' }),
text: () => new Promise((resolve) => resolve(JSON.stringify(responsesToUrls[url])) || 'fetchmock'),
status: () => (options.method === 'PUT' ? 201 : 200),
headers: headersToUrls[url],
})
)
)
})
// NOTE: in testing we use the pino-pretty transport, which results in a handle
// that we need to close to allow Jest to exit properly.
afterAll(() => status.close())
beforeAll(() => {
// We use procese.exit in a few places, which end up terminating tests
// if we don't mock it.
jest.spyOn(process, 'exit').mockImplementation((number) => {
throw new Error('process.exit: ' + number)
})
})