Skip to content

Commit cc10fa8

Browse files
committed
add waitForJob
1 parent b7009f8 commit cc10fa8

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

packages/server/src/fhir/operations/export.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import express from 'express';
44
import request from 'supertest';
55
import { initApp, shutdownApp } from '../../app';
66
import { loadTestConfig } from '../../config';
7-
import { createTestProject, initTestAuth, waitFor } from '../../test.setup';
7+
import { createTestProject, initTestAuth, waitFor, waitForJob } from '../../test.setup';
88
import { systemRepo } from '../repo';
99
import { exportResourceType } from './export';
1010
import { BulkExporter } from './utils/bulkexporter';
1111

12-
jest.mock('../../logger');
13-
1412
const app = express();
1513

1614
describe('Export', () => {
@@ -113,6 +111,7 @@ describe('Export', () => {
113111
.send({});
114112
expect(initRes.status).toBe(202);
115113
expect(initRes.headers['content-location']).toBeDefined();
114+
await waitForJob(app, initRes.headers['content-location'], accessToken);
116115
});
117116

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

132132
test('exportResourceType iterating through paginated search results', async () => {

packages/server/src/fhir/operations/groupexport.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import express from 'express';
33
import request from 'supertest';
44
import { initApp, shutdownApp } from '../../app';
55
import { loadTestConfig } from '../../config';
6-
import { createTestProject, initTestAuth, waitFor } from '../../test.setup';
6+
import { createTestProject, initTestAuth, waitFor, waitForJob } from '../../test.setup';
77
import { systemRepo } from '../repo';
88
import { groupExportResources } from './groupexport';
99
import { BulkExporter } from './utils/bulkexporter';
@@ -316,6 +316,7 @@ describe('Group Export', () => {
316316
.set('Authorization', 'Bearer ' + accessToken);
317317
expect(res4.status).toBe(202);
318318
expect(res4.headers['content-location']).toBeDefined();
319+
await waitForJob(app, res4.headers['content-location'], accessToken);
319320
});
320321

321322
test('groupExportResources without members', async () => {

packages/server/src/test.setup.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
User,
1111
} from '@medplum/fhirtypes';
1212
import { randomUUID } from 'crypto';
13+
import { Express } from 'express';
14+
import request from 'supertest';
1315
import { inviteUser } from './admin/invite';
1416
import { systemRepo } from './fhir/repo';
1517
import { generateAccessToken } from './oauth/keys';
@@ -183,3 +185,21 @@ export function waitFor(fn: () => Promise<void>): Promise<void> {
183185
}, 100);
184186
});
185187
}
188+
189+
export async function waitForJob(app: Express, contentLocation: string, accessToken: string): Promise<void> {
190+
for (let i = 0; i < 10; i++) {
191+
const res = await request(app)
192+
.get(new URL(contentLocation).pathname)
193+
.set('Authorization', 'Bearer ' + accessToken);
194+
if (res.status !== 202) {
195+
return;
196+
}
197+
await sleep(1000);
198+
}
199+
throw new Error('Job did not complete');
200+
}
201+
202+
const sleep = (ms: number): Promise<void> =>
203+
new Promise((resolve) => {
204+
setTimeout(resolve, ms);
205+
});

0 commit comments

Comments
 (0)