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