Skip to content

Commit

Permalink
Refactor: Update Probe Data Independently (hyperjumptech#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
haricnugraha authored Dec 19, 2023
1 parent d35dc5e commit ea7ca45
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 138 deletions.
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 @@ export const updateConfig = async (config: Config): Promise<void> => {

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
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

0 comments on commit ea7ca45

Please sign in to comment.