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