From 5b85f4f2b782e778dbb266e36152da39c78a556d Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Fri, 19 Oct 2018 11:21:45 -0700 Subject: [PATCH] Native: add Server#bindAsync --- packages/grpc-native-core/src/server.js | 29 ++++++++++++++++++++ packages/grpc-native-core/test/async_test.js | 14 ++++++---- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/packages/grpc-native-core/src/server.js b/packages/grpc-native-core/src/server.js index b238de04..34c123d1 100644 --- a/packages/grpc-native-core/src/server.js +++ b/packages/grpc-native-core/src/server.js @@ -954,6 +954,7 @@ Server.prototype.addProtoService = util.deprecate(function(service, * "address:port" * @param {grpc.ServerCredentials} creds Server credential object to be used for * SSL. Pass an insecure credentials object for an insecure port. + * @return {number} The bound port number. Negative if binding the port failed. */ Server.prototype.bind = function(port, creds) { if (this.started) { @@ -962,4 +963,32 @@ Server.prototype.bind = function(port, creds) { return this._server.addHttp2Port(port, creds); }; +/** + * Called with the result of attempting to bind a port + * @callback grpc.Server~bindCallback + * @param {Error=} error If non-null, indicates that binding the port failed. + * @param {number} port The bound port number. If binding the port fails, this + * will be negative to match the output of bind. + */ + +/** + * Binds the server to the given port, with SSL disabled if creds is an + * insecure credentials object. Provides the result asynchronously. + * @param {string} port The port that the server should bind on, in the format + * "address:port" + * @param {grpc.ServerCredentials} creds Server credential object to be used for + * SSL. Pass an insecure credentials object for an insecure port. + */ +Server.prototype.bindAsync = function(port, creds, callback) { + /* This can throw. We do not try to catch that error because it indicates an + * incorrect use of the function, which should not be surfaced asynchronously + */ + const result = this.bind(port, creds) + if (result < 0) { + setImmediate(callback, new Error('Failed to bind port'), result); + } else { + setImmediate(callback, null, result); + } +} + exports.Server = Server; diff --git a/packages/grpc-native-core/test/async_test.js b/packages/grpc-native-core/test/async_test.js index 786e50bf..400baa9e 100644 --- a/packages/grpc-native-core/test/async_test.js +++ b/packages/grpc-native-core/test/async_test.js @@ -37,14 +37,16 @@ var getServer = require('./math/math_server.js'); var server = getServer(); +let serverCreds = grpc.ServerCredentials.createInsecure(); + describe('Async functionality', function() { before(function(done) { - var port_num = server.bind('0.0.0.0:0', - grpc.ServerCredentials.createInsecure()); - server.start(); - math_client = new math.Math('localhost:' + port_num, - grpc.credentials.createInsecure()); - done(); + server.bind('0.0.0.0:0', serverCreds, (error, port_num) => { + server.start(); + math_client = new math.Math('localhost:' + port_num, + grpc.credentials.createInsecure()); + done(); + }); }); after(function() { grpc.closeClient(math_client);