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