Skip to content

Commit

Permalink
Merge pull request #1156 from serlo/parallel-tests
Browse files Browse the repository at this point in the history
test: Enable tests in parallel
  • Loading branch information
hugotiburtino authored Dec 1, 2023
2 parents fc12961 + d7aa261 commit 6606235
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 46 deletions.
3 changes: 1 addition & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ LOG_LEVEL=ERROR
MAILCHIMP_API_KEY=secret-us5
METADATA_API_VERSION=1.0.0
REDIS_URL=redis://127.0.0.1:6379
REDIS_URL_TEST=redis://127.0.0.1:6380
ROCKET_CHAT_API_USER_ID=an-user-id
ROCKET_CHAT_API_AUTH_TOKEN=an-auth-token
ROCKET_CHAT_URL=https://community.serlo.org/
Expand All @@ -29,4 +28,4 @@ ENMESHED_SERVER_HOST="http://localhost:8081/"
ENMESHED_SERVER_SECRET="apiKey"
ENMESHED_WEBHOOK_SECRET="webhookKey"

OPENAI_API_KEY=""
OPENAI_API_KEY=""
3 changes: 0 additions & 3 deletions __config__/jest.setup-pacts-serlo-org-database-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
createAfterEach,
createBeforeAll,
createBeforeEach,
setup,
} from './setup'
import { createAuthServices } from '~/internals/authentication'
import { emptySwrQueue } from '~/internals/swr-queue'
Expand All @@ -17,8 +16,6 @@ const pactDir = path.join(__dirname, '..', 'pacts')

const port = 9009

setup()

jest.setTimeout(60 * 1000)

global.pact = new Pact({
Expand Down
3 changes: 0 additions & 3 deletions __config__/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
createAfterEach,
createBeforeAll,
createBeforeEach,
setup,
} from './setup'

process.env.OPENAI_API_KEY = 'fake-test-key-we-are-mocking-responses'
Expand All @@ -26,8 +25,6 @@ jest.mock('@google-cloud/storage', () => {
}
})

setup()

