Skip to content

Commit

Permalink
don't wait for probe when running tests (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
Clap404 authored Sep 18, 2024
1 parent 74fcb83 commit 7d3b595
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 54 deletions.
35 changes: 15 additions & 20 deletions e2e/src/full-sync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as matchers from "jest-extended";
import { dumpUploadData, logAxiosError } from "./helpers/log-helper";
import axios, { AxiosError } from "axios";
import { doFullSync, probe, PsEventbusSyncUpload } from "./helpers/mock-probe";
import { concatMap, from, lastValueFrom, map, toArray, zip } from "rxjs";
import { from, lastValueFrom, toArray, withLatestFrom } from "rxjs";
import {
generatePredictableModuleId,
loadFixture,
Expand Down Expand Up @@ -148,20 +148,14 @@ describe("Full Sync", () => {
}),
);

const results = await lastValueFrom(
zip(message$, request$).pipe(
map((result) => ({
probeMessage: result[0],
psEventbusReq: result[1],
})),
toArray(),
),
const probeMessage = await lastValueFrom(
request$.pipe(withLatestFrom(message$, (_, message) => message)),
);

// assert
expect(results.length).toEqual(1);
expect(results[0].probeMessage.method).toBe("POST");
expect(results[0].probeMessage.headers).toMatchObject({
expect(probeMessage).toBeTruthy();
expect(probeMessage.method).toBe("POST");
expect(probeMessage.headers).toMatchObject({
"full-sync-requested": "1",
});
});
Expand All @@ -172,20 +166,21 @@ describe("Full Sync", () => {
} else {
it(`${controller} should upload complete dataset collector`, async () => {
// arrange
const fullSync$ = doFullSync(jobId, controller, { timeout: 4000 });
const response$ = doFullSync(jobId, controller, { timeout: 4000 });
const message$ = probe({ url: `/upload/${jobId}` }, { timeout: 4000 });

// act
const syncedData = await lastValueFrom(
zip(fullSync$, message$).pipe(
map((msg) => msg[1].body.file),
concatMap((syncedPage) => {
return from(syncedPage);
}),
// this combines each response from ps_eventbus to the last request captured by the probe.
// it works because ps_eventbus sends a response after calling our mock collector server
// if ps_eventbus doesn't need to call the collector, the probe completes without value after its timeout
const messages = await lastValueFrom(
response$.pipe(
withLatestFrom(message$, (_, message) => message.body.file),
toArray(),
),
);

const syncedData: PsEventbusSyncUpload[] = messages.flat();

// dump data for easier debugging or updating fixtures
let processedData = syncedData as PsEventbusSyncUpload[];
if (testConfig.dumpFullSyncData) {
Expand Down
68 changes: 34 additions & 34 deletions e2e/src/helpers/mock-probe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,45 @@ import axios from "axios";
import { Controller } from "./controllers";

const DEFAULT_OPTIONS = {
timeout: 3000,
timeout: 500,
};

export type MockProbeOptions = typeof DEFAULT_OPTIONS;
export type MockClientOptions = typeof DEFAULT_OPTIONS;

export type PsEventbusSyncResponse = {
job_id: string;
object_type: string;
syncType: string; // 'full' | 'incremental'
total_objects: number; // may not always be accurate, can't be relied on
has_remaining_objects: boolean; // reliable
remaining_objects: number; // may not always be accurate, can't be relied on
md5: string;
status: boolean;
httpCode: number;
body: unknown; // not sure what this is
upload_url: string;
};

// TODO define collection as type literal
export type Collection = string;

export type PsEventbusSyncUpload = {
collection: Collection;
id: string;
properties: unknown;
};

export type MockProbeResponse = {
apiName: string;
method: string;
headers: Record<string, string>;
url: string;
query: Record<string, string>;
params: Record<string, string>;
body: Record<string, unknown> & { file: PsEventbusSyncUpload[] };
};

// no Websocket implementation seems to exist in jest runner
if (!global.WebSocket) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -29,16 +62,6 @@ function getProbeSocket(): Observable<MockProbeResponse> {
return wsConnection;
}

export type MockProbeResponse = {
apiName: string;
method: string;
headers: Record<string, string>;
url: string;
query: Record<string, string>;
params: Record<string, string>;
body: Record<string, unknown> & { file: unknown[] };
};

export function probe(
match?: Partial<MockProbeResponse>,
options?: MockProbeOptions,
Expand All @@ -52,29 +75,6 @@ export function probe(
);
}

export type PsEventbusSyncResponse = {
job_id: string;
object_type: string;
syncType: string; // 'full' | 'incremental'
total_objects: number; // may not always be accurate, can't be relied on
has_remaining_objects: boolean; // reliable
remaining_objects: number; // may not always be accurate, can't be relied on
md5: string;
status: boolean;
httpCode: number;
body: unknown; // not sure what this is
upload_url: string;
};

// TODO define collection as type literal
export type Collection = string;

export type PsEventbusSyncUpload = {
collection: Collection;
id: string;
properties: unknown;
};

export function doFullSync(
jobId: string,
controller: Controller,
Expand Down

0 comments on commit 7d3b595

Please sign in to comment.