don't throttle the integration test

This commit is contained in:
Florian Dold 2019-12-15 17:48:22 +01:00
parent 985c90e0ad
commit 7cc3b10824
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
6 changed files with 120 additions and 82 deletions

View File

@ -22,7 +22,6 @@ import {
getDefaultNodeWallet,
withdrawTestBalance,
DefaultNodeWalletArgs,
NodeHttpLib,
} from "../headless/helpers";
import { openPromise, OpenedPromise } from "../util/promiseUtils";
import fs = require("fs");
@ -32,6 +31,7 @@ import {
HttpRequestOptions,
Headers,
} from "../util/http";
import { NodeHttpLib } from "../headless/NodeHttpLib";
// @ts-ignore: special built-in module
//import akono = require("akono");

102
src/headless/NodeHttpLib.ts Normal file
View File

@ -0,0 +1,102 @@
/*
This file is part of GNU Taler
(C) 2019 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
SPDX-License-Identifier: AGPL3.0-or-later
*/
import { Headers, HttpRequestLibrary, HttpRequestOptions, HttpResponse } from "../util/http";
import { RequestThrottler } from "../util/RequestThrottler";
import Axios, { AxiosResponse } from "axios";
/**
* Implementation of the HTTP request library interface for node.
*/
export class NodeHttpLib implements HttpRequestLibrary {
private throttle = new RequestThrottler();
private throttlingEnabled = true;
/**
* Set whether requests should be throttled.
*/
setThrottling(enabled: boolean) {
this.throttlingEnabled = enabled;
}
private async req(
method: "post" | "get",
url: string,
body: any,
opt?: HttpRequestOptions,
): Promise<HttpResponse> {
if (this.throttlingEnabled && this.throttle.applyThrottle(url)) {
throw Error("request throttled");
}
let resp: AxiosResponse;
try {
resp = await Axios({
method,
url: url,
responseType: "text",
headers: opt?.headers,
validateStatus: () => true,
transformResponse: (x) => x,
data: body,
});
} catch (e) {
throw e;
}
const respText = resp.data;
if (typeof respText !== "string") {
throw Error("unexpected response type");
}
const makeJson = async () => {
let responseJson;
try {
responseJson = JSON.parse(respText);
} catch (e) {
throw Error("Invalid JSON from HTTP response");
}
if (responseJson === null || typeof responseJson !== "object") {
throw Error("Invalid JSON from HTTP response");
}
return responseJson;
};
const headers = new Headers();
for (const hn of Object.keys(resp.headers)) {
headers.set(hn, resp.headers[hn]);
}
return {
headers,
status: resp.status,
text: async () => resp.data,
json: makeJson,
};
}
async get(
url: string,
opt?: HttpRequestOptions,
): Promise<HttpResponse> {
return this.req("get", url, undefined, opt);
}
async postJson(
url: string,
body: any,
opt?: HttpRequestOptions,
): Promise<HttpResponse> {
return this.req("post", url, body, opt);
}
}

View File

@ -1,6 +1,6 @@
/*
This file is part of GNU Taler
(C) 2019 GNUnet e.V.
(C) 2019 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
@ -16,6 +16,7 @@
/**
* Helpers to create headless wallets.
* @author Florian Dold <dold@taler.net>
*/
/**
@ -24,94 +25,20 @@
import { Wallet } from "../wallet";
import { MemoryBackend, BridgeIDBFactory, shimIndexedDB } from "idb-bridge";
import { openTalerDatabase } from "../db";
import Axios, { AxiosPromise, AxiosResponse } from "axios";
import {
HttpRequestLibrary,
HttpRequestOptions,
Headers,
} from "../util/http";
import * as amounts from "../util/amounts";
import { Bank } from "./bank";
import fs = require("fs");
import { Logger } from "../util/logging";
import { NodeThreadCryptoWorkerFactory } from "../crypto/workers/nodeThreadWorker";
import { SynchronousCryptoWorkerFactory } from "../crypto/workers/synchronousWorker";
import { RequestThrottler } from "../util/RequestThrottler";
import { WalletNotification, NotificationType } from "../types/notifications";
import { Database } from "../util/query";
import { NodeHttpLib } from "./NodeHttpLib";
import { Logger } from "../util/logging";
const logger = new Logger("helpers.ts");
export class NodeHttpLib implements HttpRequestLibrary {
private throttle = new RequestThrottler();
private async req(
method: "post" | "get",
url: string,
body: any,
opt?: HttpRequestOptions,
) {
if (this.throttle.applyThrottle(url)) {
throw Error("request throttled");
}
let resp: AxiosResponse;
try {
resp = await Axios({
method,
url: url,
responseType: "text",
headers: opt?.headers,
validateStatus: () => true,
transformResponse: (x) => x,
data: body,
});
} catch (e) {
throw e;
}
const respText = resp.data;
if (typeof respText !== "string") {
throw Error("unexpected response type");
}
const makeJson = async () => {
let responseJson;
try {
responseJson = JSON.parse(respText);
} catch (e) {
throw Error("Invalid JSON from HTTP response");
}
if (responseJson === null || typeof responseJson !== "object") {
throw Error("Invalid JSON from HTTP response");
}
return responseJson;
};
const headers = new Headers();
for (const hn of Object.keys(resp.headers)) {
headers.set(hn, resp.headers[hn]);
}
return {
headers,
status: resp.status,
text: async () => resp.data,
json: makeJson,
};
}
async get(
url: string,
opt?: HttpRequestOptions,
): Promise<import("../util/http").HttpResponse> {
return this.req("get", url, undefined, opt);
}
async postJson(
url: string,
body: any,
opt?: HttpRequestOptions,
): Promise<import("../util/http").HttpResponse> {
return this.req("post", url, body, opt);
}
}
export interface DefaultNodeWalletArgs {
/**

View File

@ -20,8 +20,10 @@
import { getDefaultNodeWallet, withdrawTestBalance } from "./helpers";
import { MerchantBackendConnection } from "./merchant";
import { Logger } from "../util/logging";
import { NodeHttpLib } from "./NodeHttpLib";
const enableTracing = false;
const logger = new Logger("integrationtest.ts");
export async function runIntegrationTest(args: {
exchangeBaseUrl: string;
@ -31,10 +33,16 @@ export async function runIntegrationTest(args: {
amountToWithdraw: string;
amountToSpend: string;
}) {
console.log("running test with", args);
const myWallet = await getDefaultNodeWallet();
logger.info("running test with arguments", args);
const myHttpLib = new NodeHttpLib();
myHttpLib.setThrottling(false);
const myWallet = await getDefaultNodeWallet({ httpLib: myHttpLib });
logger.info("withdrawing test balance");
await withdrawTestBalance(myWallet, args.amountToWithdraw, args.bankBaseUrl, args.exchangeBaseUrl);
logger.info("done withdrawing test balance");
const balance = await myWallet.getBalances();

View File

@ -423,7 +423,7 @@ testCli
merchantApiKey: cmdObj.merchantApiKey,
merchantBaseUrl: cmdObj.merchant,
}).catch(err => {
console.error("Failed with exception:");
console.error("Integration test failed with exception:");
console.error(err);
});

View File

@ -37,6 +37,7 @@
"src/crypto/workers/nodeThreadWorker.ts",
"src/crypto/workers/synchronousWorker.ts",
"src/db.ts",
"src/headless/NodeHttpLib.ts",
"src/headless/bank.ts",
"src/headless/clk.ts",
"src/headless/helpers.ts",