blob: 03ec5138b51d5e3cc64d285d4d0bcb6422d7eb52 [file] [log] [blame]
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -08001/*
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.js 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
Randy Abernethyd8187c52015-02-16 01:25:53 -080029var test = require('tape');
30//var assert = require('assert');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080031var ttypes = require('./gen-nodejs/ThriftTest_types');
Randy Abernethybd60b922015-02-26 16:59:14 -080032var TException = require('thrift').Thrift.TException;
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080033var Int64 = require('node-int64');
34var testCases = require('./test-cases');
35
36exports.ThriftTestDriver = function(client, callback) {
37
Randy Abernethyd8187c52015-02-16 01:25:53 -080038 test('NodeJS Style Callback Client Tests', function(assert) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080039
Randy Abernethyd8187c52015-02-16 01:25:53 -080040 var checkRecursively = makeRecursiveCheck(assert);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080041
Randy Abernethyd8187c52015-02-16 01:25:53 -080042 function makeAsserter(assertionFn) {
43 return function(c) {
44 var fnName = c[0];
45 var expected = c[1];
46 client[fnName](expected, function(err, actual) {
47 assert.error(err, fnName + ': no callback error');
48 assertionFn(actual, expected, fnName);
49 })
50 };
51 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080052
Nobuaki Sukegawaf56b9072015-11-23 19:38:18 +090053 testCases.simple.forEach(makeAsserter(function(a, e, m){
54 if (a instanceof Int64) {
55 var e64 = e instanceof Int64 ? e : new Int64(e);
56 assert.deepEqual(a.buffer, e64.buffer, m);
57 } else {
58 assert.equal(a, e, m);
59 }
Randy Abernethyd8187c52015-02-16 01:25:53 -080060 }));
61 testCases.deep.forEach(makeAsserter(assert.deepEqual));
Randy Abernethy983bf7d2015-10-09 12:28:57 -070062 testCases.deepUnordered.forEach(makeAsserter(makeUnorderedDeepEqual(assert)));
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080063
Nobuaki Sukegawa8a4d06f2015-11-06 21:24:26 +090064 var arr = [];
65 for (var i = 0; i < 256; ++i) {
66 arr[i] = 255 - i;
67 }
68 var buf = new Buffer(arr);
69 client.testBinary(buf, function(err, response) {
70 assert.error(err, 'testBinary: no callback error');
71 assert.equal(response.length, 256, 'testBinary');
72 assert.deepEqual(response, buf, 'testBinary(Buffer)');
73 });
74 var buf = new Buffer(arr);
75 client.testBinary(buf.toString('binary'), function(err, response) {
76 assert.error(err, 'testBinary: no callback error');
77 assert.equal(response.length, 256, 'testBinary');
78 assert.deepEqual(response, buf, 'testBinary(string)');
79 });
80
Randy Abernethybd60b922015-02-26 16:59:14 -080081 client.testMapMap(42, function(err, response) {
82 var expected = {
83 "4": {"1":1, "2":2, "3":3, "4":4},
84 "-4": {"-4":-4, "-3":-3, "-2":-2, "-1":-1}
85 };
86 assert.error(err, 'testMapMap: no callback error');
87 assert.deepEqual(expected, response, 'testMapMap');
88 });
89
Randy Abernethyd8187c52015-02-16 01:25:53 -080090 client.testStruct(testCases.out, function(err, response) {
91 assert.error(err, 'testStruct: no callback error');
92 checkRecursively(testCases.out, response, 'testStruct');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080093 });
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080094
Randy Abernethyd8187c52015-02-16 01:25:53 -080095 client.testNest(testCases.out2, function(err, response) {
96 assert.error(err, 'testNest: no callback error');
97 checkRecursively(testCases.out2, response, 'testNest');
98 });
99
100 client.testInsanity(testCases.crazy, function(err, response) {
101 assert.error(err, 'testInsanity: no callback error');
102 checkRecursively(testCases.insanity, response, 'testInsanity');
103 });
104
Henrique Mendonça15d90422015-06-25 22:31:41 +1000105 client.testInsanity(testCases.crazy2, function(err, response) {
106 assert.error(err, 'testInsanity2: no callback error');
107 checkRecursively(testCases.insanity, response, 'testInsanity2');
108 });
109
Randy Abernethyd8187c52015-02-16 01:25:53 -0800110 client.testException('TException', function(err, response) {
Randy Abernethybd60b922015-02-26 16:59:14 -0800111 assert.ok(err instanceof TException, 'testException: correct error type');
Randy Abernethyd8187c52015-02-16 01:25:53 -0800112 assert.ok(!response, 'testException: no response');
113 });
114
115 client.testException('Xception', function(err, response) {
Randy Abernethybd60b922015-02-26 16:59:14 -0800116 assert.ok(err instanceof ttypes.Xception, 'testException: correct error type');
Randy Abernethyd8187c52015-02-16 01:25:53 -0800117 assert.ok(!response, 'testException: no response');
118 assert.equal(err.errorCode, 1001, 'testException: correct error code');
119 assert.equal('Xception', err.message, 'testException: correct error message');
120 });
121
122 client.testException('no Exception', function(err, response) {
123 assert.error(err, 'testException: no callback error');
124 assert.ok(!response, 'testException: no response');
125 });
126
127 client.testOneway(0, function(err, response) {
128 assert.fail('testOneway should not answer');
129 });
130
131 checkOffByOne(function(done) {
132 client.testI32(-1, function(err, response) {
133 assert.error(err, 'checkOffByOne: no callback error');
134 assert.equal(-1, response);
135 assert.end();
136 done();
137 });
138 }, callback);
139
140 });
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800141};
142
143exports.ThriftTestDriverPromise = function(client, callback) {
144
Randy Abernethyd8187c52015-02-16 01:25:53 -0800145 test('Q Promise Client Tests', function(assert) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800146
Randy Abernethyd8187c52015-02-16 01:25:53 -0800147 var checkRecursively = makeRecursiveCheck(assert);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800148
Randy Abernethyd8187c52015-02-16 01:25:53 -0800149 function fail(msg) {
150 return function() {
151 assert.fail(msg);
152 }
153 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800154
Randy Abernethyd8187c52015-02-16 01:25:53 -0800155 function makeAsserter(assertionFn) {
156 return function(c) {
157 var fnName = c[0];
158 var expected = c[1];
159 client[fnName](expected)
160 .then(function(actual) {
161 assertionFn(actual, expected, fnName);
162 })
163 .fail(fail('fnName'));
164 };
165 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800166
Nobuaki Sukegawaf56b9072015-11-23 19:38:18 +0900167 testCases.simple.forEach(makeAsserter(function(a, e, m){
168 if (a instanceof Int64) {
169 var e64 = e instanceof Int64 ? e : new Int64(e);
170 assert.deepEqual(a.buffer, e64.buffer, m);
171 } else {
172 assert.equal(a, e, m);
173 }
Randy Abernethyd8187c52015-02-16 01:25:53 -0800174 }));
175 testCases.deep.forEach(makeAsserter(assert.deepEqual));
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700176 testCases.deepUnordered.forEach(makeAsserter(makeUnorderedDeepEqual(assert)));
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800177
Randy Abernethyd8187c52015-02-16 01:25:53 -0800178 client.testStruct(testCases.out)
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800179 .then(function(response) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800180 checkRecursively(testCases.out, response, 'testStruct');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800181 })
Randy Abernethyd8187c52015-02-16 01:25:53 -0800182 .fail(fail('testStruct'));
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800183
Randy Abernethyd8187c52015-02-16 01:25:53 -0800184 client.testNest(testCases.out2)
185 .then(function(response) {
186 checkRecursively(testCases.out2, response, 'testNest');
187 })
188 .fail(fail('testNest'));
189
190 client.testInsanity(testCases.crazy)
191 .then(function(response) {
192 checkRecursively(testCases.insanity, response, 'testInsanity');
193 })
194 .fail(fail('testInsanity'));
195
Henrique Mendonça15d90422015-06-25 22:31:41 +1000196 client.testInsanity(testCases.crazy2)
197 .then(function(response) {
198 checkRecursively(testCases.insanity, response, 'testInsanity2');
199 })
200 .fail(fail('testInsanity2'));
201
Randy Abernethyd8187c52015-02-16 01:25:53 -0800202 client.testException('TException')
203 .then(function(response) {
Randy Abernethybd60b922015-02-26 16:59:14 -0800204 fail('testException: TException');
Randy Abernethyd8187c52015-02-16 01:25:53 -0800205 })
Randy Abernethybd60b922015-02-26 16:59:14 -0800206 .fail(function(err) {
207 assert.ok(err instanceof TException);
208 });
Randy Abernethyd8187c52015-02-16 01:25:53 -0800209
210 client.testException('Xception')
211 .then(function(response) {
Randy Abernethybd60b922015-02-26 16:59:14 -0800212 fail('testException: Xception');
Randy Abernethyd8187c52015-02-16 01:25:53 -0800213 })
214 .fail(function(err) {
Randy Abernethybd60b922015-02-26 16:59:14 -0800215 assert.ok(err instanceof ttypes.Xception);
Randy Abernethyd8187c52015-02-16 01:25:53 -0800216 assert.equal(err.errorCode, 1001);
217 assert.equal('Xception', err.message);
218 });
219
220 client.testException('no Exception')
221 .then(function(response) {
222 assert.equal(undefined, response); //void
223 })
224 .fail(fail('testException'));
225
226 client.testOneway(0, fail('testOneway: should not answer'));
227
228 checkOffByOne(function(done) {
229 client.testI32(-1)
230 .then(function(response) {
231 assert.equal(-1, response);
232 assert.end();
233 done();
234 })
235 .fail(fail('checkOffByOne'));
236 }, callback);
237 });
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800238};
239
240
241// Helper Functions
242// =========================================================
243
Randy Abernethyd8187c52015-02-16 01:25:53 -0800244function makeRecursiveCheck(assert) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800245
Randy Abernethyd8187c52015-02-16 01:25:53 -0800246 return function (map1, map2, msg) {
247 var equal = true;
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800248
Randy Abernethyd8187c52015-02-16 01:25:53 -0800249 var equal = checkRecursively(map1, map2);
250
251 assert.ok(equal, msg);
252
253 // deepEqual doesn't work with fields using node-int64
254 function checkRecursively(map1, map2) {
255 if (typeof map1 !== 'function' && typeof map2 !== 'function') {
256 if (!map1 || typeof map1 !== 'object') {
257 //Handle int64 types (which use node-int64 in Node.js JavaScript)
258 if ((typeof map1 === "number") && (typeof map2 === "object") &&
259 (map2.buffer) && (map2.buffer instanceof Buffer) && (map2.buffer.length === 8)) {
260 var n = new Int64(map2.buffer);
261 return map1 === n.toNumber();
262 } else {
263 return map1 == map2;
264 }
265 } else {
266 return Object.keys(map1).every(function(key) {
267 return checkRecursively(map1[key], map2[key]);
268 });
269 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800270 }
271 }
272 }
273}
274
275function checkOffByOne(done, callback) {
276
277 var retry_limit = 30;
278 var retry_interval = 100;
279 var test_complete = false;
280 var retrys = 0;
281
282 /**
283 * redo a simple test after the oneway to make sure we aren't "off by one" --
284 * if the server treated oneway void like normal void, this next test will
285 * fail since it will get the void confirmation rather than the correct
286 * result. In this circumstance, the client will throw the exception:
287 *
288 * Because this is the last test against the server, when it completes
289 * the entire suite is complete by definition (the tests run serially).
290 */
291 done(function() {
292 test_complete = true;
293 });
294
295 //We wait up to retry_limit * retry_interval for the test suite to complete
296 function TestForCompletion() {
297 if(test_complete && callback) {
298 callback("Server successfully tested!");
299 } else {
300 if (++retrys < retry_limit) {
301 setTimeout(TestForCompletion, retry_interval);
302 } else if (callback) {
303 callback("Server test failed to complete after " +
304 (retry_limit * retry_interval / 1000) + " seconds");
305 }
306 }
307 }
308
309 setTimeout(TestForCompletion, retry_interval);
310}
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700311
312function makeUnorderedDeepEqual(assert) {
313 return function(actual, expected, name) {
314 assert.equal(actual.length, expected.length, name);
315 for (var k in actual) {
316 var found = false;
317 for (var k2 in expected) {
318 if (actual[k] === expected[k2]) {
319 found = true;
320 }
321 }
322 if (!found) {
323 assert.fail('Unexpected value ' + actual[k] + ' with key ' + k);
324 }
325 }
326 };
327}