running worker in the test

This commit is contained in:
Hongbo Wu
2024-01-31 17:45:49 +08:00
parent cf898299e4
commit 5e883cb2ba
14 changed files with 152 additions and 155 deletions

View File

@ -1,7 +1,7 @@
import { env } from '../src/env'
import { redisDataSource } from '../src/redis_data_source'
import { createTestConnection } from './db'
import { startApolloServer } from './util'
import { startApolloServer, startWorker } from './util'
export const mochaGlobalSetup = async () => {
await createTestConnection()
@ -10,6 +10,11 @@ export const mochaGlobalSetup = async () => {
if (env.redis.cache.url) {
await redisDataSource.initialize()
console.log('redis connection created')
if (redisDataSource.workerRedisClient) {
startWorker(redisDataSource.workerRedisClient)
console.log('worker started')
}
}
await startApolloServer()

View File

@ -1,7 +1,7 @@
import { appDataSource } from '../src/data_source'
import { env } from '../src/env'
import { redisDataSource } from '../src/redis_data_source'
import { stopApolloServer } from './util'
import { stopApolloServer, stopWorker } from './util'
export const mochaGlobalTeardown = async () => {
await stopApolloServer()
@ -13,5 +13,10 @@ export const mochaGlobalTeardown = async () => {
if (env.redis.cache.url) {
await redisDataSource.shutdown()
console.log('redis connection closed')
if (redisDataSource.workerRedisClient) {
stopWorker()
console.log('worker closed')
}
}
}

View File

@ -42,7 +42,12 @@ import { deleteUser } from '../../src/services/user'
import * as createTask from '../../src/utils/createTask'
import * as uploads from '../../src/utils/uploads'
import { createTestLibraryItem, createTestUser } from '../db'
import { generateFakeUuid, graphqlRequest, request } from '../util'
import {
generateFakeUuid,
graphqlRequest,
request,
waitUntilJobsDone,
} from '../util'
chai.use(chaiString)
@ -942,6 +947,8 @@ describe('Article API', () => {
)
highlights.push(highlight)
}
await waitUntilJobsDone()
})
beforeEach(async () => {
@ -1287,6 +1294,8 @@ describe('Article API', () => {
)
await saveLabelsInLibraryItem([label1], items[0].id, user.id)
await saveLabelsInLibraryItem([label2], items[1].id, user.id)
await waitUntilJobsDone()
})
after(async () => {
@ -1784,6 +1793,8 @@ describe('Article API', () => {
)
await saveLabelsInLibraryItem([label1], items[0].id, user.id)
await saveLabelsInLibraryItem([label2], items[1].id, user.id)
await waitUntilJobsDone()
})
after(async () => {
@ -1842,6 +1853,8 @@ describe('Article API', () => {
)
await saveLabelsInLibraryItem([label1], items[0].id, user.id)
await saveLabelsInLibraryItem([label2], items[1].id, user.id)
await waitUntilJobsDone()
})
after(async () => {

View File

@ -2,7 +2,9 @@ import * as chai from 'chai'
import { expect } from 'chai'
import chaiString from 'chai-string'
import 'mocha'
import { Highlight } from '../../src/entity/highlight'
import { User } from '../../src/entity/user'
import { getRepository } from '../../src/repository'
import {
createHighlight,
deleteHighlightById,
@ -11,7 +13,12 @@ import {
import { createLabel, saveLabelsInHighlight } from '../../src/services/labels'
import { deleteUser } from '../../src/services/user'
import { createTestLibraryItem, createTestUser } from '../db'
import { generateFakeShortId, generateFakeUuid, graphqlRequest, request } from '../util'
import {
generateFakeShortId,
generateFakeUuid,
graphqlRequest,
request,
} from '../util'
chai.use(chaiString)
@ -281,7 +288,7 @@ describe('Highlights API', () => {
itemId,
newHighlightId,
newShortHighlightId,
[highlightId],
[highlightId]
)
const res = await graphqlRequest(query, authToken).expect(200)

View File

@ -1,11 +1,14 @@
import { ConnectionOptions, Worker } from 'bullmq'
import { nanoid } from 'nanoid'
import supertest from 'supertest'
import { v4 } from 'uuid'
import { createWorker } from '../src/queue-processor'
import { createApp } from '../src/server'
import { corsConfig } from '../src/utils/corsConfig'
const { app, apollo } = createApp()
export const request = supertest(app)
let worker: Worker | undefined
export const startApolloServer = async () => {
await apollo.start()
@ -16,6 +19,18 @@ export const stopApolloServer = async () => {
await apollo.stop()
}
export const startWorker = async (connection: ConnectionOptions) => {
worker = createWorker(connection)
}
export const stopWorker = async () => {
worker?.close()
}
export const waitUntilJobsDone = async () => {
await worker?.waitUntilReady()
}
export const graphqlRequest = (
query: string,
authToken: string,