blob: 02613ec6268439dbc7e38e919658771c8eb8d4f5 [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
henrique216374e2014-01-14 15:17:04 +0100149client.testI64(-34359738368, function(err, response) {
150 assert( ! err);
151 assert.equal(-34359738368, response);
152});
Jens Geyerb9d55222014-01-10 21:26:25 +0100153
henrique216374e2014-01-14 15:17:04 +0100154client.testDouble(-5.2098523, function(err, response) {
155 assert( ! err);
156 assert.equal(-5.2098523, response);
157});
Jens Geyerb9d55222014-01-10 21:26:25 +0100158
henrique216374e2014-01-14 15:17:04 +0100159client.testDouble(7.012052175215044, function(err, response) {
160 assert( ! err);
161 assert.equal(7.012052175215044, response);
162});
Jens Geyerb9d55222014-01-10 21:26:25 +0100163
henrique216374e2014-01-14 15:17:04 +0100164var out = new ttypes.Xtruct({
165 string_thing: 'Zero',
166 byte_thing: 1,
167 i32_thing: -3,
168 i64_thing: 1000000
169});
170client.testStruct(out, function(err, response) {
171 assert( ! err);
172 checkRecursively(out, response);
173});
Jens Geyerb9d55222014-01-10 21:26:25 +0100174
henrique216374e2014-01-14 15:17:04 +0100175var out2 = new ttypes.Xtruct2();
176out2.byte_thing = 1;
177out2.struct_thing = out;
178out2.i32_thing = 5;
179client.testNest(out2, function(err, response) {
180 assert( ! err);
181 checkRecursively(out2, response);
182});
Jens Geyerb9d55222014-01-10 21:26:25 +0100183
henrique216374e2014-01-14 15:17:04 +0100184var mapout = {};
185for (var i = 0; i < 5; ++i) {
186 mapout[i] = i-10;
187}
188client.testMap(mapout, function(err, response) {
189 assert( ! err);
190 assert.deepEqual(mapout, response);
191});
Jens Geyerb9d55222014-01-10 21:26:25 +0100192
henrique216374e2014-01-14 15:17:04 +0100193var mapTestInput = {
194 "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
195 "longValue":stringTest, stringTest:"long key"
196};
197client.testStringMap(mapTestInput, function(err, response) {
198 assert( ! err);
199 assert.deepEqual(mapTestInput, response);
200});
Jens Geyerb9d55222014-01-10 21:26:25 +0100201
henrique216374e2014-01-14 15:17:04 +0100202var setTestInput = [1,2,3];
203client.testSet(setTestInput, function(err, response) {
204 assert( ! err);
205 assert.deepEqual(setTestInput, response);
206});
207client.testList(setTestInput, function(err, response) {
208 assert( ! err);
209 assert.deepEqual(setTestInput, response);
210});
Jens Geyerb9d55222014-01-10 21:26:25 +0100211
henrique216374e2014-01-14 15:17:04 +0100212client.testEnum(ttypes.Numberz.ONE, function(err, response) {
213 assert( ! err);
214 assert.equal(ttypes.Numberz.ONE, response);
215});
Jens Geyerb9d55222014-01-10 21:26:25 +0100216
henrique216374e2014-01-14 15:17:04 +0100217client.testTypedef(69, function(err, response) {
218 assert( ! err);
219 assert.equal(69, response);
220});
Jens Geyerb9d55222014-01-10 21:26:25 +0100221
henrique216374e2014-01-14 15:17:04 +0100222var mapMapTest = {
223 "4": {"1":1, "2":2, "3":3, "4":4},
224 "-4": {"-4":-4, "-3":-3, "-2":-2, "-1":-1}
225};
226client.testMapMap(mapMapTest, function(err, response) {
227 assert( ! err);
228 assert.deepEqual(mapMapTest, response);
229});
Jens Geyerb9d55222014-01-10 21:26:25 +0100230
henrique216374e2014-01-14 15:17:04 +0100231var crazy = new ttypes.Insanity({
232 "userMap":{ "5":5, "8":8 },
233 "xtructs":[new ttypes.Xtruct({
234 "string_thing":"Goodbye4",
235 "byte_thing":4,
236 "i32_thing":4,
237 "i64_thing":4
238 }), new ttypes.Xtruct({
239 "string_thing":"Hello2",
240 "byte_thing":2,
241 "i32_thing":2,
242 "i64_thing":2
243 })]
244});
245var insanity = {
246 "1":{ "2": crazy, "3": crazy },
ra7f76e1d2014-04-02 12:31:05 -0700247 "2":{ "6":{ "userMap":{}, "xtructs":[] } }
henrique216374e2014-01-14 15:17:04 +0100248};
249client.testInsanity(crazy, function(err, response) {
250 assert( ! err);
251 checkRecursively(insanity, response);
252});
Jens Geyerb9d55222014-01-10 21:26:25 +0100253
henrique216374e2014-01-14 15:17:04 +0100254client.testException('TException', function(err, response) {
255 assert( ! response);
256});
257
258client.testException('Xception', function(err, response) {
259 assert( ! response);
260 assert.equal(err.errorCode, 1001);
261 assert.equal('Xception', err.message);
262});
263
264client.testException('no Exception', function(err, response) {
265 assert( ! err);
266 assert.equal(undefined, response); //void
267});
268
269client.testOneway(0, function(err, response) {
270 assert(false); //should not answer
271});
272
273(function() {
274 var test_complete = false;
275 var retrys = 0;
276 var retry_limit = 30;
277 var retry_interval = 100;
278 /**
279 * redo a simple test after the oneway to make sure we aren't "off by one" --
280 * if the server treated oneway void like normal void, this next test will
281 * fail since it will get the void confirmation rather than the correct
282 * result. In this circumstance, the client will throw the exception:
283 *
284 * Because this is the last test against the server, when it completes
285 * the entire suite is complete by definition (the tests run serially).
286 */
287 client.testI32(-1, function(err, response) {
288 assert( ! err);
289 assert.equal(-1, response);
290 test_complete = true;
291 });
292
ra8f697cb2014-04-23 02:23:18 -0700293 //We wait up to retry_limit * retry_interval for the test suite to complete
henrique216374e2014-01-14 15:17:04 +0100294 function TestForCompletion() {
295 if(test_complete) {
296 if (callback) {
297 callback("Server successfully tested!");
298 }
299 } else {
300 if (++retrys < retry_limit) {
301 setTimeout(TestForCompletion, retry_interval);
302 } else {
ra8f697cb2014-04-23 02:23:18 -0700303 if (callback) {
304 callback("Server test failed to complete after " +
305 (retry_limit*retry_interval/1000) + " seconds");
306 }
henrique216374e2014-01-14 15:17:04 +0100307 }
308 }
309 }
310
311 setTimeout(TestForCompletion, retry_interval);
312})();
ra8f697cb2014-04-23 02:23:18 -0700313};