blob: 6e472adf41c64474d428c7e8e43c731f6ae5cb6d [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
Randy Abernethyd8187c52015-02-16 01:25:53 -080053 testCases.simple.forEach(makeAsserter(assert.equal));
54 testCases.simpleLoose.forEach(makeAsserter(function(a, e, m){
55 assert.ok(a == e, m);
56 }));
57 testCases.deep.forEach(makeAsserter(assert.deepEqual));
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080058
Randy Abernethybd60b922015-02-26 16:59:14 -080059 client.testMapMap(42, function(err, response) {
60 var expected = {
61 "4": {"1":1, "2":2, "3":3, "4":4},
62 "-4": {"-4":-4, "-3":-3, "-2":-2, "-1":-1}
63 };
64 assert.error(err, 'testMapMap: no callback error');
65 assert.deepEqual(expected, response, 'testMapMap');
66 });
67
Randy Abernethyd8187c52015-02-16 01:25:53 -080068 client.testStruct(testCases.out, function(err, response) {
69 assert.error(err, 'testStruct: no callback error');
70 checkRecursively(testCases.out, response, 'testStruct');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080071 });
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080072
Randy Abernethyd8187c52015-02-16 01:25:53 -080073 client.testNest(testCases.out2, function(err, response) {
74 assert.error(err, 'testNest: no callback error');
75 checkRecursively(testCases.out2, response, 'testNest');
76 });
77
78 client.testInsanity(testCases.crazy, function(err, response) {
79 assert.error(err, 'testInsanity: no callback error');
80 checkRecursively(testCases.insanity, response, 'testInsanity');
81 });
82
83 client.testException('TException', function(err, response) {
Randy Abernethybd60b922015-02-26 16:59:14 -080084 assert.ok(err instanceof TException, 'testException: correct error type');
Randy Abernethyd8187c52015-02-16 01:25:53 -080085 assert.ok(!response, 'testException: no response');
86 });
87
88 client.testException('Xception', function(err, response) {
Randy Abernethybd60b922015-02-26 16:59:14 -080089 assert.ok(err instanceof ttypes.Xception, 'testException: correct error type');
Randy Abernethyd8187c52015-02-16 01:25:53 -080090 assert.ok(!response, 'testException: no response');
91 assert.equal(err.errorCode, 1001, 'testException: correct error code');
92 assert.equal('Xception', err.message, 'testException: correct error message');
93 });
94
95 client.testException('no Exception', function(err, response) {
96 assert.error(err, 'testException: no callback error');
97 assert.ok(!response, 'testException: no response');
98 });
99
100 client.testOneway(0, function(err, response) {
101 assert.fail('testOneway should not answer');
102 });
103
104 checkOffByOne(function(done) {
105 client.testI32(-1, function(err, response) {
106 assert.error(err, 'checkOffByOne: no callback error');
107 assert.equal(-1, response);
108 assert.end();
109 done();
110 });
111 }, callback);
112
113 });
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800114};
115
116exports.ThriftTestDriverPromise = function(client, callback) {
117
Randy Abernethyd8187c52015-02-16 01:25:53 -0800118 test('Q Promise Client Tests', function(assert) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800119
Randy Abernethyd8187c52015-02-16 01:25:53 -0800120 var checkRecursively = makeRecursiveCheck(assert);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800121
Randy Abernethyd8187c52015-02-16 01:25:53 -0800122 function fail(msg) {
123 return function() {
124 assert.fail(msg);
125 }
126 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800127
Randy Abernethyd8187c52015-02-16 01:25:53 -0800128 function makeAsserter(assertionFn) {
129 return function(c) {
130 var fnName = c[0];
131 var expected = c[1];
132 client[fnName](expected)
133 .then(function(actual) {
134 assertionFn(actual, expected, fnName);
135 })
136 .fail(fail('fnName'));
137 };
138 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800139
Randy Abernethyd8187c52015-02-16 01:25:53 -0800140 testCases.simple.forEach(makeAsserter(assert.equal));
141 testCases.simpleLoose.forEach(makeAsserter(function(a, e, m){
142 assert.ok(a == e, m);
143 }));
144 testCases.deep.forEach(makeAsserter(assert.deepEqual));
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800145
Randy Abernethyd8187c52015-02-16 01:25:53 -0800146 client.testStruct(testCases.out)
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800147 .then(function(response) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800148 checkRecursively(testCases.out, response, 'testStruct');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800149 })
Randy Abernethyd8187c52015-02-16 01:25:53 -0800150 .fail(fail('testStruct'));
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800151
Randy Abernethyd8187c52015-02-16 01:25:53 -0800152 client.testNest(testCases.out2)
153 .then(function(response) {
154 checkRecursively(testCases.out2, response, 'testNest');
155 })
156 .fail(fail('testNest'));
157
158 client.testInsanity(testCases.crazy)
159 .then(function(response) {
160 checkRecursively(testCases.insanity, response, 'testInsanity');
161 })
162 .fail(fail('testInsanity'));
163
164 client.testException('TException')
165 .then(function(response) {
Randy Abernethybd60b922015-02-26 16:59:14 -0800166 fail('testException: TException');
Randy Abernethyd8187c52015-02-16 01:25:53 -0800167 })
Randy Abernethybd60b922015-02-26 16:59:14 -0800168 .fail(function(err) {
169 assert.ok(err instanceof TException);
170 });
Randy Abernethyd8187c52015-02-16 01:25:53 -0800171
172 client.testException('Xception')
173 .then(function(response) {
Randy Abernethybd60b922015-02-26 16:59:14 -0800174 fail('testException: Xception');
Randy Abernethyd8187c52015-02-16 01:25:53 -0800175 })
176 .fail(function(err) {
Randy Abernethybd60b922015-02-26 16:59:14 -0800177 assert.ok(err instanceof ttypes.Xception);
Randy Abernethyd8187c52015-02-16 01:25:53 -0800178 assert.equal(err.errorCode, 1001);
179 assert.equal('Xception', err.message);
180 });
181
182 client.testException('no Exception')
183 .then(function(response) {
184 assert.equal(undefined, response); //void
185 })
186 .fail(fail('testException'));
187
188 client.testOneway(0, fail('testOneway: should not answer'));
189
190 checkOffByOne(function(done) {
191 client.testI32(-1)
192 .then(function(response) {
193 assert.equal(-1, response);
194 assert.end();
195 done();
196 })
197 .fail(fail('checkOffByOne'));
198 }, callback);
199 });
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800200};
201
202
203// Helper Functions
204// =========================================================
205
Randy Abernethyd8187c52015-02-16 01:25:53 -0800206function makeRecursiveCheck(assert) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800207
Randy Abernethyd8187c52015-02-16 01:25:53 -0800208 return function (map1, map2, msg) {
209 var equal = true;
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800210
Randy Abernethyd8187c52015-02-16 01:25:53 -0800211 var equal = checkRecursively(map1, map2);
212
213 assert.ok(equal, msg);
214
215 // deepEqual doesn't work with fields using node-int64
216 function checkRecursively(map1, map2) {
217 if (typeof map1 !== 'function' && typeof map2 !== 'function') {
218 if (!map1 || typeof map1 !== 'object') {
219 //Handle int64 types (which use node-int64 in Node.js JavaScript)
220 if ((typeof map1 === "number") && (typeof map2 === "object") &&
221 (map2.buffer) && (map2.buffer instanceof Buffer) && (map2.buffer.length === 8)) {
222 var n = new Int64(map2.buffer);
223 return map1 === n.toNumber();
224 } else {
225 return map1 == map2;
226 }
227 } else {
228 return Object.keys(map1).every(function(key) {
229 return checkRecursively(map1[key], map2[key]);
230 });
231 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800232 }
233 }
234 }
235}
236
237function checkOffByOne(done, callback) {
238
239 var retry_limit = 30;
240 var retry_interval = 100;
241 var test_complete = false;
242 var retrys = 0;
243
244 /**
245 * redo a simple test after the oneway to make sure we aren't "off by one" --
246 * if the server treated oneway void like normal void, this next test will
247 * fail since it will get the void confirmation rather than the correct
248 * result. In this circumstance, the client will throw the exception:
249 *
250 * Because this is the last test against the server, when it completes
251 * the entire suite is complete by definition (the tests run serially).
252 */
253 done(function() {
254 test_complete = true;
255 });
256
257 //We wait up to retry_limit * retry_interval for the test suite to complete
258 function TestForCompletion() {
259 if(test_complete && callback) {
260 callback("Server successfully tested!");
261 } else {
262 if (++retrys < retry_limit) {
263 setTimeout(TestForCompletion, retry_interval);
264 } else if (callback) {
265 callback("Server test failed to complete after " +
266 (retry_limit * retry_interval / 1000) + " seconds");
267 }
268 }
269 }
270
271 setTimeout(TestForCompletion, retry_interval);
272}