beforeAll(async () => {
await createBeforeAll({
onUnhandledRequest(req) {
Expand Down
36 changes: 20 additions & 16 deletions __config__/setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import crypto from 'crypto'
import { http, HttpResponse } from 'msw'
import { SetupServer, setupServer } from 'msw/node'

Expand All @@ -6,7 +7,7 @@ import {
givenSpreadheetApi,
MockKratos,
} from '../__tests__/__utils__'
import { createCache } from '~/internals/cache'
import { createCache, createNamespacedCache } from '~/internals/cache'
import { initializeSentry, Sentry } from '~/internals/sentry'
import { Time, timeToMilliseconds } from '~/internals/swr-queue'
import { Timer } from '~/internals/timer'
Expand All @@ -33,33 +34,30 @@ export class MockTimer implements Timer {
}
}

export function setup() {
export function createBeforeAll(options: Parameters<SetupServer['listen']>[0]) {
initializeSentry({
dsn: 'https://public@127.0.0.1/0',
environment: 'testing',
context: 'testing',
})

const timer = new MockTimer()
const cache = createCache({ timer })
const server = setupServer()
const kratos = new MockKratos()

global.cache = cache
global.server = server
global.timer = timer
global.kratos = kratos
}

export async function createBeforeAll(
options: Parameters<SetupServer['listen']>[0],
) {
await global.cache.ready()

global.server.listen(options)
}

export async function createBeforeEach() {
const baseCache = createCache({ timer: global.timer })
global.cache = createNamespacedCache(baseCache, generateRandomString(10))

await global.cache.ready()

givenSpreadheetApi(defaultSpreadsheetApi())

global.server.use(
Expand All @@ -72,22 +70,28 @@ export async function createBeforeEach() {
}),
)

await global.cache.flush()
global.timer.flush()
global.sentryEvents = []
global.kratos.identities = []

process.env.ENVIRONMENT = 'local'
}

export function createAfterEach() {
export async function createAfterEach() {
global.server.resetHandlers()
}

export async function createAfterAll() {
global.server.close()
await global.cache.quit()
// redis.quit() creates a thread to close the connection.
// We wait until all threads have been run once to ensure the connection closes.
await new Promise((resolve) => setImmediate(resolve))
}

export function createAfterAll() {
global.server.close()
}

function generateRandomString(length: number) {
return crypto
.randomBytes(Math.ceil(length / 2))
.toString('hex')
.slice(0, length)
}
5 changes: 0 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,3 @@ services:
image: redis:6.0
ports:
- '6379:6379'

redis-test:
image: redis:6.0
ports:
- '6380:6379'
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"stop": "docker compose stop",
"stop:enmeshed": "docker-compose -f enmeshed/docker-compose.yml down",
"stop:redis": "docker-compose stop",
"test": "jest --config jest.config.cjs --runInBand --forceExit",
"test": "jest --config jest.config.cjs --forceExit",
"test:docker:server": "curl --verbose -H 'Content-type: application/json' --data '{\"query\": \"query { version }\" }' http://localhost:3001/graphql",
"test:docker:swr-queue-worker": "curl --verbose http://localhost:3030/.well-known/health | grep OK",
"update-version": "./scripts/update_version.sh"
Expand Down
26 changes: 24 additions & 2 deletions packages/server/src/internals/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as R from 'ramda'

import { createLockManager, LockManager } from './lock-manager'
import { log } from '../log'
import { redisUrl } from '../redis-url'
import { Time, timeToMilliseconds } from '../swr-queue'
import { Timer } from '../timer'
import { AsyncOrSync } from '~/utils'
Expand Down Expand Up @@ -45,7 +44,7 @@ export interface Cache {
}

export function createCache({ timer }: { timer: Timer }): Cache {
const client = new Redis(redisUrl)
const client = new Redis(process.env.REDIS_URL)
const lockManagers: Record<Priority, LockManager> = {
[Priority.Low]: createLockManager({
retryCount: 0,
Expand Down Expand Up @@ -167,6 +166,29 @@ export function createEmptyCache(): Cache {
}
}

export function createNamespacedCache(cache: Cache, namespace: string): Cache {
return {
get(args) {
return cache.get({ ...args, key: namespace + args.key })
},
set(args) {
return cache.set({ ...args, key: namespace + args.key })
},
remove(args) {
return cache.remove({ ...args, key: namespace + args.key })
},
ready() {
return cache.ready()
},
flush() {
return cache.flush()
},
quit() {
return cache.quit()
},
}
}

export interface CacheEntry<Value> {
value: Value
lastModified: number
Expand Down
3 changes: 1 addition & 2 deletions packages/server/src/internals/cache/lock-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Redis from 'ioredis'
import Redlock from 'redlock'

import { log } from '../log'
import { redisUrl } from '../redis-url'

export interface LockManager {
lock(key: string): Promise<Lock>
Expand All @@ -18,7 +17,7 @@ export function createLockManager({
}: {
retryCount: number
}): LockManager {
const client = new Redis(redisUrl)
const client = new Redis(process.env.REDIS_URL)
const redlock = new Redlock([client], { retryCount })

redlock.on('clientError', function (err) {
Expand Down
3 changes: 0 additions & 3 deletions packages/server/src/internals/redis-url.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Bee from 'bee-queue'
import createMiddleware from 'bull-arena'
import { Express, RequestHandler } from 'express'

import { redisUrl } from '~/internals/redis-url'
import { queueName } from '~/internals/swr-queue'

export function applySwrQueueDashboardMiddleware({ app }: { app: Express }) {
Expand Down Expand Up @@ -42,7 +41,7 @@ function createDashboardMiddleware(): RequestHandler {
name: queueName,
hostId: 'SWR Queue',
type: 'bee',
url: redisUrl,
url: process.env.REDIS_URL,
},
],
},
Expand Down
9 changes: 2 additions & 7 deletions packages/server/src/internals/swr-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Cache, CacheEntry, Priority } from './cache'
import { isQuery, QuerySpec } from './data-source-helper'
import { captureErrorEvent } from './error-event'
import { log } from './log'
import { redisUrl } from './redis-url'
import { Timer } from './timer'
import { modelFactories } from '~/model'

Expand Down Expand Up @@ -67,9 +66,7 @@ export function createSwrQueue({
)

const queue = new Queue<UpdateJob>(queueName, {
redis: {
url: redisUrl,
},
redis: { url: process.env.REDIS_URL },
isWorker: false,
removeOnFailure: true,
removeOnSuccess: true,
Expand Down Expand Up @@ -162,9 +159,7 @@ export function createSwrQueueWorker({
)

const queue = new Queue<UpdateJob>(queueName, {
redis: {
url: redisUrl,
},
redis: { url: process.env.REDIS_URL },
removeOnFailure: true,
removeOnSuccess: true,
})
Expand Down

0 comments on commit 6606235

Please sign in to comment.