Skip to content

Commit

Permalink
add new tests and fix old tests
Browse files Browse the repository at this point in the history
  • Loading branch information
il3ven committed Aug 6, 2022
1 parent 7e2f7e2 commit 95c05ff
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/worker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Queue from "better-queue";

import logger from "./logger.mjs";
import { messages } from "./api.mjs";
import { endpointStore, populateStore } from "./endpoint_store.mjs";
import { endpointStore, populateEndpointStore } from "./endpoint_store.mjs";

const log = logger("worker");

Expand Down Expand Up @@ -71,7 +71,8 @@ export function run() {
workerData.queue.options
)}`
);
populateStore(endpointStore, workerData.endpoints);
if (workerData.endpoints)
populateEndpointStore(endpointStore, workerData.endpoints);
const queue = new Queue(messages.route, workerData.queue.options);
queue.on("task_finish", loggingProxy(queue, reply));
queue.on("task_failed", loggingProxy(queue, panic));
Expand Down
42 changes: 41 additions & 1 deletion test/api_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test("sending a json-rpc request that times out", async (t) => {
await messages.route(message, cb);
});

test("sending an https request that times out", async (t) => {
test("sending an https request timeout through message", async (t) => {
const pauseMilliseconds = 1000;
const worker = await createWorker(
`
Expand Down Expand Up @@ -77,6 +77,46 @@ test("sending an https request that times out", async (t) => {
await messages.route(message, cb);
});

test.only("sending an https request timeout through config", async (t) => {
const pauseMilliseconds = 1000;
const worker = await createWorker(
`
app.get('/', async (req, res) => {
res.status(200).send("hello world");
});
`,
{ pauseMilliseconds }
);

const endpointStore = new Map();
endpointStore.set(`http://localhost:${worker.port}`, { timeout: 500 });

const { messages } = await esmock("../src/api.mjs", {
"../src/endpoint_store.mjs": {
endpointStore: endpointStore,
},
});

const message = {
type: "https",
version: messages.version,
options: {
url: `http://localhost:${worker.port}`,
method: "GET",
},
results: null,
error: null,
};

t.plan(3);
const cb = (err, response) => {
t.truthy(err);
t.true(err.error.includes("AbortError"));
t.falsy(response);
};
await messages.route(message, cb);
});

test("setting a timeout that isn't triggered", async (t) => {
const timeout = 1000;
const signal = AbortSignal.timeout(timeout);
Expand Down
23 changes: 23 additions & 0 deletions test/endpoint_store_test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//@format
import test from "ava";
import { RateLimiter } from "limiter";

import { populateEndpointStore } from "../src/endpoint_store.mjs";

test("should be able to populate endpoint store", async (t) => {
const store = new Map();

populateEndpointStore(store, {
"https://infura.io/": {
timeout: 500,
requestPerUnit: 50,
unit: "second",
},
});

const endpoint = store.get("https://infura.io");

t.truthy(endpoint);
t.assert(endpoint.rateLimiter instanceof RateLimiter);
t.is(endpoint.timeout, 500);
});

0 comments on commit 95c05ff

Please sign in to comment.