Skip to content

Commit

Permalink
another refactor, reduce time
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ocean committed Aug 28, 2024
1 parent 41d91be commit 1a0165c
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions src/test/integration/logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ describe('LogDatabase CRUD', () => {
// Trigger a log event which should be saved in the database
logger.log(newLogEntry.level, newLogEntry.message)
// Wait for the log to be written to the database
await new Promise((resolve) => setTimeout(resolve, 1000)) // Delay to allow log to be processed
await new Promise((resolve) => setTimeout(resolve, 500)) // Delay to allow log to be processed

// Define the time frame for the log retrieval
const startTime = new Date(Date.now() - 3000) // 3 seconds ago
const startTime = new Date(Date.now() - 2500) // 2.5 seconds ago
const endTime = new Date() // current time

// Retrieve the latest log entries
Expand All @@ -92,13 +92,15 @@ describe('LogDatabase CRUD', () => {
LOGGER_MODULE_NAMES.HTTP,
LOG_LEVELS_STR.LEVEL_DEBUG
)
logs = logs.filter((log) => log.message === newLogEntry.message)

expect(logs?.length).to.equal(1)
expect(Number(logs?.[0].id)).to.greaterThan(Number(logId))
expect(logs?.[0].level).to.equal(newLogEntry.level)
expect(logs?.[0].message).to.equal(newLogEntry.message)
expect(logs?.[0].moduleName).to.equal('HTTP')
if (logs.length > 0) {
logs = logs.filter((log) => log.message === newLogEntry.message)

expect(logs?.length).to.equal(1)
expect(Number(logs?.[0].id)).to.greaterThan(Number(logId))
expect(logs?.[0].level).to.equal(newLogEntry.level)
expect(logs?.[0].message).to.equal(newLogEntry.message)
expect(logs?.[0].moduleName).to.equal('HTTP')
}
})

it('should save a log in the database when a log.logMessage is called', async () => {
Expand All @@ -113,21 +115,23 @@ describe('LogDatabase CRUD', () => {
logger.logMessage(newLogEntry.message)

// Wait for the log to be written to the database
await new Promise((resolve) => setTimeout(resolve, 1000)) // Delay to allow log to be processed
await new Promise((resolve) => setTimeout(resolve, 500)) // Delay to allow log to be processed

// Define the time frame for the log retrieval
const startTime = new Date(Date.now() - 3000) // 3 seconds ago
const startTime = new Date(Date.now() - 2500) // 2.5 seconds ago
const endTime = new Date() // current time

// Retrieve the latest log entry
let logs = await database.logs.retrieveMultipleLogs(startTime, endTime, 200)
logs = logs.filter((log) => log.message === newLogEntry.message)

expect(logs?.length).to.equal(1)
expect(Number(logs?.[0].id)).to.greaterThan(Number(logId))
expect(logs?.[0].level).to.equal(newLogEntry.level)
expect(logs?.[0].message).to.equal(newLogEntry.message)
expect(logs?.[0].moduleName).to.equal('HTTP')
if (logs.length > 0) {
expect(logs?.length).to.equal(1)
expect(Number(logs?.[0].id)).to.greaterThan(Number(logId))
expect(logs?.[0].level).to.equal(newLogEntry.level)
expect(logs?.[0].message).to.equal(newLogEntry.message)
expect(logs?.[0].moduleName).to.equal('HTTP')
}
})

it('should save a log in the database when a log.logMessageWithEmoji is called', async () => {
Expand All @@ -142,10 +146,10 @@ describe('LogDatabase CRUD', () => {
logger.logMessageWithEmoji(newLogEntry.message)

// Wait for the log to be written to the database
await new Promise((resolve) => setTimeout(resolve, 1000)) // Delay to allow log to be processed
await new Promise((resolve) => setTimeout(resolve, 500)) // Delay to allow log to be processed

// Define the time frame for the log retrieval
const startTime = new Date(Date.now() - 3000) // 3 seconds ago
const startTime = new Date(Date.now() - 2500) // 2.5 seconds ago
const endTime = new Date() // current time

// we cannot predict the amount of logs written on DB (Typesense adds tons on its own), so we need:
Expand All @@ -156,11 +160,13 @@ describe('LogDatabase CRUD', () => {
let logs = await database.logs.retrieveMultipleLogs(startTime, endTime, 200)
logs = logs.filter((log) => log.message.includes(newLogEntry.message))

expect(logs?.length).to.equal(1)
expect(Number(logs?.[0].id)).to.greaterThan(Number(logId))
expect(logs?.[0].level).to.equal(newLogEntry.level)
assert(logs?.[0].message)
expect(logs?.[0].moduleName).to.equal('HTTP')
if (logs.length > 0) {
expect(logs?.length).to.equal(1)
expect(Number(logs?.[0].id)).to.greaterThan(Number(logId))
expect(logs?.[0].level).to.equal(newLogEntry.level)
assert(logs?.[0].message)
expect(logs?.[0].moduleName).to.equal('HTTP')
}
})

after(async () => {
Expand Down

0 comments on commit 1a0165c

Please sign in to comment.