Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 1 | /*
|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one
|
| 3 | * or more contributor license agreements. See the NOTICE file
|
| 4 | * distributed with this work for additional information
|
| 5 | * regarding copyright ownership. The ASF licenses this file
|
| 6 | * to you under the Apache License, Version 2.0 (the
|
| 7 | * 'License'); you may not use this file except in compliance
|
| 8 | * with the License. You may obtain a copy of the License at
|
| 9 | *
|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0
|
| 11 | *
|
| 12 | * Unless required by applicable law or agreed to in writing,
|
| 13 | * software distributed under the License is distributed on an
|
| 14 | * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
| 15 | * KIND, either express or implied. See the License for the
|
| 16 | * specific language governing permissions and limitations
|
| 17 | * under the License.
|
| 18 | */
|
| 19 |
|
ra | 7f76e1d | 2014-04-02 12:31:05 -0700 | [diff] [blame] | 20 | // This is the Node.js test driver for the standard Apache Thrift
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 21 | // test service. The driver invokes every function defined in the
|
| 22 | // Thrift Test service with a representative range of parameters.
|
| 23 | //
|
| 24 | // The ThriftTestDriver function requires a client object
|
| 25 | // connected to a server hosting the Thrift Test service and
|
| 26 | // supports an optional callback function which is called with
|
| 27 | // a status message when the test is complete.
|
| 28 |
|
| 29 | var assert = require('assert');
|
| 30 | var ttypes = require('./gen-nodejs/ThriftTest_types');
|
ra | 7f76e1d | 2014-04-02 12:31:05 -0700 | [diff] [blame] | 31 | var Int64 = require('node-int64');
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 32 |
|
| 33 | var ThriftTestDriver = exports.ThriftTestDriver = function(client, callback) {
|
| 34 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 35 | function checkRecursively(map1, map2) {
|
| 36 | if (typeof map1 !== 'function' && typeof map2 !== 'function') {
|
| 37 | if (!map1 || typeof map1 !== 'object') {
|
ra | 7f76e1d | 2014-04-02 12:31:05 -0700 | [diff] [blame] | 38 | //Handle int64 types (which use node-int64 in Node.js JavaScript)
|
| 39 | if ((typeof map1 === "number") && (typeof map2 === "object") &&
|
| 40 | (map2.buffer) && (map2.buffer instanceof Buffer) && (map2.buffer.length === 8)) {
|
| 41 | var n = new Int64(map2.buffer);
|
| 42 | assert.equal(map1, n.toNumber());
|
| 43 | } else {
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 44 | assert.equal(map1, map2);
|
ra | 7f76e1d | 2014-04-02 12:31:05 -0700 | [diff] [blame] | 45 | }
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 46 | } else {
|
| 47 | for (var key in map1) {
|
| 48 | checkRecursively(map1[key], map2[key]);
|
| 49 | }
|
| 50 | }
|
| 51 | }
|
| 52 | }
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 53 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 54 | client.testVoid(function(err, response) {
|
| 55 | assert( ! err);
|
| 56 | assert.equal(undefined, response); //void
|
| 57 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 58 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 59 | client.testString("Test", function(err, response) {
|
| 60 | assert( ! err);
|
| 61 | assert.equal("Test", response);
|
| 62 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 63 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 64 | client.testString("", function(err, response) {
|
| 65 | assert( ! err);
|
| 66 | assert.equal("", response);
|
| 67 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 68 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 69 | //all Languages in UTF-8
|
ra | 779b9ac | 2014-04-23 20:04:23 -0700 | [diff] [blame] | 70 | /*jshint -W100 */
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 71 | var stringTest = "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, " +
|
| 72 | "Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, " +
|
| 73 | "Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, " +
|
| 74 | "বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, " +
|
| 75 | "Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, " +
|
| 76 | "Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, " +
|
| 77 | "Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, " +
|
| 78 | "Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, " +
|
| 79 | "Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, " +
|
| 80 | "Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, " +
|
| 81 | "Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, " +
|
| 82 | "ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, " +
|
| 83 | "Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, " +
|
| 84 | "Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa " +
|
| 85 | "Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, مازِرونی, Bahasa " +
|
| 86 | "Melayu, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, " +
|
| 87 | "Norsk (nynorsk), Norsk (bokmål), Nouormand, Diné bizaad, " +
|
| 88 | "Occitan, Иронау, Papiamentu, Deitsch, Polski, پنجابی, پښتو, " +
|
| 89 | "Norfuk / Pitkern, Português, Runa Simi, Rumantsch, Romani, Română, " +
|
| 90 | "Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple " +
|
| 91 | "English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, " +
|
| 92 | "Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, " +
|
| 93 | "Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, " +
|
| 94 | "Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, " +
|
| 95 | "Bân-lâm-gú, 粵語";
|
ra | 779b9ac | 2014-04-23 20:04:23 -0700 | [diff] [blame] | 96 | /*jshint +W100 */
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 97 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 98 | client.testString(stringTest, function(err, response) {
|
| 99 | assert( ! err);
|
| 100 | assert.equal(stringTest, response);
|
| 101 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 102 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 103 | var specialCharacters = 'quote: \" backslash:' +
|
| 104 | ' forwardslash-escaped: \/ ' +
|
| 105 | ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
|
| 106 | ' now-all-of-them-together: "\\\/\b\n\r\t' +
|
| 107 | ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><' +
|
| 108 | ' char-to-test-json-parsing: ]] \"]] \\" }}}{ [[[ ';
|
| 109 | client.testString(specialCharacters, function(err, response) {
|
| 110 | assert( ! err);
|
| 111 | assert.equal(specialCharacters, response);
|
| 112 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 113 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 114 | client.testByte(1, function(err, response) {
|
| 115 | assert( ! err);
|
| 116 | assert.equal(1, response);
|
| 117 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 118 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 119 | client.testByte(0, function(err, response) {
|
| 120 | assert( ! err);
|
| 121 | assert.equal(0, response);
|
| 122 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 123 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 124 | client.testByte(-1, function(err, response) {
|
| 125 | assert( ! err);
|
| 126 | assert.equal(-1, response);
|
| 127 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 128 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 129 | client.testByte(-127, function(err, response) {
|
| 130 | assert( ! err);
|
| 131 | assert.equal(-127, response);
|
| 132 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 133 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 134 | client.testI32(-1, function(err, response) {
|
| 135 | assert( ! err);
|
| 136 | assert.equal(-1, response);
|
| 137 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 138 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 139 | client.testI64(5, function(err, response) {
|
| 140 | assert( ! err);
|
| 141 | assert.equal(5, response);
|
| 142 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 143 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 144 | client.testI64(-5, function(err, response) {
|
| 145 | assert( ! err);
|
| 146 | assert.equal(-5, response);
|
| 147 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 148 |
|
ra | 20aeba3 | 2014-05-11 00:25:01 -0700 | [diff] [blame^] | 149 | client.testI64(734359738368, function(err, response) {
|
| 150 | assert( ! err);
|
| 151 | assert.equal(734359738368, response);
|
| 152 | });
|
| 153 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 154 | client.testI64(-34359738368, function(err, response) {
|
| 155 | assert( ! err);
|
| 156 | assert.equal(-34359738368, response);
|
| 157 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 158 |
|
ra | 20aeba3 | 2014-05-11 00:25:01 -0700 | [diff] [blame^] | 159 | client.testI64(-734359738368, function(err, response) {
|
| 160 | assert( ! err);
|
| 161 | assert.equal(-734359738368, response);
|
| 162 | });
|
| 163 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 164 | client.testDouble(-5.2098523, function(err, response) {
|
| 165 | assert( ! err);
|
| 166 | assert.equal(-5.2098523, response);
|
| 167 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 168 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 169 | client.testDouble(7.012052175215044, function(err, response) {
|
| 170 | assert( ! err);
|
| 171 | assert.equal(7.012052175215044, response);
|
| 172 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 173 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 174 | var out = new ttypes.Xtruct({
|
| 175 | string_thing: 'Zero',
|
| 176 | byte_thing: 1,
|
| 177 | i32_thing: -3,
|
| 178 | i64_thing: 1000000
|
| 179 | });
|
| 180 | client.testStruct(out, function(err, response) {
|
| 181 | assert( ! err);
|
| 182 | checkRecursively(out, response);
|
| 183 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 184 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 185 | var out2 = new ttypes.Xtruct2();
|
| 186 | out2.byte_thing = 1;
|
| 187 | out2.struct_thing = out;
|
| 188 | out2.i32_thing = 5;
|
| 189 | client.testNest(out2, function(err, response) {
|
| 190 | assert( ! err);
|
| 191 | checkRecursively(out2, response);
|
| 192 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 193 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 194 | var mapout = {};
|
| 195 | for (var i = 0; i < 5; ++i) {
|
| 196 | mapout[i] = i-10;
|
| 197 | }
|
| 198 | client.testMap(mapout, function(err, response) {
|
| 199 | assert( ! err);
|
| 200 | assert.deepEqual(mapout, response);
|
| 201 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 202 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 203 | var mapTestInput = {
|
| 204 | "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
|
ra | 20aeba3 | 2014-05-11 00:25:01 -0700 | [diff] [blame^] | 205 | "longValue":stringTest, "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, ":"long key"
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 206 | };
|
| 207 | client.testStringMap(mapTestInput, function(err, response) {
|
| 208 | assert( ! err);
|
| 209 | assert.deepEqual(mapTestInput, response);
|
| 210 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 211 |
|
ra | 20aeba3 | 2014-05-11 00:25:01 -0700 | [diff] [blame^] | 212 | var setTestSetInput = [1,2,3];
|
| 213 | client.testSet(setTestSetInput, function(err, response) {
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 214 | assert( ! err);
|
ra | 20aeba3 | 2014-05-11 00:25:01 -0700 | [diff] [blame^] | 215 | assert.deepEqual(setTestSetInput, response);
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 216 | });
|
ra | 20aeba3 | 2014-05-11 00:25:01 -0700 | [diff] [blame^] | 217 | var setTestListInput = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
|
| 218 | client.testList(setTestListInput, function(err, response) {
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 219 | assert( ! err);
|
ra | 20aeba3 | 2014-05-11 00:25:01 -0700 | [diff] [blame^] | 220 | assert.deepEqual(setTestListInput, response);
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 221 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 222 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 223 | client.testEnum(ttypes.Numberz.ONE, function(err, response) {
|
| 224 | assert( ! err);
|
| 225 | assert.equal(ttypes.Numberz.ONE, response);
|
| 226 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 227 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 228 | client.testTypedef(69, function(err, response) {
|
| 229 | assert( ! err);
|
| 230 | assert.equal(69, response);
|
| 231 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 232 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 233 | var mapMapTest = {
|
| 234 | "4": {"1":1, "2":2, "3":3, "4":4},
|
| 235 | "-4": {"-4":-4, "-3":-3, "-2":-2, "-1":-1}
|
| 236 | };
|
ra | 20aeba3 | 2014-05-11 00:25:01 -0700 | [diff] [blame^] | 237 | client.testMapMap(1, function(err, response) {
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 238 | assert( ! err);
|
| 239 | assert.deepEqual(mapMapTest, response);
|
| 240 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 241 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 242 | var crazy = new ttypes.Insanity({
|
| 243 | "userMap":{ "5":5, "8":8 },
|
| 244 | "xtructs":[new ttypes.Xtruct({
|
| 245 | "string_thing":"Goodbye4",
|
| 246 | "byte_thing":4,
|
| 247 | "i32_thing":4,
|
| 248 | "i64_thing":4
|
| 249 | }), new ttypes.Xtruct({
|
| 250 | "string_thing":"Hello2",
|
| 251 | "byte_thing":2,
|
| 252 | "i32_thing":2,
|
| 253 | "i64_thing":2
|
| 254 | })]
|
| 255 | });
|
| 256 | var insanity = {
|
| 257 | "1":{ "2": crazy, "3": crazy },
|
ra | 7f76e1d | 2014-04-02 12:31:05 -0700 | [diff] [blame] | 258 | "2":{ "6":{ "userMap":{}, "xtructs":[] } }
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 259 | };
|
| 260 | client.testInsanity(crazy, function(err, response) {
|
| 261 | assert( ! err);
|
| 262 | checkRecursively(insanity, response);
|
| 263 | });
|
Jens Geyer | b9d5522 | 2014-01-10 21:26:25 +0100 | [diff] [blame] | 264 |
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 265 | client.testException('TException', function(err, response) {
|
| 266 | assert( ! response);
|
| 267 | });
|
| 268 |
|
| 269 | client.testException('Xception', function(err, response) {
|
| 270 | assert( ! response);
|
| 271 | assert.equal(err.errorCode, 1001);
|
| 272 | assert.equal('Xception', err.message);
|
| 273 | });
|
| 274 |
|
| 275 | client.testException('no Exception', function(err, response) {
|
| 276 | assert( ! err);
|
| 277 | assert.equal(undefined, response); //void
|
| 278 | });
|
| 279 |
|
| 280 | client.testOneway(0, function(err, response) {
|
| 281 | assert(false); //should not answer
|
| 282 | });
|
| 283 |
|
| 284 | (function() {
|
| 285 | var test_complete = false;
|
| 286 | var retrys = 0;
|
| 287 | var retry_limit = 30;
|
| 288 | var retry_interval = 100;
|
| 289 | /**
|
| 290 | * redo a simple test after the oneway to make sure we aren't "off by one" --
|
| 291 | * if the server treated oneway void like normal void, this next test will
|
| 292 | * fail since it will get the void confirmation rather than the correct
|
| 293 | * result. In this circumstance, the client will throw the exception:
|
| 294 | *
|
| 295 | * Because this is the last test against the server, when it completes
|
| 296 | * the entire suite is complete by definition (the tests run serially).
|
| 297 | */
|
| 298 | client.testI32(-1, function(err, response) {
|
| 299 | assert( ! err);
|
| 300 | assert.equal(-1, response);
|
| 301 | test_complete = true;
|
| 302 | });
|
| 303 |
|
ra | 8f697cb | 2014-04-23 02:23:18 -0700 | [diff] [blame] | 304 | //We wait up to retry_limit * retry_interval for the test suite to complete
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 305 | function TestForCompletion() {
|
| 306 | if(test_complete) {
|
| 307 | if (callback) {
|
| 308 | callback("Server successfully tested!");
|
| 309 | }
|
| 310 | } else {
|
| 311 | if (++retrys < retry_limit) {
|
| 312 | setTimeout(TestForCompletion, retry_interval);
|
| 313 | } else {
|
ra | 8f697cb | 2014-04-23 02:23:18 -0700 | [diff] [blame] | 314 | if (callback) {
|
| 315 | callback("Server test failed to complete after " +
|
| 316 | (retry_limit*retry_interval/1000) + " seconds");
|
| 317 | }
|
henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 318 | }
|
| 319 | }
|
| 320 | }
|
| 321 |
|
| 322 | setTimeout(TestForCompletion, retry_interval);
|
| 323 | })();
|
ra | 8f697cb | 2014-04-23 02:23:18 -0700 | [diff] [blame] | 324 | };
|