blob: b1d744b6c807edb262c77434ab6e91674e9e59fc [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
henrique216374e2014-01-14 15:17:04 +0100174var out = new ttypes.Xtruct({
175 string_thing: 'Zero',
176 byte_thing: 1,
177 i32_thing: -3,
178 i64_thing: 1000000
179});
180client.testStruct(out, function(err, response) {
181 assert( ! err);
182 checkRecursively(out, response);
183});
Jens Geyerb9d55222014-01-10 21:26:25 +0100184
henrique216374e2014-01-14 15:17:04 +0100185var out2 = new ttypes.Xtruct2();
186out2.byte_thing = 1;
187out2.struct_thing = out;
188out2.i32_thing = 5;
189client.testNest(out2, function(err, response) {
190 assert( ! err);
191 checkRecursively(out2, response);
192});
Jens Geyerb9d55222014-01-10 21:26:25 +0100193
henrique216374e2014-01-14 15:17:04 +0100194var mapout = {};
195for (var i = 0; i < 5; ++i) {
196 mapout[i] = i-10;
197}
198client.testMap(mapout, function(err, response) {
199 assert( ! err);
200 assert.deepEqual(mapout, response);
201});
Jens Geyerb9d55222014-01-10 21:26:25 +0100202
henrique216374e2014-01-14 15:17:04 +0100203var mapTestInput = {
204 "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
ra20aeba32014-05-11 00:25:01 -0700205 "longValue":stringTest, "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, ":"long key"
henrique216374e2014-01-14 15:17:04 +0100206};
207client.testStringMap(mapTestInput, function(err, response) {
208 assert( ! err);
209 assert.deepEqual(mapTestInput, response);
210});
Jens Geyerb9d55222014-01-10 21:26:25 +0100211
ra20aeba32014-05-11 00:25:01 -0700212var setTestSetInput = [1,2,3];
213client.testSet(setTestSetInput, function(err, response) {
henrique216374e2014-01-14 15:17:04 +0100214 assert( ! err);
ra20aeba32014-05-11 00:25:01 -0700215 assert.deepEqual(setTestSetInput, response);
henrique216374e2014-01-14 15:17:04 +0100216});
ra20aeba32014-05-11 00:25:01 -0700217var setTestListInput = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
218client.testList(setTestListInput, function(err, response) {
henrique216374e2014-01-14 15:17:04 +0100219 assert( ! err);
ra20aeba32014-05-11 00:25:01 -0700220 assert.deepEqual(setTestListInput, response);
henrique216374e2014-01-14 15:17:04 +0100221});
Jens Geyerb9d55222014-01-10 21:26:25 +0100222
henrique216374e2014-01-14 15:17:04 +0100223client.testEnum(ttypes.Numberz.ONE, function(err, response) {
224 assert( ! err);
225 assert.equal(ttypes.Numberz.ONE, response);
226});
Jens Geyerb9d55222014-01-10 21:26:25 +0100227
henrique216374e2014-01-14 15:17:04 +0100228client.testTypedef(69, function(err, response) {
229 assert( ! err);
230 assert.equal(69, response);
231});
Jens Geyerb9d55222014-01-10 21:26:25 +0100232
henrique216374e2014-01-14 15:17:04 +0100233var mapMapTest = {
234 "4": {"1":1, "2":2, "3":3, "4":4},
235 "-4": {"-4":-4, "-3":-3, "-2":-2, "-1":-1}
236};
ra20aeba32014-05-11 00:25:01 -0700237client.testMapMap(1, function(err, response) {
henrique216374e2014-01-14 15:17:04 +0100238 assert( ! err);
239 assert.deepEqual(mapMapTest, response);
240});
Jens Geyerb9d55222014-01-10 21:26:25 +0100241
henrique216374e2014-01-14 15:17:04 +0100242var 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});
256var insanity = {
257 "1":{ "2": crazy, "3": crazy },
ra7f76e1d2014-04-02 12:31:05 -0700258 "2":{ "6":{ "userMap":{}, "xtructs":[] } }
henrique216374e2014-01-14 15:17:04 +0100259};
260client.testInsanity(crazy, function(err, response) {
261 assert( ! err);
262 checkRecursively(insanity, response);
263});
Jens Geyerb9d55222014-01-10 21:26:25 +0100264
henrique216374e2014-01-14 15:17:04 +0100265client.testException('TException', function(err, response) {
266 assert( ! response);
267});
268
269client.testException('Xception', function(err, response) {
270 assert( ! response);
271 assert.equal(err.errorCode, 1001);
272 assert.equal('Xception', err.message);
273});
274
275client.testException('no Exception', function(err, response) {
276 assert( ! err);
277 assert.equal(undefined, response); //void
278});
279
280client.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
ra8f697cb2014-04-23 02:23:18 -0700304 //We wait up to retry_limit * retry_interval for the test suite to complete
henrique216374e2014-01-14 15:17:04 +0100305 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 {
ra8f697cb2014-04-23 02:23:18 -0700314 if (callback) {
315 callback("Server test failed to complete after " +
316 (retry_limit*retry_interval/1000) + " seconds");
317 }
henrique216374e2014-01-14 15:17:04 +0100318 }
319 }
320 }
321
322 setTimeout(TestForCompletion, retry_interval);
323})();
ra8f697cb2014-04-23 02:23:18 -0700324};