THRIFT-2376 nodejs: allow Promise style calls for client and server
patch: Pierre Lamot
diff --git a/compiler/cpp/src/generate/t_js_generator.cc b/compiler/cpp/src/generate/t_js_generator.cc
index 65a0219..7d99ab2 100644
--- a/compiler/cpp/src/generate/t_js_generator.cc
+++ b/compiler/cpp/src/generate/t_js_generator.cc
@@ -51,12 +51,12 @@
const std::string& option_string) :
t_oop_generator(program) {
(void) option_string;
-
+
std::map<std::string, std::string>::const_iterator iter;
-
+
iter = parsed_options.find("node");
gen_node_ = (iter != parsed_options.end());
-
+
iter = parsed_options.find("jquery");
gen_jquery_ = (iter != parsed_options.end());
@@ -302,7 +302,7 @@
*/
string t_js_generator::js_includes() {
if (gen_node_) {
- return string("var Thrift = require('thrift').Thrift;");
+ return string("var Thrift = require('thrift').Thrift;\nvar Q = require('q');");
}
string inc;
@@ -859,44 +859,83 @@
const std::vector<t_field*>& fields = arg_struct->get_members();
vector<t_field*>::const_iterator f_iter;
- f_service_ <<
- indent() << "this._handler." << tfunction->get_name() << "(";
-
- bool first = true;
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if (first) {
- first = false;
- } else {
- f_service_ << ", ";
- }
- f_service_ << "args." << (*f_iter)->get_name();
- }
-
// Shortcut out here for oneway functions
if (tfunction->is_oneway()) {
+ indent(f_service_) << "this._handler." << tfunction->get_name() << "(";
+
+ bool first = true;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if (first) {
+ first = false;
+ } else {
+ f_service_ << ", ";
+ }
+ f_service_ << "args." << (*f_iter)->get_name();
+ }
+
f_service_ << ")" << endl;
scope_down(f_service_);
f_service_ << endl;
return;
}
- if (!first) {
- f_service_ << ", ";
+ f_service_ <<
+ indent() << "if (this._handler." << tfunction->get_name() << ".length === " << fields.size() <<") {" << endl;
+ indent_up();
+ indent(f_service_) << "Q.fcall(this._handler." << tfunction->get_name();
+
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ f_service_ << ", args." << (*f_iter)->get_name();
}
- f_service_ << "function (err, result) {" << endl;
+
+ f_service_ << ")" << endl;
+ indent_up();
+ indent(f_service_) << ".then(function(result) {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "var result = new " << resultname << "({success: result});" << endl <<
+ indent() << "output.writeMessageBegin(\"" << tfunction->get_name() <<
+ "\", Thrift.MessageType.REPLY, seqid);" << endl <<
+ indent() << "result.write(output);" << endl <<
+ indent() << "output.writeMessageEnd();" << endl <<
+ indent() << "output.flush();" << endl;
+ indent_down();
+ indent(f_service_) << "}, function (err) {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "var result = new " << resultname << "(err);" << endl <<
+ indent() << "output.writeMessageBegin(\"" << tfunction->get_name() <<
+ "\", Thrift.MessageType.REPLY, seqid);" << endl <<
+ indent() << "result.write(output);" << endl <<
+ indent() << "output.writeMessageEnd();" << endl <<
+ indent() << "output.flush();" << endl;
+ indent_down();
+ indent(f_service_) << "});" << endl;
+ indent_down();
+ indent_down();
+ indent(f_service_) << "} else {" << endl;
+ indent_up();
+ indent(f_service_) << "this._handler." << tfunction->get_name() << "(";
+
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ f_service_ << "args." << (*f_iter)->get_name() << ", ";
+ }
+
+ f_service_ << " function (err, result) {" << endl;
indent_up();
f_service_ <<
- indent() << "var result = new " << resultname << "((err != null ? err : {success: result}));" << endl <<
- indent() << "output.writeMessageBegin(\"" << tfunction->get_name() <<
+ indent() << "var result = new " << resultname << "((err != null ? err : {success: result}));" << endl <<
+ indent() << "output.writeMessageBegin(\"" << tfunction->get_name() <<
"\", Thrift.MessageType.REPLY, seqid);" << endl <<
- indent() << "result.write(output);" << endl <<
- indent() << "output.writeMessageEnd();" << endl <<
- indent() << "output.flush();" << endl;
+ indent() << "result.write(output);" << endl <<
+ indent() << "output.writeMessageEnd();" << endl <<
+ indent() << "output.flush();" << endl;
indent_down();
- indent(f_service_) << "})" << endl;
-
+ indent(f_service_) << "});" << endl;
+ indent_down();
+ indent(f_service_) << "}" << endl;
scope_down(f_service_);
f_service_ << endl;
}
@@ -971,7 +1010,7 @@
if (gen_node_) {
f_service_ <<
- js_namespace(tservice->get_program()) << service_name_ << "Client = " <<
+ js_namespace(tservice->get_program()) << service_name_ << "Client = " <<
"exports.Client = function(output, pClass) {"<<endl;
} else {
f_service_ <<
@@ -1035,8 +1074,34 @@
if (gen_node_) { //Node.js output ./gen-nodejs
f_service_ <<
indent() << "this._seqid = this.new_seqid();" << endl <<
+ indent() << "if (callback === undefined) {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "var _defer = Q.defer();" << endl <<
+ indent() << "this._reqs[this.seqid()] = function(error, result) {" << endl;
+ indent_up();
+ indent(f_service_) << "if (error) {" << endl;
+ indent_up();
+ indent(f_service_) << "_defer.reject(error);" << endl;
+ indent_down();
+ indent(f_service_) << "} else {" << endl;
+ indent_up();
+ indent(f_service_) << "_defer.resolve(result);" << endl;
+ indent_down();
+ indent(f_service_) << "}" << endl;
+ indent_down();
+ indent(f_service_) << "};" << endl;
+ f_service_ <<
+ indent() << "this.send_" << funname << "(" << arglist << ");" << endl <<
+ indent() << "return _defer.promise;" << endl;
+ indent_down();
+ indent(f_service_) << "} else {" << endl;
+ indent_up();
+ f_service_ <<
indent() << "this._reqs[this.seqid()] = callback;" << endl <<
indent() << "this.send_" << funname << "(" << arglist << ");" << endl;
+ indent_down();
+ indent(f_service_) << "}" << endl;
}
else if (gen_jquery_) { //jQuery output ./gen-js
f_service_ << indent() << "if (callback === undefined) {" << endl;
@@ -1073,7 +1138,7 @@
f_service_ << indent() << "}" << endl;
}
}
-
+
indent_down();
f_service_ << "};" << endl << endl;
@@ -1105,7 +1170,7 @@
}
f_service_ <<
- indent() << "var args = new " << argsname << "();" << endl;
+ indent() << "var args = new " << argsname << "();" << endl;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
f_service_ <<
@@ -1123,7 +1188,7 @@
if (gen_jquery_) {
f_service_ << indent() << "return this.output.getTransport().flush(callback);" << endl;
} else {
- f_service_ << indent() << "if (callback) {" << endl;
+ f_service_ << indent() << "if (callback) {" << endl;
f_service_ << indent() << " var self = this;" << endl;
f_service_ << indent() << " this.output.getTransport().flush(true, function() {" << endl;
f_service_ << indent() << " if (this.readyState == 4 && this.status == 200) {" << endl;
@@ -1838,5 +1903,3 @@
THRIFT_REGISTER_GENERATOR(js, "Javascript",
" jquery: Generate jQuery compatible code.\n"
" node: Generate node.js compatible code.\n")
-
-
diff --git a/lib/nodejs/package.json b/lib/nodejs/package.json
index b2b92d6..d7e39e4 100755
--- a/lib/nodejs/package.json
+++ b/lib/nodejs/package.json
@@ -26,6 +26,7 @@
"engines": { "node": ">= 0.2.4" },
"dependencies": {
"node-int64": "~0.3.0",
+ "q": "1.0.x",
"nodeunit": "~0.8.0"
},
"devDependencies": {
diff --git a/lib/nodejs/test/client.js b/lib/nodejs/test/client.js
index 43d88f0..2aa2295 100644
--- a/lib/nodejs/test/client.js
+++ b/lib/nodejs/test/client.js
@@ -28,6 +28,7 @@
var ThriftProtocols = require('thrift/protocol');
var ThriftTest = require('./gen-nodejs/ThriftTest');
var ThriftTestDriver = require('./thrift_test_driver').ThriftTestDriver;
+var ThriftTestDriverPromise = require('./thrift_test_driver_promise').ThriftTestDriver;
var program = require('commander');
@@ -35,10 +36,12 @@
.option('-p, --protocol <protocol>', 'Set thrift protocol (binary|json) [protocol]')
.option('-t, --transport <transport>', 'Set thrift transport (buffered|framed) [transport]')
.option('--ssl', 'use SSL transport')
+ .option('--promise', 'test with promise style functions')
.parse(process.argv);
var protocol = undefined;
var transport = undefined;
+var testDriver = undefined;
if (program.protocol === "binary") {
protocol = ThriftProtocols.TBinaryProtocol;
@@ -58,6 +61,12 @@
transport = ThriftTransports.TBufferedTransport;
}
+if (program.promise) {
+ testDriver = ThriftTestDriverPromise;
+} else {
+ testDriver = ThriftTestDriver;
+}
+
var options = {
transport: transport,
protocol: protocol
@@ -78,7 +87,7 @@
assert(false, err);
});
-ThriftTestDriver(client, function (status) {
+testDriver(client, function (status) {
console.log(status);
connection.end();
});
diff --git a/lib/nodejs/test/server.js b/lib/nodejs/test/server.js
index 69519ab..acc0398 100644
--- a/lib/nodejs/test/server.js
+++ b/lib/nodejs/test/server.js
@@ -29,6 +29,7 @@
var ThriftProtocols = require('thrift/protocol');
var ThriftTest = require('./gen-nodejs/ThriftTest');
var ThriftTestHandler = require('./test_handler').ThriftTestHandler;
+var ThriftTestHandlerPromise = require('./test_handler_promise').ThriftTestHandler;
var program = require('commander');
@@ -37,10 +38,12 @@
.option('-p, --protocol <protocol>', 'Set thift protocol (binary|json) [protocol]')
.option('-t, --transport <transport>', 'Set thift transport (buffered|framed) [transport]')
.option('--ssl', 'use ssl transport')
+ .option('--promise', 'test with promise style functions')
.parse(process.argv);
var protocol = undefined;
var transport = undefined;
+var handler = undefined;
if (program.protocol === "binary") {
protocol = ThriftProtocols.TBinaryProtocol;
@@ -60,6 +63,12 @@
transport = ThriftTransports.TBufferedTransport;
}
+if (program.promise) {
+ handler = ThriftTestHandlerPromise;
+} else {
+ handler = ThriftTestHandler;
+}
+
var options = {
protocol: protocol,
transport: transport
@@ -69,9 +78,9 @@
//ssl options
options.key = fs.readFileSync(path.resolve(__dirname, 'server.key'));
options.cert = fs.readFileSync(path.resolve(__dirname, 'server.crt'));
- thrift.createSSLServer(ThriftTest, ThriftTestHandler, options).listen(9090);
+ thrift.createSSLServer(ThriftTest, handler, options).listen(9090);
} else {
//default
- thrift.createServer(ThriftTest, ThriftTestHandler, options).listen(9090);
+ thrift.createServer(ThriftTest, handler, options).listen(9090);
}
diff --git a/lib/nodejs/test/testAll.sh b/lib/nodejs/test/testAll.sh
index cdd0c79..96f8a2a 100755
--- a/lib/nodejs/test/testAll.sh
+++ b/lib/nodejs/test/testAll.sh
@@ -75,5 +75,7 @@
testClientServer binary framed --ssl || TESTOK=1
testMultiplexedClientServer binary framed --ssl || TESTOK=1
+#test promise style
+testClientServer binary framed --promise || TESTOK=1
exit $TESTOK
diff --git a/lib/nodejs/test/test_handler_promise.js b/lib/nodejs/test/test_handler_promise.js
new file mode 100644
index 0000000..fc698eb
--- /dev/null
+++ b/lib/nodejs/test/test_handler_promise.js
@@ -0,0 +1,194 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * 'License'); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+//This is the server side Node test handler for the standard
+// Apache Thrift test service.
+
+var ttypes = require('./gen-nodejs/ThriftTest_types');
+var TException = require('thrift/thrift').TException;
+
+var ThriftTestHandler = exports.ThriftTestHandler = {
+ testVoid: function() {
+ console.log('testVoid()');
+ },
+ testString: function(thing) {
+ console.log('testString(\'' + thing + '\')');
+ return thing;
+ },
+ testByte: function(thing) {
+ console.log('testByte(' + thing + ')');
+ return thing;
+ },
+ testI32: function(thing) {
+ console.log('testI32(' + thing + ')');
+ return thing;
+ },
+ testI64: function(thing) {
+ console.log('testI64(' + thing + ')');
+ return thing;
+ },
+ testDouble: function(thing) {
+ console.log('testDouble(' + thing + ')');
+ return thing;
+ },
+ testStruct: function(thing) {
+ console.log('testStruct(');
+ console.log(thing);
+ console.log(')');
+ return thing;
+ },
+ testNest: function(nest) {
+ console.log('testNest(');
+ console.log(nest);
+ console.log(')');
+ return nest;
+ },
+ testMap: function(thing) {
+ console.log('testMap(');
+ console.log(thing);
+ console.log(')');
+ return thing;
+ },
+ testStringMap: function(thing) {
+ console.log('testStringMap(');
+ console.log(thing);
+ console.log(')');
+ return thing;
+ },
+ testSet: function(thing, result) {
+ console.log('testSet(');
+ console.log(thing);
+ console.log(')');
+ return thing;
+ },
+ testList: function(thing) {
+ console.log('testList(');
+ console.log(thing);
+ console.log(')');
+ return thing;
+ },
+ testEnum: function(thing) {
+ console.log('testEnum(' + thing + ')');
+ return thing;
+ },
+ testTypedef: function(thing) {
+ console.log('testTypedef(' + thing + ')');
+ return thing;
+ },
+ testMapMap: function(hello) {
+ console.log('testMapMap(' + hello + ')');
+
+ var mapmap = [];
+ var pos = [];
+ var neg = [];
+ for (var i = 1; i < 5; i++) {
+ pos[i] = i;
+ neg[-i] = -i;
+ }
+ mapmap[4] = pos;
+ mapmap[-4] = neg;
+
+ return mapmap;
+ },
+ testInsanity: function(argument) {
+ console.log('testInsanity(');
+ console.log(argument);
+ console.log(')');
+
+ var hello = new ttypes.Xtruct();
+ hello.string_thing = 'Hello2';
+ hello.byte_thing = 2;
+ hello.i32_thing = 2;
+ hello.i64_thing = 2;
+
+ var goodbye = new ttypes.Xtruct();
+ goodbye.string_thing = 'Goodbye4';
+ goodbye.byte_thing = 4;
+ goodbye.i32_thing = 4;
+ goodbye.i64_thing = 4;
+
+ var crazy = new ttypes.Insanity();
+ crazy.userMap = [];
+ crazy.userMap[ttypes.Numberz.EIGHT] = 8;
+ crazy.userMap[ttypes.Numberz.FIVE] = 5;
+ crazy.xtructs = [goodbye, hello];
+
+ var first_map = [];
+ var second_map = [];
+
+ first_map[ttypes.Numberz.TWO] = crazy;
+ first_map[ttypes.Numberz.THREE] = crazy;
+
+ var looney = new ttypes.Insanity();
+ second_map[ttypes.Numberz.SIX] = looney;
+
+ var insane = [];
+ insane[1] = first_map;
+ insane[2] = second_map;
+
+ console.log('insane result:');
+ console.log(insane);
+ return insane;
+ },
+ testMulti: function(arg0, arg1, arg2, arg3, arg4, arg5) {
+ console.log('testMulti()');
+
+ var hello = new ttypes.Xtruct();
+ hello.string_thing = 'Hello2';
+ hello.byte_thing = arg0;
+ hello.i32_thing = arg1;
+ hello.i64_thing = arg2;
+ return hello;
+ },
+ testException: function(arg) {
+ console.log('testException('+arg+')');
+ if (arg === 'Xception') {
+ var x = new ttypes.Xception();
+ x.errorCode = 1001;
+ x.message = arg;
+ throw x;
+ } else if (arg === 'TException') {
+ throw new TException(arg);
+ } else {
+ return;
+ }
+ },
+ testMultiException: function(arg0, arg1) {
+ console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
+ if (arg0 === ('Xception')) {
+ var x = new ttypes.Xception();
+ x.errorCode = 1001;
+ x.message = 'This is an Xception';
+ throw x;
+ } else if (arg0 === ('Xception2')) {
+ var x2 = new ttypes.Xception2();
+ x2.errorCode = 2002;
+ x2.struct_thing = new ttypes.Xtruct();
+ x2.struct_thing.string_thing = 'This is an Xception2';
+ throw x2;
+ }
+
+ var res = new ttypes.Xtruct();
+ res.string_thing = arg1;
+ return res;
+ },
+ testOneway: function(sleepFor) {
+ console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!');
+ }
+}; //ThriftTestSvcHandler
diff --git a/lib/nodejs/test/thrift_test_driver_promise.js b/lib/nodejs/test/thrift_test_driver_promise.js
new file mode 100644
index 0000000..b5c1b87
--- /dev/null
+++ b/lib/nodejs/test/thrift_test_driver_promise.js
@@ -0,0 +1,394 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * 'License'); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+ // This is the Node test driver for the standard Apache Thrift
+ // test service. The driver invokes every function defined in the
+ // Thrift Test service with a representative range of parameters.
+ //
+ // The ThriftTestDriver function requires a client object
+ // connected to a server hosting the Thrift Test service and
+ // supports an optional callback function which is called with
+ // a status message when the test is complete.
+
+var assert = require('assert');
+var ttypes = require('./gen-nodejs/ThriftTest_types');
+
+var ThriftTestDriver = exports.ThriftTestDriver = function(client, callback) {
+
+// deepEqual doesn't work with fields using node-int64
+function checkRecursively(map1, map2) {
+ if (typeof map1 !== 'function' && typeof map2 !== 'function') {
+ if (!map1 || typeof map1 !== 'object') {
+ assert.equal(map1, map2);
+ } else {
+ for (var key in map1) {
+ checkRecursively(map1[key], map2[key]);
+ }
+ }
+ }
+}
+
+client.testVoid()
+ .then(function(response) {
+ assert.equal(undefined, response); //void
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+
+client.testString("Test")
+ .then(function(response) {
+ assert.equal("Test", response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+client.testString("")
+ .then(function(response) {
+ assert.equal("", response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+//all Languages in UTF-8
+var stringTest = "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, " +
+ "Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, " +
+ "Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, " +
+ "বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, " +
+ "Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, " +
+ "Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, " +
+ "Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, " +
+ "Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, " +
+ "Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, " +
+ "Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, " +
+ "Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, " +
+ "ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, " +
+ "Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, " +
+ "Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa " +
+ "Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, مازِرونی, Bahasa " +
+ "Melayu, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, " +
+ "Norsk (nynorsk), Norsk (bokmål), Nouormand, Diné bizaad, " +
+ "Occitan, Иронау, Papiamentu, Deitsch, Polski, پنجابی, پښتو, " +
+ "Norfuk / Pitkern, Português, Runa Simi, Rumantsch, Romani, Română, " +
+ "Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple " +
+ "English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, " +
+ "Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, " +
+ "Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, " +
+ "Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, " +
+ "Bân-lâm-gú, 粵語";
+
+client.testString(stringTest)
+ .then(function(response) {
+ assert.equal(stringTest, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+
+var specialCharacters = 'quote: \" backslash:' +
+ ' forwardslash-escaped: \/ ' +
+ ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
+ ' now-all-of-them-together: "\\\/\b\n\r\t' +
+ ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><' +
+ ' char-to-test-json-parsing: ]] \"]] \\" }}}{ [[[ ';
+client.testString(specialCharacters)
+ .then(function(response) {
+ assert.equal(specialCharacters, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+
+client.testByte(1)
+ .then(function(response) {
+ assert.equal(1, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+
+client.testByte(0)
+ .then(function(response) {
+ assert.equal(0, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+client.testByte(-1)
+ .then(function(response) {
+ assert.equal(-1, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+client.testByte(-127)
+ .then(function(response) {
+ assert.equal(-127, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+client.testI32(-1)
+ .then(function(response) {
+ assert.equal(-1, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+client.testI64(5)
+ .then(function(response) {
+ assert.equal(5, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+client.testI64(-5)
+ .then(function(response) {
+ assert.equal(-5, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+client.testI64(-34359738368)
+ .then(function(response) {
+ assert.equal(-34359738368, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+client.testDouble(-5.2098523)
+ .then(function(response) {
+ assert.equal(-5.2098523, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+client.testDouble(7.012052175215044)
+ .then(function(response) {
+ assert.equal(7.012052175215044, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+var out = new ttypes.Xtruct({
+ string_thing: 'Zero',
+ byte_thing: 1,
+ i32_thing: -3,
+ i64_thing: 1000000
+});
+client.testStruct(out)
+ .then(function(response) {
+ checkRecursively(out, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+var out2 = new ttypes.Xtruct2();
+out2.byte_thing = 1;
+out2.struct_thing = out;
+out2.i32_thing = 5;
+client.testNest(out2)
+ .then(function(response) {
+ checkRecursively(out2, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+var mapout = {};
+for (var i = 0; i < 5; ++i) {
+ mapout[i] = i-10;
+}
+client.testMap(mapout)
+ .then(function(response) {
+ assert.deepEqual(mapout, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+var mapTestInput = {
+ "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
+ "longValue":stringTest, stringTest:"long key"
+};
+client.testStringMap(mapTestInput)
+ .then(function(response) {
+ assert.deepEqual(mapTestInput, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+var setTestInput = [1,2,3];
+client.testSet(setTestInput)
+ .then(function(response) {
+ assert.deepEqual(setTestInput, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+client.testList(setTestInput)
+ .then(function(response) {
+ assert.deepEqual(setTestInput, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+client.testEnum(ttypes.Numberz.ONE)
+ .then(function(response) {
+ assert.equal(ttypes.Numberz.ONE, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+client.testTypedef(69)
+ .then(function(response) {
+ assert.equal(69, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+var mapMapTest = {
+ "4": {"1":1, "2":2, "3":3, "4":4},
+ "-4": {"-4":-4, "-3":-3, "-2":-2, "-1":-1}
+};
+client.testMapMap(mapMapTest)
+ .then(function(response) {
+ assert.deepEqual(mapMapTest, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+var crazy = new ttypes.Insanity({
+ "userMap":{ "5":5, "8":8 },
+ "xtructs":[new ttypes.Xtruct({
+ "string_thing":"Goodbye4",
+ "byte_thing":4,
+ "i32_thing":4,
+ "i64_thing":4
+ }), new ttypes.Xtruct({
+ "string_thing":"Hello2",
+ "byte_thing":2,
+ "i32_thing":2,
+ "i64_thing":2
+ })]
+});
+
+var insanity = {
+ "1":{ "2": crazy, "3": crazy },
+ "2":{ "6":{ "userMap":null, "xtructs":null } }
+};
+client.testInsanity(crazy)
+ .then(function(response) {
+ checkRecursively(insanity, response);
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+client.testException('TException')
+ .then(function() {
+ assert(false);
+ });
+
+client.testException('Xception')
+ .then(function(response) {
+ assert.equal(err.errorCode, 1001);
+ assert.equal('Xception', err.message)
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+client.testException('no Exception')
+ .then(function(response) {
+ assert.equal(undefined, response); //void
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+client.testOneway(0, function(error, response) {
+ assert(false); //should not answer
+});
+
+(function() {
+ var test_complete = false;
+ var retrys = 0;
+ var retry_limit = 30;
+ var retry_interval = 100;
+ /**
+ * redo a simple test after the oneway to make sure we aren't "off by one" --
+ * if the server treated oneway void like normal void, this next test will
+ * fail since it will get the void confirmation rather than the correct
+ * result. In this circumstance, the client will throw the exception:
+ *
+ * Because this is the last test against the server, when it completes
+ * the entire suite is complete by definition (the tests run serially).
+ */
+ client.testI32(-1)
+ .then(function(response) {
+ assert.equal(-1, response);
+ test_complete = true
+ })
+ .fail(function() {
+ assert(false);
+ });
+
+//We wait up to retry_limit * retry_interval for the test suite to complete
+ function TestForCompletion() {
+ if(test_complete) {
+ if (callback) {
+ callback("Server successfully tested!");
+ }
+ } else {
+ if (++retrys < retry_limit) {
+ setTimeout(TestForCompletion, retry_interval);
+ } else {
+ if (callback) {
+ callback("Server test failed to complete after " +
+ (retry_limit*retry_interval/1000) + " seconds");
+ }
+ }
+ }
+ }
+
+ setTimeout(TestForCompletion, retry_interval);
+})();
+}
diff --git a/tutorial/nodejs/Makefile.am b/tutorial/nodejs/Makefile.am
index 2affb9f..ecf3b2b 100644
--- a/tutorial/nodejs/Makefile.am
+++ b/tutorial/nodejs/Makefile.am
@@ -30,9 +30,18 @@
tutorialclient: all
NODE_PATH="$(top_builddir)/lib/nodejs:$(top_builddir)/lib/nodejs/lib:$(NODEPATH)" $(NODEJS) NodeClient.js
+tutorialserver_promise: all
+ NODE_PATH="$(top_builddir)/lib/nodejs:$(top_builddir)/lib/nodejs/lib:$(NODEPATH)" $(NODEJS) NodeServerPromise.js
+
+tutorialclient_promise: all
+ NODE_PATH="$(top_builddir)/lib/nodejs:$(top_builddir)/lib/nodejs/lib:$(NODEPATH)" $(NODEJS) NodeClientPromise.js
+
+
clean-local:
$(RM) -r gen-*
EXTRA_DIST = \
NodeServer.js \
- NodeClient.js
+ NodeClient.js \
+ NodeServerPromise.js \
+ NodeClientPromise.js
diff --git a/tutorial/nodejs/NodeClient.js b/tutorial/nodejs/NodeClient.js
index 3d09709..74aa55a 100644
--- a/tutorial/nodejs/NodeClient.js
+++ b/tutorial/nodejs/NodeClient.js
@@ -68,7 +68,7 @@
work.num2 = 10;
client.calculate(1, work, function(err, message) {
- console.log('15-10=' + message.value);
+ console.log('15-10=' + message);
client.getStruct(1, function(err, message){
console.log('Check log: ' + message.value);
diff --git a/tutorial/nodejs/NodeClientPromise.js b/tutorial/nodejs/NodeClientPromise.js
new file mode 100644
index 0000000..7bcf884
--- /dev/null
+++ b/tutorial/nodejs/NodeClientPromise.js
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+var thrift = require('thrift');
+var ThriftTransports = require('thrift/transport');
+var ThriftProtocols = require('thrift/protocol');
+var Calculator = require('./gen-nodejs/Calculator');
+var ttypes = require('./gen-nodejs/tutorial_types');
+
+
+transport = ThriftTransports.TBufferedTransport()
+protocol = ThriftProtocols.TBinaryProtocol()
+
+var connection = thrift.createConnection("localhost", 9090, {
+ transport : transport,
+ protocol : protocol
+});
+
+connection.on('error', function(err) {
+ assert(false, err);
+});
+
+// Create a Calculator client with the connection
+var client = thrift.createClient(Calculator, connection);
+
+
+client.ping()
+ .then(function() {
+ console.log('ping()');
+ });
+
+client.add(1,1)
+ .then(function(response) {
+ console.log("1+1=" + response);
+ });
+
+work = new ttypes.Work();
+work.op = ttypes.Operation.DIVIDE;
+work.num1 = 1;
+work.num2 = 0;
+
+client.calculate(1, work)
+ .then(function(message) {
+ console.log('Whoa? You know how to divide by zero?');
+ })
+ .fail(function(err) {
+ console.log("InvalidOperation " + err);
+ });
+
+
+work.op = ttypes.Operation.SUBTRACT;
+work.num1 = 15;
+work.num2 = 10;
+
+client.calculate(1, work)
+ .then(function(value) {
+ console.log('15-10=' + value);
+ return client.getStruct(1);
+ })
+ .then(function(message) {
+ console.log('Check log: ' + message.value);
+ })
+ .fin(function() {
+ //close the connection once we're done
+ connection.end();
+ });
diff --git a/tutorial/nodejs/NodeServerPromise.js b/tutorial/nodejs/NodeServerPromise.js
new file mode 100644
index 0000000..4fd0a35
--- /dev/null
+++ b/tutorial/nodejs/NodeServerPromise.js
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+var thrift = require("thrift");
+var Calculator = require("./gen-nodejs/Calculator");
+var ttypes = require("./gen-nodejs/tutorial_types");
+var SharedStruct = require("./gen-nodejs/shared_types").SharedStruct;
+
+var data = {};
+
+var server = thrift.createServer(Calculator, {
+ ping: function() {
+ console.log("ping()");
+ },
+
+ add: function(n1, n2) {
+ console.log("add(", n1, ",", n2, ")");
+ return n1 + n2;
+ },
+
+ calculate: function(logid, work) {
+ console.log("calculate(", logid, ",", work, ")");
+
+ var val = 0;
+ if (work.op == ttypes.Operation.ADD) {
+ val = work.num1 + work.num2;
+ } else if (work.op === ttypes.Operation.SUBTRACT) {
+ val = work.num1 - work.num2;
+ } else if (work.op === ttypes.Operation.MULTIPLY) {
+ val = work.num1 * work.num2;
+ } else if (work.op === ttypes.Operation.DIVIDE) {
+ if (work.num2 === 0) {
+ var x = new ttypes.InvalidOperation();
+ x.what = work.op;
+ x.why = 'Cannot divide by 0';
+ throw x;
+ }
+ val = work.num1 / work.num2;
+ } else {
+ var x = new ttypes.InvalidOperation();
+ x.what = work.op;
+ x.why = 'Invalid operation';
+ throw x;
+ }
+
+ var entry = new SharedStruct();
+ entry.key = logid;
+ entry.value = ""+val;
+ data[logid] = entry;
+ return val;
+ },
+
+ getStruct: function(key) {
+ console.log("getStruct(", key, ")");
+ return data[key];
+ },
+
+ zip: function() {
+ console.log("zip()");
+ }
+
+});
+
+server.listen(9090);