Skip to content

Commit

Permalink
test: split two test
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 committed Oct 14, 2024
1 parent 365397e commit 9be4205
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions packages/cronjob/test/mongodb.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import Fastify from 'fastify'
import Fastify, { FastifyInstance } from 'fastify'
import { MongoClient } from 'mongodb'
import { test, TestContext } from 'node:test'
import { after, before, test, TestContext } from 'node:test'
import { setTimeout } from 'node:timers/promises'
import { fastifyCronJob, MongoAdapter } from '../lib'
import { MONGODB_URL } from './config'

test('MongoAdapter', async function (t: TestContext) {
const client = new MongoClient(MONGODB_URL)
await client.connect()
let client: MongoClient
let fastify: FastifyInstance
before(async () => {
client = new MongoClient(MONGODB_URL)
fastify = Fastify()

const db = client.db('cicd')
const fastify = Fastify()

await fastify.register(fastifyCronJob, {
adapter: new MongoAdapter({
Expand All @@ -20,27 +21,33 @@ test('MongoAdapter', async function (t: TestContext) {
})

await fastify.ready()
})

after(async () => {
await fastify.close()
await setTimeout(500)
await client.close()
})

test('cron', async function (t: TestContext) {
const cronTick: Date[] = []
const cron = await fastify.cronjob.setCronJob(() => {
cronTick.push(new Date())
}, '*/2 * * * * *', '*/2 * * * * *')
await setTimeout(5000)
fastify.cronjob.clearTimeout(cron)

t.assert.ok(cronTick.length >= 1 && cronTick.length <= 3)
})

test('loop', async function (t: TestContext) {
const loopTick: Date[] = []
const loop = await fastify.cronjob.setLoopTask(async () => {
loopTick.push(new Date())
await setTimeout(1000)
}, '1s')
await setTimeout(3000)
await setTimeout(5000)
fastify.cronjob.clearTimeout(loop)

await setTimeout(3000)
t.assert.ok(cronTick.length >= 1 && cronTick.length <= 3)
t.assert.ok(loopTick.length >= 1 && loopTick.length <= 3)

await fastify.close()
await setTimeout(500)
await client.close()
})

0 comments on commit 9be4205

Please sign in to comment.