Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Update Probe Data Independently #1203

2 changes: 1 addition & 1 deletion src/components/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@

setContext({ config: newConfig })
setProbes(newConfig.probes)
emitter.emit(events.config.updated, newConfig)
emitter.emit(events.config.updated)
log.info('Config file update detected')
} catch (error: unknown) {
const message = getErrorMessage(error)
Expand Down Expand Up @@ -232,7 +232,7 @@
}

const getPathAndTypeFromFlag = (flags: MonikaFlags) => {
// TODO: Assuming the first index of config is the primary config

Check warning on line 235 in src/components/config/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: Assuming the first index of config...'
let path = flags.config?.[0]
let type = 'monika'

Expand Down
162 changes: 71 additions & 91 deletions src/symon/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ import type { Probe } from '../interfaces/probe'

import SymonClient from '.'
import { getContext, resetContext, setContext } from '../context'
import { deleteProbe, getProbes } from '../components/config/probe'
import { deleteProbe, findProbe, getProbes } from '../components/config/probe'
import { validateProbes } from '../components/config/validation'
import events from '../events'
import { md5Hash } from '../utils/hash'
import { getEventEmitter } from '../utils/events'
import { getErrorMessage } from '../utils/catch-error-handler'

Expand Down Expand Up @@ -290,6 +289,25 @@ describe('Symon initiate', () => {

it('should add a new probe', async () => {
// arrange
const newProbe: Probe = {
id: '3',
name: 'New Probe',
interval: 2,
requests: [{ url: 'https://example.com', body: '', timeout: 2000 }],
alerts: [],
}
server.use(
rest.get(
'http://localhost:4000/api/v1/monika/1234/probe-changes',
(_, res, ctx) =>
res(
ctx.json({
message: 'Successfully get probe changes',
data: [{ type: 'add', probe: newProbe }],
})
)
)
)
const symonGetProbesIntervalMs = 100
setContext({
flags: {
Expand All @@ -312,59 +330,40 @@ describe('Symon initiate', () => {

// assert
// 3. Check the probe data after connected to Symon
expect(getProbes()).deep.eq(await validateProbes(config.probes))

// arrange
// 4. Simulate adding a probe
const newProbe: Probe = {
id: '3',
name: 'New Probe',
interval: 2,
requests: [{ url: 'https://example.com', body: '', timeout: 2000 }],
alerts: [],
}
server.use(
rest.get(
'http://localhost:4000/api/v1/monika/1234/probes',
(_, res, ctx) => {
const newProbes: Probe[] = [...config.probes, newProbe]

return res(
ctx.set('etag', md5Hash(newProbes)),
ctx.json({
statusCode: 'ok',
message: 'Successfully get probes configuration',
data: newProbes,
})
)
}
)
)
expect(getProbes().length).eq(2)

// act
// 5. Wait for the probe fetch to run
// 4. Wait for the probe fetch to run
await sleep(symonGetProbesIntervalMs)

// assert
// 6. Check the updated probe cache
expect(getProbes()).deep.eq(
await validateProbes([...config.probes, newProbe])
)
// 5. Check the updated probe cache
expect(getProbes().length).eq(3)

// act
// 7. Wait for probe fetch to run
await sleep(symonGetProbesIntervalMs)

// assert
// 8. Should not update the probe cache
expect(getProbes()).deep.eq(
await validateProbes([...config.probes, newProbe])
)
await symon.stop()
}).timeout(15_000)

it('should update a probe', async () => {
// arrange
server.use(
rest.get(
'http://localhost:4000/api/v1/monika/1234/probe-changes',
(_, res, ctx) =>
res(
ctx.json({
message: 'Successfully get probe changes',
data: [
{
type: 'update',
// eslint-disable-next-line camelcase
probe_id: '1',
probe: { ...findProbe('1'), interval: 2 },
},
],
})
)
)
)
const symonGetProbesIntervalMs = 100
setContext({
flags: {
Expand All @@ -387,42 +386,41 @@ describe('Symon initiate', () => {

// assert
// 3. Check the probe data after connected to Symon
expect(getProbes()).deep.eq(await validateProbes(config.probes))

// arrange
// 4. Simulate updating a probe
const updatedProbes: Probe[] = [{ ...config.probes[0], interval: 5 }]
server.use(
rest.get(
'http://localhost:4000/api/v1/monika/1234/probes',
(_, res, ctx) => {
const newProbes: Probe[] = updatedProbes

return res(
ctx.set('etag', md5Hash(newProbes)),
ctx.json({
statusCode: 'ok',
message: 'Successfully get probes configuration',
data: newProbes,
})
)
}
)
)
expect(getProbes().length).eq(2)

// act
// 5. Wait for the probe fetch to run
// 4. Wait for the probe fetch to run
await sleep(symonGetProbesIntervalMs)

// assert
// 6. Check the updated probe cache
expect(getProbes()).deep.eq(await validateProbes(updatedProbes))
// 5. Check the updated probe cache
expect(getProbes().length).eq(2)
expect(findProbe('1')?.interval).eq(2)

await symon.stop()
}).timeout(15_000)

it('should delete a probe', async () => {
// arrange
server.use(
rest.get(
'http://localhost:4000/api/v1/monika/1234/probe-changes',
(_, res, ctx) =>
res(
ctx.json({
message: 'Successfully get probe changes',
data: [
{
type: 'delete',
// eslint-disable-next-line camelcase
probe_id: '1',
probe: {},
},
],
})
)
)
)
const symonGetProbesIntervalMs = 100
setContext({
flags: {
Expand All @@ -445,33 +443,15 @@ describe('Symon initiate', () => {

// assert
// 3. Check the probe data after connected to Symon
expect(getProbes()).deep.eq(await validateProbes(config.probes))

// arrange
// 4. Simulate deleting a probe
const updatedProbes: Probe[] = config.probes.filter(({ id }) => id === '1')
server.use(
rest.get(
'http://localhost:4000/api/v1/monika/1234/probes',
(_, res, ctx) =>
res(
ctx.set('etag', md5Hash(updatedProbes)),
ctx.json({
statusCode: 'ok',
message: 'Successfully get probes configuration',
data: updatedProbes,
})
)
)
)
expect(getProbes().length).eq(2)

// act
// 5. Wait for the probe fetch to run
// 4. Wait for the probe fetch to run
await sleep(symonGetProbesIntervalMs)

// assert
// 6. Check the updated probe cache
expect(getProbes()).deep.eq(await validateProbes(updatedProbes))
// 5. Check the updated probe cache
expect(getProbes().length).eq(1)

await symon.stop()
}).timeout(15_000)
Expand Down
Loading
Loading