Skip to content

Commit

Permalink
add waitForJob
Browse files Browse the repository at this point in the history
  • Loading branch information
tonylee80 committed Jun 28, 2023
1 parent b7009f8 commit cc10fa8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/server/src/fhir/operations/export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import express from 'express';
import request from 'supertest';
import { initApp, shutdownApp } from '../../app';
import { loadTestConfig } from '../../config';
import { createTestProject, initTestAuth, waitFor } from '../../test.setup';
import { createTestProject, initTestAuth, waitFor, waitForJob } from '../../test.setup';
import { systemRepo } from '../repo';
import { exportResourceType } from './export';
import { BulkExporter } from './utils/bulkexporter';

jest.mock('../../logger');

const app = express();

describe('Export', () => {
Expand Down Expand Up @@ -113,6 +111,7 @@ describe('Export', () => {
.send({});
expect(initRes.status).toBe(202);
expect(initRes.headers['content-location']).toBeDefined();
await waitForJob(app, initRes.headers['content-location'], accessToken);
});

test('Patient Export Accepted with GET', async () => {
Expand All @@ -127,6 +126,7 @@ describe('Export', () => {
.send({});
expect(initRes.status).toBe(202);
expect(initRes.headers['content-location']).toBeDefined();
await waitForJob(app, initRes.headers['content-location'], accessToken);
});

test('exportResourceType iterating through paginated search results', async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/fhir/operations/groupexport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import express from 'express';
import request from 'supertest';
import { initApp, shutdownApp } from '../../app';
import { loadTestConfig } from '../../config';
import { createTestProject, initTestAuth, waitFor } from '../../test.setup';
import { createTestProject, initTestAuth, waitFor, waitForJob } from '../../test.setup';
import { systemRepo } from '../repo';
import { groupExportResources } from './groupexport';
import { BulkExporter } from './utils/bulkexporter';
Expand Down Expand Up @@ -316,6 +316,7 @@ describe('Group Export', () => {
.set('Authorization', 'Bearer ' + accessToken);
expect(res4.status).toBe(202);
expect(res4.headers['content-location']).toBeDefined();
await waitForJob(app, res4.headers['content-location'], accessToken);
});

test('groupExportResources without members', async () => {
Expand Down
20 changes: 20 additions & 0 deletions packages/server/src/test.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
User,
} from '@medplum/fhirtypes';
import { randomUUID } from 'crypto';
import { Express } from 'express';
import request from 'supertest';
import { inviteUser } from './admin/invite';
import { systemRepo } from './fhir/repo';
import { generateAccessToken } from './oauth/keys';
Expand Down Expand Up @@ -183,3 +185,21 @@ export function waitFor(fn: () => Promise<void>): Promise<void> {
}, 100);
});
}

export async function waitForJob(app: Express, contentLocation: string, accessToken: string): Promise<void> {
for (let i = 0; i < 10; i++) {
const res = await request(app)
.get(new URL(contentLocation).pathname)
.set('Authorization', 'Bearer ' + accessToken);
if (res.status !== 202) {
return;
}
await sleep(1000);
}
throw new Error('Job did not complete');
}

const sleep = (ms: number): Promise<void> =>
new Promise((resolve) => {
setTimeout(resolve, ms);
});

0 comments on commit cc10fa8

Please sign in to comment.