Konrad Grochowski | f9c4be8 | 2015-01-09 11:32:26 +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 | |
Randy Abernethy | 96f4f07 | 2015-02-10 02:29:15 -0800 | [diff] [blame^] | 20 | // This is the Node.js test driver for the standard Apache Thrift |
| 21 | // test service. The driver invokes every function defined in the |
Konrad Grochowski | f9c4be8 | 2015-01-09 11:32:26 +0100 | [diff] [blame] | 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 |
Randy Abernethy | 96f4f07 | 2015-02-10 02:29:15 -0800 | [diff] [blame^] | 26 | // supports an optional callback function which is called with |
Konrad Grochowski | f9c4be8 | 2015-01-09 11:32:26 +0100 | [diff] [blame] | 27 | // a status message when the test is complete. |
| 28 | |
| 29 | var assert = require('assert'); |
| 30 | var ttypes = require('./gen-nodejs/ThriftTest_types'); |
| 31 | var Int64 = require('node-int64'); |
| 32 | |
| 33 | var ThriftTestDriver = exports.ThriftTestDriver = function(client, callback) { |
Randy Abernethy | 96f4f07 | 2015-02-10 02:29:15 -0800 | [diff] [blame^] | 34 | |
Konrad Grochowski | f9c4be8 | 2015-01-09 11:32:26 +0100 | [diff] [blame] | 35 | function checkRecursively(map1, map2) { |
| 36 | if (typeof map1 !== 'function' && typeof map2 !== 'function') { |
| 37 | if (!map1 || typeof map1 !== 'object') { |
| 38 | //Handle int64 types (which use node-int64 in Node.js JavaScript) |
Randy Abernethy | 96f4f07 | 2015-02-10 02:29:15 -0800 | [diff] [blame^] | 39 | if ((typeof map1 === "number") && (typeof map2 === "object") && |
Konrad Grochowski | f9c4be8 | 2015-01-09 11:32:26 +0100 | [diff] [blame] | 40 | (map2.buffer) && (map2.buffer instanceof Buffer) && (map2.buffer.length === 8)) { |
| 41 | var n = new Int64(map2.buffer); |
Randy Abernethy | 96f4f07 | 2015-02-10 02:29:15 -0800 | [diff] [blame^] | 42 | assert.equal(map1, n.toNumber()); |
Konrad Grochowski | f9c4be8 | 2015-01-09 11:32:26 +0100 | [diff] [blame] | 43 | } else { |
| 44 | assert.equal(map1, map2); |
| 45 | } |
| 46 | } else { |
| 47 | for (var key in map1) { |
| 48 | checkRecursively(map1[key], map2[key]); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | client.testVoid(function(err, response) { |
| 55 | assert( ! err); |
| 56 | assert.equal(undefined, response); //void |
| 57 | }); |
| 58 | |
| 59 | client.testString("Test", function(err, response) { |
| 60 | assert( ! err); |
| 61 | assert.equal("Test", response); |
| 62 | }); |
| 63 | |
| 64 | client.testString("", function(err, response) { |
| 65 | assert( ! err); |
| 66 | assert.equal("", response); |
| 67 | }); |
| 68 | |
| 69 | //all Languages in UTF-8 |
| 70 | /*jshint -W100 */ |
| 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ú, 粵語"; |
| 96 | /*jshint +W100 */ |
| 97 | |
| 98 | client.testString(stringTest, function(err, response) { |
| 99 | assert( ! err); |
| 100 | assert.equal(stringTest, response); |
| 101 | }); |
| 102 | |
| 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 | }); |
| 113 | |
| 114 | client.testByte(1, function(err, response) { |
| 115 | assert( ! err); |
| 116 | assert.equal(1, response); |
| 117 | }); |
| 118 | |
| 119 | client.testByte(0, function(err, response) { |
| 120 | assert( ! err); |
| 121 | assert.equal(0, response); |
| 122 | }); |
| 123 | |
| 124 | client.testByte(-1, function(err, response) { |
| 125 | assert( ! err); |
| 126 | assert.equal(-1, response); |
| 127 | }); |
| 128 | |
| 129 | client.testByte(-127, function(err, response) { |
| 130 | assert( ! err); |
| 131 | assert.equal(-127, response); |
| 132 | }); |
| 133 | |
| 134 | client.testI32(-1, function(err, response) { |
| 135 | assert( ! err); |
| 136 | assert.equal(-1, response); |
| 137 | }); |
| 138 | |
| 139 | client.testI64(5, function(err, response) { |
| 140 | assert( ! err); |
| 141 | assert.equal(5, response); |
| 142 | }); |
| 143 | |
| 144 | client.testI64(-5, function(err, response) { |
| 145 | assert( ! err); |
| 146 | assert.equal(-5, response); |
| 147 | }); |
| 148 | |
| 149 | client.testI64(734359738368, function(err, response) { |
| 150 | assert( ! err); |
| 151 | assert.equal(734359738368, response); |
| 152 | }); |
| 153 | |
| 154 | client.testI64(-34359738368, function(err, response) { |
| 155 | assert( ! err); |
| 156 | assert.equal(-34359738368, response); |
| 157 | }); |
| 158 | |
| 159 | client.testI64(-734359738368, function(err, response) { |
| 160 | assert( ! err); |
| 161 | assert.equal(-734359738368, response); |
| 162 | }); |
| 163 | |
| 164 | client.testDouble(-5.2098523, function(err, response) { |
| 165 | assert( ! err); |
| 166 | assert.equal(-5.2098523, response); |
| 167 | }); |
| 168 | |
| 169 | client.testDouble(7.012052175215044, function(err, response) { |
| 170 | assert( ! err); |
| 171 | assert.equal(7.012052175215044, response); |
| 172 | }); |
| 173 | |
Konrad Grochowski | f9c4be8 | 2015-01-09 11:32:26 +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 | }); |
| 184 | |
| 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 | }); |
| 193 | |
| 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 | }); |
| 202 | |
| 203 | var mapTestInput = { |
| 204 | "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key", |
| 205 | "longValue":stringTest, "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, ":"long key" |
| 206 | }; |
| 207 | client.testStringMap(mapTestInput, function(err, response) { |
| 208 | assert( ! err); |
| 209 | assert.deepEqual(mapTestInput, response); |
| 210 | }); |
| 211 | |
| 212 | var setTestSetInput = [1,2,3]; |
| 213 | client.testSet(setTestSetInput, function(err, response) { |
| 214 | assert( ! err); |
| 215 | assert.deepEqual(setTestSetInput, response); |
| 216 | }); |
| 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) { |
| 219 | assert( ! err); |
| 220 | assert.deepEqual(setTestListInput, response); |
| 221 | }); |
| 222 | |
| 223 | client.testEnum(ttypes.Numberz.ONE, function(err, response) { |
| 224 | assert( ! err); |
| 225 | assert.equal(ttypes.Numberz.ONE, response); |
| 226 | }); |
| 227 | |
| 228 | client.testTypedef(69, function(err, response) { |
| 229 | assert( ! err); |
| 230 | assert.equal(69, response); |
| 231 | }); |
| 232 | |
| 233 | var mapMapTest = { |
| 234 | "4": {"1":1, "2":2, "3":3, "4":4}, |
| 235 | "-4": {"-4":-4, "-3":-3, "-2":-2, "-1":-1} |
| 236 | }; |
| 237 | client.testMapMap(1, function(err, response) { |
| 238 | assert( ! err); |
| 239 | assert.deepEqual(mapMapTest, response); |
| 240 | }); |
| 241 | |
| 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 }, |
| 258 | "2":{ "6":{ "userMap":{}, "xtructs":[] } } |
| 259 | }; |
| 260 | client.testInsanity(crazy, function(err, response) { |
| 261 | assert( ! err); |
| 262 | checkRecursively(insanity, response); |
| 263 | }); |
| 264 | |
| 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 | |
| 304 | //We wait up to retry_limit * retry_interval for the test suite to complete |
| 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 { |
| 314 | if (callback) { |
| 315 | callback("Server test failed to complete after " + |
| 316 | (retry_limit*retry_interval/1000) + " seconds"); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | setTimeout(TestForCompletion, retry_interval); |
| 323 | })(); |
| 324 | }; |