Skip to content

Commit

Permalink
update http server test
Browse files Browse the repository at this point in the history
  • Loading branch information
eladb committed Sep 27, 2023
1 parent 8eca853 commit 4784021
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 46 deletions.
31 changes: 12 additions & 19 deletions examples/tests/sdk_tests/service/http-server.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
const http = require("http");

class HttpServer {
startServer(body) {
return new Promise((resolve, reject) => {
this.server = http.createServer();
this.server.on("request", (_, res) => res.end(body));
this.server.on("error", reject);
this.server.on("listening", () => resolve(this.server.address().port));
this.server.listen();
});
}

stopServer() {
return new Promise((resolve, _) => {
this.server.close(resolve);
});
}
}

module.exports = new HttpServer();
exports.createServer = async function(body) {
return new Promise((resolve, reject) => {
const server = http.createServer();
server.on("request", (_, res) => res.end(body));
server.on("error", reject);
server.on("listening", () => resolve({
address: () => server.address(),
close: () => new Promise((resolve) => server.close(resolve)),
}));
server.listen();
});
};
72 changes: 72 additions & 0 deletions examples/tests/sdk_tests/service/http-server.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
bring cloud;
bring util;
bring http;

struct Address {
port: num;
}

interface IHttpServer {
inflight address(): Address;
inflight close(): void;
}


// hack: only supported in the "sim" target for now
if util.env("WING_TARGET") == "sim" {
class MyService {
b: cloud.Bucket;
body: str;

pub s: cloud.Service;

init(body: str) {
this.b = new cloud.Bucket();
this.body = body;

// it's idiomatic to just pass `this` here and implement the callbacks on the current object.
this.s = new cloud.Service(inflight () => {
log("starting service");
let server = MyService.createServer(this.body);
let port = server.address().port;
log("listening on port ${port}");
this.b.put("port", "${port}");

return () => {
log("closing server...");
server.close();
};
});
}

pub inflight port(): num {
return num.fromStr(this.b.get("port"));
}

extern "./http-server.js" static inflight createServer(body: str): IHttpServer;
}

let foo = new MyService("bang bang!");

test "http server is started with the service" {
let response = http.get("http://localhost:${foo.port()}");
log(response.body ?? "");
assert(response.body ?? "" == "bang bang!");
}

test "service.stop() closes the http server" {
let before = http.get("http://localhost:${foo.port()}");
assert(before.ok);

foo.s.stop();

// now the http server is expected to be closed
let var error = false;
try {
http.get("http://localhost:${foo.port()}");
} catch {
error = true;
}
assert(error);
}
}
27 changes: 0 additions & 27 deletions examples/tests/sdk_tests/service/stateful.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@ if util.env("WING_TARGET") == "sim" {
log("starting service");
util.sleep(1s);
this.b.put("ready", "true");
let port = MyService.startServer(this.body);
log("listening on port ${port}");
let state = 456;
this.b.put("port", "${port}");

return () => {
log("stopping service");
log("state is: ${state}");

// make sure inflight state is presistent across onStart/onStop
assert(state == 456);
MyService.stopServer();
};
});
}
Expand All @@ -43,35 +39,12 @@ if util.env("WING_TARGET") == "sim" {
pub inflight port(): num {
return num.fromStr(this.b.get("port"));
}

extern "./http-server.js" static inflight startServer(body: str): num;
extern "./http-server.js" static inflight stopServer();
}

let foo = new MyService("bang bang!");

// see https://github.com/winglang/wing/issues/4251
test "service is ready only after onStart finishes" {
foo.access();

let response = http.get("http://localhost:${foo.port()}");
log(response.body ?? "");
assert(response.body ?? "" == "bang bang!");
}

test "service.stop() can be used to stop the service before shutdown" {
let before = http.get("http://localhost:${foo.port()}");
assert(before.ok);

foo.s.stop();

// now the http server is expected to be closed
let var error = false;
try {
http.get("http://localhost:${foo.port()}");
} catch {
error = true;
}
assert(error);
}
}

0 comments on commit 4784021

Please sign in to comment.