Skip to content
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
target
coverage
example
7 changes: 6 additions & 1 deletion lib/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ module.exports = async args => {

const url = 'http://127.0.0.1:' + checkPort + checkPath;
console.info('Request %s for health check', url);
const res = await urllib.request(url);
let res;
try {
res = await urllib.request(url);
} catch (_) {
return false;
}
const body = res.data.toString() || '';
// 匹配返回的 status
if (res.status !== expectedStatus) return false;
Expand Down
24 changes: 23 additions & 1 deletion test/check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ describe('test/check.test.js', () => {
beforeEach(() => mkdirp(releaseDir));
afterEach(() => rimraf(homeDir));
afterEach(done => {
server.close(done);
if (!server) done();
server.close(() => {
server = null;
done();
});
});

describe('check status', () => {
Expand Down Expand Up @@ -79,6 +83,24 @@ describe('test/check.test.js', () => {
.expect('code', 1)
.end();
});

});

describe('check without listen', () => {
beforeEach(async () => {
await fs.symlink(
path.join(__dirname, 'fixtures/check-unlisten'),
path.join(homeDir, 'release/run')
);
});

it('should fail', async () => {
await coffee.fork(checkBin)
.debug()
.expect('stdout', /Request http:\/\/127.0.0.1:7002\/healthy for health check/)
.expect('code', 1)
.end();
});
});

});
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/check-unlisten/.nodestack
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
health_check:
enable: true
port: 7002
path: /healthy
status: 200
body: healthy