allow fault injection callbacks to run async code
This commit is contained in:
parent
48fa2ccea1
commit
883637d3f2
@ -28,17 +28,10 @@ import { URL } from "url";
|
||||
import {
|
||||
GlobalTestState,
|
||||
ExchangeService,
|
||||
BankService,
|
||||
ExchangeServiceInterface,
|
||||
MerchantServiceInterface,
|
||||
MerchantService,
|
||||
PrivateOrderStatusQuery,
|
||||
} from "./harness";
|
||||
import {
|
||||
PostOrderRequest,
|
||||
PostOrderResponse,
|
||||
MerchantOrderPrivateStatusResponse,
|
||||
} from "./merchantApiTypes";
|
||||
|
||||
export interface FaultProxyConfig {
|
||||
inboundPort: number;
|
||||
@ -65,8 +58,8 @@ export interface FaultInjectionResponseContext {
|
||||
}
|
||||
|
||||
export interface FaultSpec {
|
||||
modifyRequest?: (ctx: FaultInjectionRequestContext) => void;
|
||||
modifyResponse?: (ctx: FaultInjectionResponseContext) => void;
|
||||
modifyRequest?: (ctx: FaultInjectionRequestContext) => Promise<void>;
|
||||
modifyResponse?: (ctx: FaultInjectionResponseContext) => Promise<void>;
|
||||
}
|
||||
|
||||
export class FaultProxy {
|
||||
@ -85,7 +78,7 @@ export class FaultProxy {
|
||||
req.on("data", (chunk) => {
|
||||
requestChunks.push(chunk);
|
||||
});
|
||||
req.on("end", () => {
|
||||
req.on("end", async () => {
|
||||
console.log("end of data");
|
||||
let requestBuffer: Buffer | undefined;
|
||||
if (requestChunks.length > 0) {
|
||||
@ -103,7 +96,7 @@ export class FaultProxy {
|
||||
|
||||
for (const faultSpec of this.currentFaultSpecs) {
|
||||
if (faultSpec.modifyRequest) {
|
||||
faultSpec.modifyRequest(faultReqContext);
|
||||
await faultSpec.modifyRequest(faultReqContext);
|
||||
}
|
||||
}
|
||||
|
||||
@ -138,7 +131,7 @@ export class FaultProxy {
|
||||
proxyResp.on("data", (proxyRespData) => {
|
||||
respChunks.push(proxyRespData);
|
||||
});
|
||||
proxyResp.on("end", () => {
|
||||
proxyResp.on("end", async () => {
|
||||
console.log("end of target response");
|
||||
let responseBuffer: Buffer | undefined;
|
||||
if (respChunks.length > 0) {
|
||||
@ -154,7 +147,7 @@ export class FaultProxy {
|
||||
for (const faultSpec of this.currentFaultSpecs) {
|
||||
const modResponse = faultSpec.modifyResponse;
|
||||
if (modResponse) {
|
||||
modResponse(faultRespContext);
|
||||
await modResponse(faultRespContext);
|
||||
}
|
||||
}
|
||||
if (faultRespContext.dropResponse) {
|
||||
|
@ -155,7 +155,7 @@ export async function runExchangeManagementTest(t: GlobalTestState) {
|
||||
t.assertTrue(exchangesList.exchanges.length === 0);
|
||||
|
||||
faultyExchange.faultProxy.addFault({
|
||||
modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||
const url = new URL(ctx.request.requestUrl);
|
||||
if (url.pathname === "/keys") {
|
||||
const body = {
|
||||
@ -193,7 +193,7 @@ export async function runExchangeManagementTest(t: GlobalTestState) {
|
||||
faultyExchange.faultProxy.clearAllFaults();
|
||||
|
||||
faultyExchange.faultProxy.addFault({
|
||||
modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||
const url = new URL(ctx.request.requestUrl);
|
||||
if (url.pathname === "/keys") {
|
||||
const keys = ctx.responseBody?.toString("utf-8");
|
||||
|
@ -91,7 +91,7 @@ export async function runPayAbortTest(t: GlobalTestState) {
|
||||
let firstDepositUrl: string | undefined;
|
||||
|
||||
faultyExchange.faultProxy.addFault({
|
||||
modifyRequest(ctx: FaultInjectionRequestContext) {
|
||||
async modifyRequest(ctx: FaultInjectionRequestContext) {
|
||||
const url = new URL(ctx.requestUrl);
|
||||
if (url.pathname.endsWith("/deposit")) {
|
||||
if (!firstDepositUrl) {
|
||||
@ -104,7 +104,7 @@ export async function runPayAbortTest(t: GlobalTestState) {
|
||||
}
|
||||
}
|
||||
},
|
||||
modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||
const url = new URL(ctx.request.requestUrl);
|
||||
if (url.pathname.endsWith("/deposit") && url.href != firstDepositUrl) {
|
||||
ctx.responseBody = Buffer.from("{}");
|
||||
@ -114,7 +114,7 @@ export async function runPayAbortTest(t: GlobalTestState) {
|
||||
});
|
||||
|
||||
faultyMerchant.faultProxy.addFault({
|
||||
modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||
const url = new URL(ctx.request.requestUrl);
|
||||
if (url.pathname.endsWith("/pay") && url.href != firstDepositUrl) {
|
||||
ctx.responseBody = Buffer.from("{}");
|
||||
|
@ -167,7 +167,7 @@ export async function runPayPaidTest(t: GlobalTestState) {
|
||||
let numPaidRequested = 0;
|
||||
|
||||
faultyMerchant.faultProxy.addFault({
|
||||
modifyRequest(ctx: FaultInjectionRequestContext) {
|
||||
async modifyRequest(ctx: FaultInjectionRequestContext) {
|
||||
const url = new URL(ctx.requestUrl);
|
||||
if (url.pathname.endsWith("/pay")) {
|
||||
numPayRequested++;
|
||||
|
@ -83,10 +83,10 @@ export async function runPaymentFaultTest(t: GlobalTestState) {
|
||||
|
||||
// Print all requests to the exchange
|
||||
faultyExchange.faultProxy.addFault({
|
||||
modifyRequest(ctx: FaultInjectionRequestContext) {
|
||||
async modifyRequest(ctx: FaultInjectionRequestContext) {
|
||||
console.log("got request", ctx);
|
||||
},
|
||||
modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||
console.log("got response", ctx);
|
||||
},
|
||||
});
|
||||
@ -180,7 +180,7 @@ export async function runPaymentFaultTest(t: GlobalTestState) {
|
||||
// Drop 3 responses from the exchange.
|
||||
let faultCount = 0;
|
||||
faultyExchange.faultProxy.addFault({
|
||||
modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||
if (!ctx.request.requestUrl.endsWith("/deposit")) {
|
||||
return;
|
||||
}
|
||||
|
@ -116,7 +116,7 @@ export async function runPaymentTransientTest(t: GlobalTestState) {
|
||||
let faultInjected = false;
|
||||
|
||||
faultyMerchant.faultProxy.addFault({
|
||||
modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||
console.log("in modifyResponse");
|
||||
const url = new URL(ctx.request.requestUrl);
|
||||
console.log("pathname is", url.pathname);
|
||||
|
Loading…
Reference in New Issue
Block a user