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