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 | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 58 | |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 59 | 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 Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 68 | client.testStruct(testCases.out, function(err, response) { |
| 69 | assert.error(err, 'testStruct: no callback error'); |
| 70 | checkRecursively(testCases.out, response, 'testStruct'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 71 | }); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 72 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 73 | 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 Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 84 | assert.ok(err instanceof TException, 'testException: correct error type'); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 85 | assert.ok(!response, 'testException: no response'); |
| 86 | }); |
| 87 | |
| 88 | client.testException('Xception', function(err, response) { |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 89 | assert.ok(err instanceof ttypes.Xception, 'testException: correct error type'); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 90 | 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 Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | exports.ThriftTestDriverPromise = function(client, callback) { |
| 117 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 118 | test('Q Promise Client Tests', function(assert) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 119 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 120 | var checkRecursively = makeRecursiveCheck(assert); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 121 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 122 | function fail(msg) { |
| 123 | return function() { |
| 124 | assert.fail(msg); |
| 125 | } |
| 126 | } |
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 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 Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 139 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 140 | 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 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 | client.testStruct(testCases.out) |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 147 | .then(function(response) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 148 | checkRecursively(testCases.out, response, 'testStruct'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 149 | }) |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 150 | .fail(fail('testStruct')); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 151 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 152 | 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 Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 166 | fail('testException: TException'); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 167 | }) |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 168 | .fail(function(err) { |
| 169 | assert.ok(err instanceof TException); |
| 170 | }); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 171 | |
| 172 | client.testException('Xception') |
| 173 | .then(function(response) { |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 174 | fail('testException: Xception'); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 175 | }) |
| 176 | .fail(function(err) { |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 177 | assert.ok(err instanceof ttypes.Xception); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 178 | 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 Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | |
| 203 | // Helper Functions |
| 204 | // ========================================================= |
| 205 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 206 | function makeRecursiveCheck(assert) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 207 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 208 | return function (map1, map2, msg) { |
| 209 | var equal = true; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 210 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 211 | 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 Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | function 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 | } |