blob: c61e99d44e01ea3a49a03a3ad9ef53e739b80864 [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
20 // This is the Node test driver for the standard Apache Thrift
21 // 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');
31
32var ThriftTestDriver = exports.ThriftTestDriver = function(client, callback) {
33
henrique216374e2014-01-14 15:17:04 +010034// deepEqual doesn't work with fields using node-int64
35function checkRecursively(map1, map2) {
36 if (typeof map1 !== 'function' && typeof map2 !== 'function') {
37 if (!map1 || typeof map1 !== 'object') {
38 assert.equal(map1, map2);
39 } else {
40 for (var key in map1) {
41 checkRecursively(map1[key], map2[key]);
42 }
43 }
44 }
45}
Jens Geyerb9d55222014-01-10 21:26:25 +010046
henrique216374e2014-01-14 15:17:04 +010047client.testVoid(function(err, response) {
48 assert( ! err);
49 assert.equal(undefined, response); //void
50});
Jens Geyerb9d55222014-01-10 21:26:25 +010051
henrique216374e2014-01-14 15:17:04 +010052client.testString("Test", function(err, response) {
53 assert( ! err);
54 assert.equal("Test", response);
55});
Jens Geyerb9d55222014-01-10 21:26:25 +010056
henrique216374e2014-01-14 15:17:04 +010057client.testString("", function(err, response) {
58 assert( ! err);
59 assert.equal("", response);
60});
Jens Geyerb9d55222014-01-10 21:26:25 +010061
henrique216374e2014-01-14 15:17:04 +010062//all Languages in UTF-8
63var stringTest = "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, " +
64 "Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, " +
65 "Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, " +
66 "বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, " +
67 "Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, " +
68 "Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, " +
69 "Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, " +
70 "Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, " +
71 "Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, " +
72 "Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, " +
73 "Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, " +
74 "ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, " +
75 "Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, " +
76 "Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa " +
77 "Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, مازِرونی, Bahasa " +
78 "Melayu, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, ‪" +
79 "Norsk (nynorsk)‬, ‪Norsk (bokmål)‬, Nouormand, Diné bizaad, " +
80 "Occitan, Иронау, Papiamentu, Deitsch, Polski, پنجابی, پښتو, " +
81 "Norfuk / Pitkern, Português, Runa Simi, Rumantsch, Romani, Română, " +
82 "Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple " +
83 "English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, " +
84 "Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, " +
85 "Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, " +
86 "Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, " +
87 "Bân-lâm-gú, 粵語";
Jens Geyerb9d55222014-01-10 21:26:25 +010088
henrique216374e2014-01-14 15:17:04 +010089client.testString(stringTest, function(err, response) {
90 assert( ! err);
91 assert.equal(stringTest, response);
92});
Jens Geyerb9d55222014-01-10 21:26:25 +010093
henrique216374e2014-01-14 15:17:04 +010094var specialCharacters = 'quote: \" backslash:' +
95 ' forwardslash-escaped: \/ ' +
96 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
97 ' now-all-of-them-together: "\\\/\b\n\r\t' +
98 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><' +
99 ' char-to-test-json-parsing: ]] \"]] \\" }}}{ [[[ ';
100client.testString(specialCharacters, function(err, response) {
101 assert( ! err);
102 assert.equal(specialCharacters, response);
103});
Jens Geyerb9d55222014-01-10 21:26:25 +0100104
henrique216374e2014-01-14 15:17:04 +0100105client.testByte(1, function(err, response) {
106 assert( ! err);
107 assert.equal(1, response);
108});
Jens Geyerb9d55222014-01-10 21:26:25 +0100109
henrique216374e2014-01-14 15:17:04 +0100110client.testByte(0, function(err, response) {
111 assert( ! err);
112 assert.equal(0, response);
113});
Jens Geyerb9d55222014-01-10 21:26:25 +0100114
henrique216374e2014-01-14 15:17:04 +0100115client.testByte(-1, function(err, response) {
116 assert( ! err);
117 assert.equal(-1, response);
118});
Jens Geyerb9d55222014-01-10 21:26:25 +0100119
henrique216374e2014-01-14 15:17:04 +0100120client.testByte(-127, function(err, response) {
121 assert( ! err);
122 assert.equal(-127, response);
123});
Jens Geyerb9d55222014-01-10 21:26:25 +0100124
henrique216374e2014-01-14 15:17:04 +0100125client.testI32(-1, function(err, response) {
126 assert( ! err);
127 assert.equal(-1, response);
128});
Jens Geyerb9d55222014-01-10 21:26:25 +0100129
henrique216374e2014-01-14 15:17:04 +0100130client.testI64(5, function(err, response) {
131 assert( ! err);
132 assert.equal(5, response);
133});
Jens Geyerb9d55222014-01-10 21:26:25 +0100134
henrique216374e2014-01-14 15:17:04 +0100135client.testI64(-5, function(err, response) {
136 assert( ! err);
137 assert.equal(-5, response);
138});
Jens Geyerb9d55222014-01-10 21:26:25 +0100139
henrique216374e2014-01-14 15:17:04 +0100140client.testI64(-34359738368, function(err, response) {
141 assert( ! err);
142 assert.equal(-34359738368, response);
143});
Jens Geyerb9d55222014-01-10 21:26:25 +0100144
henrique216374e2014-01-14 15:17:04 +0100145client.testDouble(-5.2098523, function(err, response) {
146 assert( ! err);
147 assert.equal(-5.2098523, response);
148});
Jens Geyerb9d55222014-01-10 21:26:25 +0100149
henrique216374e2014-01-14 15:17:04 +0100150client.testDouble(7.012052175215044, function(err, response) {
151 assert( ! err);
152 assert.equal(7.012052175215044, response);
153});
Jens Geyerb9d55222014-01-10 21:26:25 +0100154
henrique216374e2014-01-14 15:17:04 +0100155var out = new ttypes.Xtruct({
156 string_thing: 'Zero',
157 byte_thing: 1,
158 i32_thing: -3,
159 i64_thing: 1000000
160});
161client.testStruct(out, function(err, response) {
162 assert( ! err);
163 checkRecursively(out, response);
164});
Jens Geyerb9d55222014-01-10 21:26:25 +0100165
henrique216374e2014-01-14 15:17:04 +0100166var out2 = new ttypes.Xtruct2();
167out2.byte_thing = 1;
168out2.struct_thing = out;
169out2.i32_thing = 5;
170client.testNest(out2, function(err, response) {
171 assert( ! err);
172 checkRecursively(out2, response);
173});
Jens Geyerb9d55222014-01-10 21:26:25 +0100174
henrique216374e2014-01-14 15:17:04 +0100175var mapout = {};
176for (var i = 0; i < 5; ++i) {
177 mapout[i] = i-10;
178}
179client.testMap(mapout, function(err, response) {
180 assert( ! err);
181 assert.deepEqual(mapout, response);
182});
Jens Geyerb9d55222014-01-10 21:26:25 +0100183
henrique216374e2014-01-14 15:17:04 +0100184var mapTestInput = {
185 "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
186 "longValue":stringTest, stringTest:"long key"
187};
188client.testStringMap(mapTestInput, function(err, response) {
189 assert( ! err);
190 assert.deepEqual(mapTestInput, response);
191});
Jens Geyerb9d55222014-01-10 21:26:25 +0100192
henrique216374e2014-01-14 15:17:04 +0100193var setTestInput = [1,2,3];
194client.testSet(setTestInput, function(err, response) {
195 assert( ! err);
196 assert.deepEqual(setTestInput, response);
197});
198client.testList(setTestInput, function(err, response) {
199 assert( ! err);
200 assert.deepEqual(setTestInput, response);
201});
Jens Geyerb9d55222014-01-10 21:26:25 +0100202
henrique216374e2014-01-14 15:17:04 +0100203client.testEnum(ttypes.Numberz.ONE, function(err, response) {
204 assert( ! err);
205 assert.equal(ttypes.Numberz.ONE, response);
206});
Jens Geyerb9d55222014-01-10 21:26:25 +0100207
henrique216374e2014-01-14 15:17:04 +0100208client.testTypedef(69, function(err, response) {
209 assert( ! err);
210 assert.equal(69, response);
211});
Jens Geyerb9d55222014-01-10 21:26:25 +0100212
henrique216374e2014-01-14 15:17:04 +0100213var mapMapTest = {
214 "4": {"1":1, "2":2, "3":3, "4":4},
215 "-4": {"-4":-4, "-3":-3, "-2":-2, "-1":-1}
216};
217client.testMapMap(mapMapTest, function(err, response) {
218 assert( ! err);
219 assert.deepEqual(mapMapTest, response);
220});
Jens Geyerb9d55222014-01-10 21:26:25 +0100221
henrique216374e2014-01-14 15:17:04 +0100222var crazy = new ttypes.Insanity({
223 "userMap":{ "5":5, "8":8 },
224 "xtructs":[new ttypes.Xtruct({
225 "string_thing":"Goodbye4",
226 "byte_thing":4,
227 "i32_thing":4,
228 "i64_thing":4
229 }), new ttypes.Xtruct({
230 "string_thing":"Hello2",
231 "byte_thing":2,
232 "i32_thing":2,
233 "i64_thing":2
234 })]
235});
236var insanity = {
237 "1":{ "2": crazy, "3": crazy },
238 "2":{ "6":{ "userMap":null, "xtructs":null } }
239};
240client.testInsanity(crazy, function(err, response) {
241 assert( ! err);
242 checkRecursively(insanity, response);
243});
Jens Geyerb9d55222014-01-10 21:26:25 +0100244
henrique216374e2014-01-14 15:17:04 +0100245client.testException('TException', function(err, response) {
246 assert( ! response);
247});
248
249client.testException('Xception', function(err, response) {
250 assert( ! response);
251 assert.equal(err.errorCode, 1001);
252 assert.equal('Xception', err.message);
253});
254
255client.testException('no Exception', function(err, response) {
256 assert( ! err);
257 assert.equal(undefined, response); //void
258});
259
260client.testOneway(0, function(err, response) {
261 assert(false); //should not answer
262});
263
264(function() {
265 var test_complete = false;
266 var retrys = 0;
267 var retry_limit = 30;
268 var retry_interval = 100;
269 /**
270 * redo a simple test after the oneway to make sure we aren't "off by one" --
271 * if the server treated oneway void like normal void, this next test will
272 * fail since it will get the void confirmation rather than the correct
273 * result. In this circumstance, the client will throw the exception:
274 *
275 * Because this is the last test against the server, when it completes
276 * the entire suite is complete by definition (the tests run serially).
277 */
278 client.testI32(-1, function(err, response) {
279 assert( ! err);
280 assert.equal(-1, response);
281 test_complete = true;
282 });
283
284//We wait up to retry_limit * retry_interval for the test suite to complete
285 function TestForCompletion() {
286 if(test_complete) {
287 if (callback) {
288 callback("Server successfully tested!");
289 }
290 } else {
291 if (++retrys < retry_limit) {
292 setTimeout(TestForCompletion, retry_interval);
293 } else {
294 if (callback) {
295 callback("Server test failed to complete after " +
296 (retry_limit*retry_interval/1000) + " seconds");
297 }
298 }
299 }
300 }
301
302 setTimeout(TestForCompletion, retry_interval);
303})();
304}