Skip to content

Add tests for last-activity updates #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/testutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ var addTarget = (exports.addTarget = function (proxy, path, port, websocket, tar

server.listen(port);
servers.push(server);
return proxy.addRoute(path, { target: target });
return proxy.addRoute(path, { target: target }).then(() => {
// routes are created with an activity timestamp artificially shifted into the past
// so that activity can more easily be measured
return proxy._routes.update(path, { last_activity: proxy._setup_timestamp });
});
});

var addTargetRedirecting = (exports.addTargetRedirecting = function (
Expand Down Expand Up @@ -90,6 +94,7 @@ exports.setupProxy = function (port, options, paths) {
options.log = defaultLogger({ level: "error" });

var proxy = new configproxy.ConfigurableProxy(options);
proxy._setup_timestamp = new Date(new Date().getTime() - 60000);
var ip = "127.0.0.1";
var countdown = 2;
var resolvePromise;
Expand Down
59 changes: 56 additions & 3 deletions test/proxy_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ describe("Proxy Tests", function () {
path: "/",
})
);
done();

// check last_activity was updated
return proxy._routes.get("/").then((route) => {
expect(route.last_activity).toBeGreaterThan(proxy._setup_timestamp);
done();
});
});
});

Expand All @@ -66,8 +71,12 @@ describe("Proxy Tests", function () {
message: "hi",
})
);
ws.close();
done();
// check last_activity was updated
return proxy._routes.get("/").then((route) => {
expect(route.last_activity).toBeGreaterThan(proxy._setup_timestamp);
ws.close();
done();
});
}
nmsgs++;
});
Expand Down Expand Up @@ -269,6 +278,50 @@ describe("Proxy Tests", function () {
.then(done);
});

it("last_activity not updated on errors", function (done) {
let now = new Date();
// mock timestamp in the past
let firstActivity = new Date(now.getTime() - 60000);

function expectNoActivity() {
return proxy._routes.get("/missing", (route) => {
expect(route.last_activity).toEqual(proxy._setup_timestamp);
});
}

proxy
.removeRoute("/")
// add a route to nowhere
.then(() => proxy.addRoute("/missing", { target: "https://127.0.0.1:54321" }))
.then(() => {
// set last_activity into the past
proxy._routes.update("/missing", { last_activity: firstActivity });
})
// fail a web request
.then(() => r(hostUrl + "/missing/prefix"))
.then((body) => done.fail("Expected 503"))
.catch((err) => {
expect(err.statusCode).toEqual(503);
})
// check that activity was not updated
.then(expectNoActivity)
// fail a websocket request
.then(() => {
var ws = new WebSocket("ws://127.0.0.1:" + port + "/missing/ws");
ws.on("error", () => {
// expect this, since there is no websocket handler
// check last_activity was not updated
expectNoActivity().then((route) => {
ws.close();
done();
});
});
ws.on("open", () => {
done.fail("Expected websocket error");
});
});
});

it("custom error target", function (done) {
var proxyPort = 55550;
util
Expand Down