Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 1 | /* |
| 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 Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 29 | var test = require('tape'); |
| 30 | //var assert = require('assert'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 31 | var ttypes = require('./gen-nodejs/ThriftTest_types'); |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 32 | var TException = require('thrift').Thrift.TException; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 33 | var Int64 = require('node-int64'); |
| 34 | var testCases = require('./test-cases'); |
| 35 | |
| 36 | exports.ThriftTestDriver = function(client, callback) { |
| 37 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 38 | test('NodeJS Style Callback Client Tests', function(assert) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 39 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 40 | var checkRecursively = makeRecursiveCheck(assert); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 41 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 42 | 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 Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 52 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 53 | 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 Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 58 | testCases.deepUnordered.forEach(makeAsserter(makeUnorderedDeepEqual(assert))); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 59 | |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 60 | client.testMapMap(42, function(err, response) { |
| 61 | var expected = { |
| 62 | "4": {"1":1, "2":2, "3":3, "4":4}, |
| 63 | "-4": {"-4":-4, "-3":-3, "-2":-2, "-1":-1} |
| 64 | }; |
| 65 | assert.error(err, 'testMapMap: no callback error'); |
| 66 | assert.deepEqual(expected, response, 'testMapMap'); |
| 67 | }); |
| 68 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 69 | client.testStruct(testCases.out, function(err, response) { |
| 70 | assert.error(err, 'testStruct: no callback error'); |
| 71 | checkRecursively(testCases.out, response, 'testStruct'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 72 | }); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 73 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 74 | client.testNest(testCases.out2, function(err, response) { |
| 75 | assert.error(err, 'testNest: no callback error'); |
| 76 | checkRecursively(testCases.out2, response, 'testNest'); |
| 77 | }); |
| 78 | |
| 79 | client.testInsanity(testCases.crazy, function(err, response) { |
| 80 | assert.error(err, 'testInsanity: no callback error'); |
| 81 | checkRecursively(testCases.insanity, response, 'testInsanity'); |
| 82 | }); |
| 83 | |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 84 | client.testInsanity(testCases.crazy2, function(err, response) { |
| 85 | assert.error(err, 'testInsanity2: no callback error'); |
| 86 | checkRecursively(testCases.insanity, response, 'testInsanity2'); |
| 87 | }); |
| 88 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 89 | client.testException('TException', function(err, response) { |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 90 | assert.ok(err instanceof TException, 'testException: correct error type'); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 91 | assert.ok(!response, 'testException: no response'); |
| 92 | }); |
| 93 | |
| 94 | client.testException('Xception', function(err, response) { |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 95 | assert.ok(err instanceof ttypes.Xception, 'testException: correct error type'); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 96 | assert.ok(!response, 'testException: no response'); |
| 97 | assert.equal(err.errorCode, 1001, 'testException: correct error code'); |
| 98 | assert.equal('Xception', err.message, 'testException: correct error message'); |
| 99 | }); |
| 100 | |
| 101 | client.testException('no Exception', function(err, response) { |
| 102 | assert.error(err, 'testException: no callback error'); |
| 103 | assert.ok(!response, 'testException: no response'); |
| 104 | }); |
| 105 | |
| 106 | client.testOneway(0, function(err, response) { |
| 107 | assert.fail('testOneway should not answer'); |
| 108 | }); |
| 109 | |
| 110 | checkOffByOne(function(done) { |
| 111 | client.testI32(-1, function(err, response) { |
| 112 | assert.error(err, 'checkOffByOne: no callback error'); |
| 113 | assert.equal(-1, response); |
| 114 | assert.end(); |
| 115 | done(); |
| 116 | }); |
| 117 | }, callback); |
| 118 | |
| 119 | }); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | exports.ThriftTestDriverPromise = function(client, callback) { |
| 123 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 124 | test('Q Promise Client Tests', function(assert) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 125 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 126 | var checkRecursively = makeRecursiveCheck(assert); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 127 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 128 | function fail(msg) { |
| 129 | return function() { |
| 130 | assert.fail(msg); |
| 131 | } |
| 132 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 133 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 134 | function makeAsserter(assertionFn) { |
| 135 | return function(c) { |
| 136 | var fnName = c[0]; |
| 137 | var expected = c[1]; |
| 138 | client[fnName](expected) |
| 139 | .then(function(actual) { |
| 140 | assertionFn(actual, expected, fnName); |
| 141 | }) |
| 142 | .fail(fail('fnName')); |
| 143 | }; |
| 144 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 145 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 146 | testCases.simple.forEach(makeAsserter(assert.equal)); |
| 147 | testCases.simpleLoose.forEach(makeAsserter(function(a, e, m){ |
| 148 | assert.ok(a == e, m); |
| 149 | })); |
| 150 | testCases.deep.forEach(makeAsserter(assert.deepEqual)); |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 151 | testCases.deepUnordered.forEach(makeAsserter(makeUnorderedDeepEqual(assert))); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 152 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 153 | client.testStruct(testCases.out) |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 154 | .then(function(response) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 155 | checkRecursively(testCases.out, response, 'testStruct'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 156 | }) |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 157 | .fail(fail('testStruct')); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 158 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 159 | client.testNest(testCases.out2) |
| 160 | .then(function(response) { |
| 161 | checkRecursively(testCases.out2, response, 'testNest'); |
| 162 | }) |
| 163 | .fail(fail('testNest')); |
| 164 | |
| 165 | client.testInsanity(testCases.crazy) |
| 166 | .then(function(response) { |
| 167 | checkRecursively(testCases.insanity, response, 'testInsanity'); |
| 168 | }) |
| 169 | .fail(fail('testInsanity')); |
| 170 | |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 171 | client.testInsanity(testCases.crazy2) |
| 172 | .then(function(response) { |
| 173 | checkRecursively(testCases.insanity, response, 'testInsanity2'); |
| 174 | }) |
| 175 | .fail(fail('testInsanity2')); |
| 176 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 177 | client.testException('TException') |
| 178 | .then(function(response) { |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 179 | fail('testException: TException'); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 180 | }) |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 181 | .fail(function(err) { |
| 182 | assert.ok(err instanceof TException); |
| 183 | }); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 184 | |
| 185 | client.testException('Xception') |
| 186 | .then(function(response) { |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 187 | fail('testException: Xception'); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 188 | }) |
| 189 | .fail(function(err) { |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 190 | assert.ok(err instanceof ttypes.Xception); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 191 | assert.equal(err.errorCode, 1001); |
| 192 | assert.equal('Xception', err.message); |
| 193 | }); |
| 194 | |
| 195 | client.testException('no Exception') |
| 196 | .then(function(response) { |
| 197 | assert.equal(undefined, response); //void |
| 198 | }) |
| 199 | .fail(fail('testException')); |
| 200 | |
| 201 | client.testOneway(0, fail('testOneway: should not answer')); |
| 202 | |
| 203 | checkOffByOne(function(done) { |
| 204 | client.testI32(-1) |
| 205 | .then(function(response) { |
| 206 | assert.equal(-1, response); |
| 207 | assert.end(); |
| 208 | done(); |
| 209 | }) |
| 210 | .fail(fail('checkOffByOne')); |
| 211 | }, callback); |
| 212 | }); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | |
| 216 | // Helper Functions |
| 217 | // ========================================================= |
| 218 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 219 | function makeRecursiveCheck(assert) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 220 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 221 | return function (map1, map2, msg) { |
| 222 | var equal = true; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 223 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 224 | var equal = checkRecursively(map1, map2); |
| 225 | |
| 226 | assert.ok(equal, msg); |
| 227 | |
| 228 | // deepEqual doesn't work with fields using node-int64 |
| 229 | function checkRecursively(map1, map2) { |
| 230 | if (typeof map1 !== 'function' && typeof map2 !== 'function') { |
| 231 | if (!map1 || typeof map1 !== 'object') { |
| 232 | //Handle int64 types (which use node-int64 in Node.js JavaScript) |
| 233 | if ((typeof map1 === "number") && (typeof map2 === "object") && |
| 234 | (map2.buffer) && (map2.buffer instanceof Buffer) && (map2.buffer.length === 8)) { |
| 235 | var n = new Int64(map2.buffer); |
| 236 | return map1 === n.toNumber(); |
| 237 | } else { |
| 238 | return map1 == map2; |
| 239 | } |
| 240 | } else { |
| 241 | return Object.keys(map1).every(function(key) { |
| 242 | return checkRecursively(map1[key], map2[key]); |
| 243 | }); |
| 244 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | function checkOffByOne(done, callback) { |
| 251 | |
| 252 | var retry_limit = 30; |
| 253 | var retry_interval = 100; |
| 254 | var test_complete = false; |
| 255 | var retrys = 0; |
| 256 | |
| 257 | /** |
| 258 | * redo a simple test after the oneway to make sure we aren't "off by one" -- |
| 259 | * if the server treated oneway void like normal void, this next test will |
| 260 | * fail since it will get the void confirmation rather than the correct |
| 261 | * result. In this circumstance, the client will throw the exception: |
| 262 | * |
| 263 | * Because this is the last test against the server, when it completes |
| 264 | * the entire suite is complete by definition (the tests run serially). |
| 265 | */ |
| 266 | done(function() { |
| 267 | test_complete = true; |
| 268 | }); |
| 269 | |
| 270 | //We wait up to retry_limit * retry_interval for the test suite to complete |
| 271 | function TestForCompletion() { |
| 272 | if(test_complete && callback) { |
| 273 | callback("Server successfully tested!"); |
| 274 | } else { |
| 275 | if (++retrys < retry_limit) { |
| 276 | setTimeout(TestForCompletion, retry_interval); |
| 277 | } else if (callback) { |
| 278 | callback("Server test failed to complete after " + |
| 279 | (retry_limit * retry_interval / 1000) + " seconds"); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | setTimeout(TestForCompletion, retry_interval); |
| 285 | } |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 286 | |
| 287 | function makeUnorderedDeepEqual(assert) { |
| 288 | return function(actual, expected, name) { |
| 289 | assert.equal(actual.length, expected.length, name); |
| 290 | for (var k in actual) { |
| 291 | var found = false; |
| 292 | for (var k2 in expected) { |
| 293 | if (actual[k] === expected[k2]) { |
| 294 | found = true; |
| 295 | } |
| 296 | } |
| 297 | if (!found) { |
| 298 | assert.fail('Unexpected value ' + actual[k] + ' with key ' + k); |
| 299 | } |
| 300 | } |
| 301 | }; |
| 302 | } |