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