blob: b8c900bb1df51456614434f6881d7265b98489bc [file] [log] [blame]
Jens Geyerb9d55222014-01-10 21:26:25 +01001/*
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
ra7f76e1d2014-04-02 12:31:05 -070020 // This is the Node.js test driver for the standard Apache Thrift
Jens Geyerb9d55222014-01-10 21:26:25 +010021 // 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
29var assert = require('assert');
30var ttypes = require('./gen-nodejs/ThriftTest_types');
ra7f76e1d2014-04-02 12:31:05 -070031var Int64 = require('node-int64');
Jens Geyerb9d55222014-01-10 21:26:25 +010032
33var ThriftTestDriver = exports.ThriftTestDriver = function(client, callback) {
34
henrique216374e2014-01-14 15:17:04 +010035function checkRecursively(map1, map2) {
36 if (typeof map1 !== 'function' && typeof map2 !== 'function') {
37 if (!map1 || typeof map1 !== 'object') {
ra7f76e1d2014-04-02 12:31:05 -070038 //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 {
henrique216374e2014-01-14 15:17:04 +010044 assert.equal(map1, map2);
ra7f76e1d2014-04-02 12:31:05 -070045 }
henrique216374e2014-01-14 15:17:04 +010046 } else {
47 for (var key in map1) {
48 checkRecursively(map1[key], map2[key]);
49 }
50 }
51 }
52}
Jens Geyerb9d55222014-01-10 21:26:25 +010053
henrique216374e2014-01-14 15:17:04 +010054client.testVoid(function(err, response) {
55 assert( ! err);
56 assert.equal(undefined, response); //void
57});
Jens Geyerb9d55222014-01-10 21:26:25 +010058
henrique216374e2014-01-14 15:17:04 +010059client.testString("Test", function(err, response) {
60 assert( ! err);
61 assert.equal("Test", response);
62});
Jens Geyerb9d55222014-01-10 21:26:25 +010063
henrique216374e2014-01-14 15:17:04 +010064client.testString("", function(err, response) {
65 assert( ! err);
66 assert.equal("", response);
67});
Jens Geyerb9d55222014-01-10 21:26:25 +010068
henrique216374e2014-01-14 15:17:04 +010069//all Languages in UTF-8
ra779b9ac2014-04-23 20:04:23 -070070/*jshint -W100 */
henrique216374e2014-01-14 15:17:04 +010071var 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ú, 粵語";
ra779b9ac2014-04-23 20:04:23 -070096/*jshint +W100 */
Jens Geyerb9d55222014-01-10 21:26:25 +010097
henrique216374e2014-01-14 15:17:04 +010098client.testString(stringTest, function(err, response) {
99 assert( ! err);
100 assert.equal(stringTest, response);
101});
Jens Geyerb9d55222014-01-10 21:26:25 +0100102
henrique216374e2014-01-14 15:17:04 +0100103var 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: ]] \"]] \\" }}}{ [[[ ';
109client.testString(specialCharacters, function(err, response) {
110 assert( ! err);
111 assert.equal(specialCharacters, response);
112});
Jens Geyerb9d55222014-01-10 21:26:25 +0100113
henrique216374e2014-01-14 15:17:04 +0100114client.testByte(1, function(err, response) {
115 assert( ! err);
116 assert.equal(1, response);
117});
Jens Geyerb9d55222014-01-10 21:26:25 +0100118
henrique216374e2014-01-14 15:17:04 +0100119client.testByte(0, function(err, response) {
120 assert( ! err);
121 assert.equal(0, response);
122});
Jens Geyerb9d55222014-01-10 21:26:25 +0100123
henrique216374e2014-01-14 15:17:04 +0100124client.testByte(-1, function(err, response) {
125 assert( ! err);
126 assert.equal(-1, response);
127});
Jens Geyerb9d55222014-01-10 21:26:25 +0100128
henrique216374e2014-01-14 15:17:04 +0100129client.testByte(-127, function(err, response) {
130 assert( ! err);
131 assert.equal(-127, response);
132});
Jens Geyerb9d55222014-01-10 21:26:25 +0100133
henrique216374e2014-01-14 15:17:04 +0100134client.testI32(-1, function(err, response) {
135 assert( ! err);
136 assert.equal(-1, response);
137});
Jens Geyerb9d55222014-01-10 21:26:25 +0100138
henrique216374e2014-01-14 15:17:04 +0100139client.testI64(5, function(err, response) {
140 assert( ! err);
141 assert.equal(5, response);
142});
Jens Geyerb9d55222014-01-10 21:26:25 +0100143
henrique216374e2014-01-14 15:17:04 +0100144client.testI64(-5, function(err, response) {
145 assert( ! err);
146 assert.equal(-5, response);
147});
Jens Geyerb9d55222014-01-10 21:26:25 +0100148
ra20aeba32014-05-11 00:25:01 -0700149client.testI64(734359738368, function(err, response) {
150 assert( ! err);
151 assert.equal(734359738368, response);
152});
153
henrique216374e2014-01-14 15:17:04 +0100154client.testI64(-34359738368, function(err, response) {
155 assert( ! err);
156 assert.equal(-34359738368, response);
157});
Jens Geyerb9d55222014-01-10 21:26:25 +0100158
ra20aeba32014-05-11 00:25:01 -0700159client.testI64(-734359738368, function(err, response) {
160 assert( ! err);
161 assert.equal(-734359738368, response);
162});
163
henrique216374e2014-01-14 15:17:04 +0100164client.testDouble(-5.2098523, function(err, response) {
165 assert( ! err);
166 assert.equal(-5.2098523, response);
167});
Jens Geyerb9d55222014-01-10 21:26:25 +0100168
henrique216374e2014-01-14 15:17:04 +0100169client.testDouble(7.012052175215044, function(err, response) {
170 assert( ! err);
171 assert.equal(7.012052175215044, response);
172});
Jens Geyerb9d55222014-01-10 21:26:25 +0100173
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100174// TODO: add testBinary()
175
henrique216374e2014-01-14 15:17:04 +0100176var out = new ttypes.Xtruct({
177 string_thing: 'Zero',
178 byte_thing: 1,
179 i32_thing: -3,
180 i64_thing: 1000000
181});
182client.testStruct(out, function(err, response) {
183 assert( ! err);
184 checkRecursively(out, response);
185});
Jens Geyerb9d55222014-01-10 21:26:25 +0100186
henrique216374e2014-01-14 15:17:04 +0100187var out2 = new ttypes.Xtruct2();
188out2.byte_thing = 1;
189out2.struct_thing = out;
190out2.i32_thing = 5;
191client.testNest(out2, function(err, response) {
192 assert( ! err);
193 checkRecursively(out2, response);
194});
Jens Geyerb9d55222014-01-10 21:26:25 +0100195
henrique216374e2014-01-14 15:17:04 +0100196var mapout = {};
197for (var i = 0; i < 5; ++i) {
198 mapout[i] = i-10;
199}
200client.testMap(mapout, function(err, response) {
201 assert( ! err);
202 assert.deepEqual(mapout, response);
203});
Jens Geyerb9d55222014-01-10 21:26:25 +0100204
henrique216374e2014-01-14 15:17:04 +0100205var mapTestInput = {
206 "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
ra20aeba32014-05-11 00:25:01 -0700207 "longValue":stringTest, "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, ":"long key"
henrique216374e2014-01-14 15:17:04 +0100208};
209client.testStringMap(mapTestInput, function(err, response) {
210 assert( ! err);
211 assert.deepEqual(mapTestInput, response);
212});
Jens Geyerb9d55222014-01-10 21:26:25 +0100213
ra20aeba32014-05-11 00:25:01 -0700214var setTestSetInput = [1,2,3];
215client.testSet(setTestSetInput, function(err, response) {
henrique216374e2014-01-14 15:17:04 +0100216 assert( ! err);
ra20aeba32014-05-11 00:25:01 -0700217 assert.deepEqual(setTestSetInput, response);
henrique216374e2014-01-14 15:17:04 +0100218});
ra20aeba32014-05-11 00:25:01 -0700219var setTestListInput = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
220client.testList(setTestListInput, function(err, response) {
henrique216374e2014-01-14 15:17:04 +0100221 assert( ! err);
ra20aeba32014-05-11 00:25:01 -0700222 assert.deepEqual(setTestListInput, response);
henrique216374e2014-01-14 15:17:04 +0100223});
Jens Geyerb9d55222014-01-10 21:26:25 +0100224
henrique216374e2014-01-14 15:17:04 +0100225client.testEnum(ttypes.Numberz.ONE, function(err, response) {
226 assert( ! err);
227 assert.equal(ttypes.Numberz.ONE, response);
228});
Jens Geyerb9d55222014-01-10 21:26:25 +0100229
henrique216374e2014-01-14 15:17:04 +0100230client.testTypedef(69, function(err, response) {
231 assert( ! err);
232 assert.equal(69, response);
233});
Jens Geyerb9d55222014-01-10 21:26:25 +0100234
henrique216374e2014-01-14 15:17:04 +0100235var mapMapTest = {
236 "4": {"1":1, "2":2, "3":3, "4":4},
237 "-4": {"-4":-4, "-3":-3, "-2":-2, "-1":-1}
238};
ra20aeba32014-05-11 00:25:01 -0700239client.testMapMap(1, function(err, response) {
henrique216374e2014-01-14 15:17:04 +0100240 assert( ! err);
241 assert.deepEqual(mapMapTest, response);
242});
Jens Geyerb9d55222014-01-10 21:26:25 +0100243
henrique216374e2014-01-14 15:17:04 +0100244var crazy = new ttypes.Insanity({
245 "userMap":{ "5":5, "8":8 },
246 "xtructs":[new ttypes.Xtruct({
247 "string_thing":"Goodbye4",
248 "byte_thing":4,
249 "i32_thing":4,
250 "i64_thing":4
251 }), new ttypes.Xtruct({
252 "string_thing":"Hello2",
253 "byte_thing":2,
254 "i32_thing":2,
255 "i64_thing":2
256 })]
257});
258var insanity = {
259 "1":{ "2": crazy, "3": crazy },
ra7f76e1d2014-04-02 12:31:05 -0700260 "2":{ "6":{ "userMap":{}, "xtructs":[] } }
henrique216374e2014-01-14 15:17:04 +0100261};
262client.testInsanity(crazy, function(err, response) {
263 assert( ! err);
264 checkRecursively(insanity, response);
265});
Jens Geyerb9d55222014-01-10 21:26:25 +0100266
henrique216374e2014-01-14 15:17:04 +0100267client.testException('TException', function(err, response) {
268 assert( ! response);
269});
270
271client.testException('Xception', function(err, response) {
272 assert( ! response);
273 assert.equal(err.errorCode, 1001);
274 assert.equal('Xception', err.message);
275});
276
277client.testException('no Exception', function(err, response) {
278 assert( ! err);
279 assert.equal(undefined, response); //void
280});
281
282client.testOneway(0, function(err, response) {
283 assert(false); //should not answer
284});
285
286(function() {
287 var test_complete = false;
288 var retrys = 0;
289 var retry_limit = 30;
290 var retry_interval = 100;
291 /**
292 * redo a simple test after the oneway to make sure we aren't "off by one" --
293 * if the server treated oneway void like normal void, this next test will
294 * fail since it will get the void confirmation rather than the correct
295 * result. In this circumstance, the client will throw the exception:
296 *
297 * Because this is the last test against the server, when it completes
298 * the entire suite is complete by definition (the tests run serially).
299 */
300 client.testI32(-1, function(err, response) {
301 assert( ! err);
302 assert.equal(-1, response);
303 test_complete = true;
304 });
305
ra8f697cb2014-04-23 02:23:18 -0700306 //We wait up to retry_limit * retry_interval for the test suite to complete
henrique216374e2014-01-14 15:17:04 +0100307 function TestForCompletion() {
308 if(test_complete) {
309 if (callback) {
310 callback("Server successfully tested!");
311 }
312 } else {
313 if (++retrys < retry_limit) {
314 setTimeout(TestForCompletion, retry_interval);
315 } else {
ra8f697cb2014-04-23 02:23:18 -0700316 if (callback) {
317 callback("Server test failed to complete after " +
318 (retry_limit*retry_interval/1000) + " seconds");
319 }
henrique216374e2014-01-14 15:17:04 +0100320 }
321 }
322 }
323
324 setTimeout(TestForCompletion, retry_interval);
325})();
ra8f697cb2014-04-23 02:23:18 -0700326};