0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-12-01 12:21:02 +01:00
posthog/plugin-server/tests/server.test.ts
Karl-Aksel Puulmann 6c637397fc
refactor(plugin-server): extract capabilities for VMs and plugin server (#9714)
* Extract calculation method from vm/lazy.ts

* Add tests for capabilities.ts

* Move capabilities.ts under vm which it's more closely tied to

* Add (blank) capabilities object to hub

This will be used to check whether to start certain jobs

* Don't start plugin scheduled tasks unless have the capabilities

* scheduledTasks => pluginScheduledTasks in capabilities

* Cancel all jobs on schedule exit

* Remove capabilities code from services/schedule.ts

* scheduleControl => pluginScheduleControl

* Flip conditional

* Do less manual cleanup

* Add test for stopping node-schedule at exit

* Move cancelAllScheduledJobs

* Test capabilities check in server.test.ts

* Capabilities check for jobs

* Improve comment

* Rename method

* Rename jobs => processJobs

* Fix failed import
2022-05-11 09:01:54 +03:00

132 lines
4.2 KiB
TypeScript

import * as Sentry from '@sentry/node'
import * as nodeSchedule from 'node-schedule'
import { startJobQueueConsumer } from '../src/main/job-queues/job-queue-consumer'
import { ServerInstance, startPluginsServer } from '../src/main/pluginsServer'
import { startPluginSchedules } from '../src/main/services/schedule'
import { LogLevel, PluginServerCapabilities, PluginsServerConfig } from '../src/types'
import { killProcess } from '../src/utils/kill'
import { delay } from '../src/utils/utils'
import { makePiscina } from '../src/worker/piscina'
import { resetTestDatabase } from './helpers/sql'
jest.mock('@sentry/node')
jest.mock('../src/utils/db/sql')
jest.mock('../src/utils/kill')
jest.mock('../src/main/services/schedule')
jest.mock('../src/main/job-queues/job-queue-consumer')
jest.setTimeout(60000) // 60 sec timeout
function numberOfScheduledJobs() {
return Object.keys(nodeSchedule.scheduledJobs).length
}
describe('server', () => {
let pluginsServer: ServerInstance | null = null
function createPluginServer(
config: Partial<PluginsServerConfig> = {},
capabilities: PluginServerCapabilities | null = null
) {
return startPluginsServer(
{
WORKER_CONCURRENCY: 2,
LOG_LEVEL: LogLevel.Debug,
...config,
},
makePiscina,
capabilities
)
}
afterEach(async () => {
await pluginsServer?.stop()
pluginsServer = null
})
test('startPluginsServer does not error', async () => {
const testCode = `
async function processEvent (event) {
return event
}
`
await resetTestDatabase(testCode)
pluginsServer = await createPluginServer()
})
describe('plugin server staleness check', () => {
test('test if the server terminates', async () => {
const testCode = `
async function processEvent (event) {
return event
}
`
await resetTestDatabase(testCode)
pluginsServer = await createPluginServer({
STALENESS_RESTART_SECONDS: 5,
})
await delay(10000)
expect(killProcess).toHaveBeenCalled()
expect(Sentry.captureMessage).toHaveBeenCalledWith(
`Plugin Server has not ingested events for over 5 seconds! Rebooting.`,
{
extra: {
instanceId: expect.any(String),
lastActivity: expect.any(String),
lastActivityType: 'serverStart',
piscina: expect.any(String),
isServerStale: true,
timeSinceLastActivity: expect.any(Number),
},
}
)
})
})
test('starting and stopping node-schedule scheduled jobs', async () => {
expect(numberOfScheduledJobs()).toEqual(0)
pluginsServer = await createPluginServer()
expect(numberOfScheduledJobs()).toBeGreaterThan(1)
await pluginsServer.stop()
pluginsServer = null
expect(numberOfScheduledJobs()).toEqual(0)
})
describe('plugin-server capabilities', () => {
test('starts all main services by default', async () => {
pluginsServer = await createPluginServer()
expect(startPluginSchedules).toHaveBeenCalled()
expect(startJobQueueConsumer).toHaveBeenCalled()
})
test('disabling pluginScheduledTasks', async () => {
pluginsServer = await createPluginServer(
{},
{ ingestion: true, pluginScheduledTasks: false, processJobs: true }
)
expect(startPluginSchedules).not.toHaveBeenCalled()
expect(startJobQueueConsumer).toHaveBeenCalled()
})
test('disabling processJobs', async () => {
pluginsServer = await createPluginServer(
{},
{ ingestion: true, pluginScheduledTasks: true, processJobs: false }
)
expect(startPluginSchedules).toHaveBeenCalled()
expect(startJobQueueConsumer).not.toHaveBeenCalled()
})
})
})