Skip to content

Commit

Permalink
Improve dump tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bidoubiwa committed Nov 9, 2020
1 parent 0383498 commit 6383477
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
17 changes: 9 additions & 8 deletions tests/dump_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
privateClient,
publicClient,
anonymousClient,
waitForDumpProcessing,
} from './meilisearch-test-utils'

beforeAll(async () => {
Expand All @@ -17,18 +18,18 @@ describe.each([
{ client: privateClient, permission: 'Private' },
])('Test on dump', ({ client, permission }) => {
test(`${permission} key: create a new dump`, async () => {
await client.createDump().then((response) => {
expect(response.uid).toBeDefined()
expect(response.status).toEqual('in_progress')
})
const response = await client.createDump()
expect(response.uid).toBeDefined()
expect(response.status).toEqual('in_progress')
await waitForDumpProcessing(response.uid, client)
})

test(`${permission} key: get dump status`, async () => {
const enqueuedDump = await client.createDump()
await client.getDumpStatus(enqueuedDump.uid).then((response) => {
expect(response.uid).toEqual(enqueuedDump.uid)
expect(response.status).toBeDefined()
})
await waitForDumpProcessing(enqueuedDump.uid, client)
const response = await client.getDumpStatus(enqueuedDump.uid)
expect(response.uid).toEqual(enqueuedDump.uid)
expect(response.status).toBeDefined()
})
})

Expand Down
21 changes: 21 additions & 0 deletions tests/meilisearch-test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import MeiliSearch from '../src/meilisearch'
import * as Types from '../src/types'
import { sleep } from '../src/utils'

// testing
const MASTER_KEY = 'masterKey'
Expand Down Expand Up @@ -49,6 +50,25 @@ const clearAllIndexes = async (config: Types.Config): Promise<void> => {
await expect(client.listIndexes()).resolves.toHaveLength(0)
}

async function waitForDumpProcessing(
dumpId: string,
client: MeiliSearch,
{
timeOutMs = 5000,
intervalMs = 50,
}: { timeOutMs?: number; intervalMs?: number } = {}
): Promise<Types.EnqueuedDump> {
const startingTime = Date.now()
while (Date.now() - startingTime < timeOutMs) {
const response = await client.getDumpStatus(dumpId)
if (response.status !== 'in_progress') return response
await sleep(intervalMs)
}
throw new Types.MeiliSearchTimeOutError(
`timeout of ${timeOutMs}ms has exceeded on process ${dumpId} when waiting for pending update to resolve.`
)
}

export {
clearAllIndexes,
config,
Expand All @@ -60,4 +80,5 @@ export {
PRIVATE_KEY,
MASTER_KEY,
MeiliSearch,
waitForDumpProcessing,
}

0 comments on commit 6383477

Please sign in to comment.