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 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [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. |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 28 | |
Cameron Martin | 21ed4a2 | 2024-04-22 11:08:19 +0100 | [diff] [blame] | 29 | import test from "tape"; |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 30 | |
Cameron Martin | 21ed4a2 | 2024-04-22 11:08:19 +0100 | [diff] [blame] | 31 | import helpers from "./helpers.js"; |
| 32 | import thrift from "thrift"; |
| 33 | import Int64 from "node-int64"; |
| 34 | import * as testCases from "./test-cases.mjs"; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 35 | |
Cameron Martin | 21ed4a2 | 2024-04-22 11:08:19 +0100 | [diff] [blame] | 36 | const ttypes = await import( |
| 37 | `./${helpers.genPath}/ThriftTest_types.${helpers.moduleExt}` |
| 38 | ); |
| 39 | |
| 40 | const TException = thrift.Thrift.TException; |
| 41 | |
| 42 | export const ThriftTestDriver = function (client, callback) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 43 | test( |
| 44 | "NodeJS Style Callback Client Tests", |
| 45 | { skip: helpers.ecmaMode === "es6" }, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 46 | function (assert) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 47 | const checkRecursively = makeRecursiveCheck(assert); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 48 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 49 | function makeAsserter(assertionFn) { |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 50 | return function (c) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 51 | const fnName = c[0]; |
| 52 | const expected = c[1]; |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 53 | client[fnName](expected, function (err, actual) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 54 | assert.error(err, fnName + ": no callback error"); |
| 55 | assertionFn(actual, expected, fnName); |
| 56 | }); |
| 57 | }; |
Nobuaki Sukegawa | f56b907 | 2015-11-23 19:38:18 +0900 | [diff] [blame] | 58 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 59 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 60 | testCases.simple.forEach( |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 61 | makeAsserter(function (a, e, m) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 62 | if (a instanceof Int64) { |
| 63 | const e64 = e instanceof Int64 ? e : new Int64(e); |
| 64 | assert.deepEqual(a.buffer, e64.buffer, m); |
| 65 | } else { |
| 66 | assert.equal(a, e, m); |
| 67 | } |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 68 | }), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 69 | ); |
| 70 | testCases.deep.forEach(makeAsserter(assert.deepEqual)); |
| 71 | testCases.deepUnordered.forEach( |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 72 | makeAsserter(makeUnorderedDeepEqual(assert)), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 73 | ); |
Nobuaki Sukegawa | 8a4d06f | 2015-11-06 21:24:26 +0900 | [diff] [blame] | 74 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 75 | const arr = []; |
| 76 | for (let i = 0; i < 256; ++i) { |
| 77 | arr[i] = 255 - i; |
| 78 | } |
| 79 | let buf = new Buffer(arr); |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 80 | client.testBinary(buf, function (err, response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 81 | assert.error(err, "testBinary: no callback error"); |
| 82 | assert.equal(response.length, 256, "testBinary"); |
| 83 | assert.deepEqual(response, buf, "testBinary(Buffer)"); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 84 | }); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 85 | buf = new Buffer(arr); |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 86 | client.testBinary(buf.toString("binary"), function (err, response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 87 | assert.error(err, "testBinary: no callback error"); |
| 88 | assert.equal(response.length, 256, "testBinary"); |
| 89 | assert.deepEqual(response, buf, "testBinary(string)"); |
| 90 | }); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 91 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 92 | client.testMapMap(42, function (err, response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 93 | const expected = { |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 94 | 4: { 1: 1, 2: 2, 3: 3, 4: 4 }, |
| 95 | "-4": { "-4": -4, "-3": -3, "-2": -2, "-1": -1 }, |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 96 | }; |
| 97 | assert.error(err, "testMapMap: no callback error"); |
| 98 | assert.deepEqual(expected, response, "testMapMap"); |
| 99 | }); |
| 100 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 101 | client.testStruct(testCases.out, function (err, response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 102 | assert.error(err, "testStruct: no callback error"); |
| 103 | checkRecursively(testCases.out, response, "testStruct"); |
| 104 | }); |
| 105 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 106 | client.testNest(testCases.out2, function (err, response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 107 | assert.error(err, "testNest: no callback error"); |
| 108 | checkRecursively(testCases.out2, response, "testNest"); |
| 109 | }); |
| 110 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 111 | client.testInsanity(testCases.crazy, function (err, response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 112 | assert.error(err, "testInsanity: no callback error"); |
| 113 | checkRecursively(testCases.insanity, response, "testInsanity"); |
| 114 | }); |
| 115 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 116 | client.testInsanity(testCases.crazy2, function (err, response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 117 | assert.error(err, "testInsanity2: no callback error"); |
| 118 | checkRecursively(testCases.insanity, response, "testInsanity2"); |
| 119 | }); |
| 120 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 121 | client.testException("TException", function (err, response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 122 | assert.ok( |
| 123 | err instanceof TException, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 124 | "testException: correct error type", |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 125 | ); |
| 126 | assert.ok(!response, "testException: no response"); |
| 127 | }); |
| 128 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 129 | client.testException("Xception", function (err, response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 130 | assert.ok( |
| 131 | err instanceof ttypes.Xception, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 132 | "testException: correct error type", |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 133 | ); |
| 134 | assert.ok(!response, "testException: no response"); |
| 135 | assert.equal(err.errorCode, 1001, "testException: correct error code"); |
| 136 | assert.equal( |
| 137 | "Xception", |
| 138 | err.message, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 139 | "testException: correct error message", |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 140 | ); |
| 141 | }); |
| 142 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 143 | client.testException("no Exception", function (err, response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 144 | assert.error(err, "testException: no callback error"); |
| 145 | assert.ok(!response, "testException: no response"); |
| 146 | }); |
| 147 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 148 | client.testOneway(0, function (err, response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 149 | assert.error(err, "testOneway: no callback error"); |
| 150 | assert.strictEqual(response, undefined, "testOneway: void response"); |
| 151 | }); |
| 152 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 153 | checkOffByOne(function (done) { |
| 154 | client.testI32(-1, function (err, response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 155 | assert.error(err, "checkOffByOne: no callback error"); |
| 156 | assert.equal(-1, response); |
| 157 | assert.end(); |
| 158 | done(); |
| 159 | }); |
| 160 | }, callback); |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 161 | }, |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 162 | ); |
| 163 | |
| 164 | // ES6 does not support callback style |
| 165 | if (helpers.ecmaMode === "es6") { |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 166 | checkOffByOne((done) => done(), callback); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 167 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 168 | }; |
| 169 | |
Cameron Martin | 21ed4a2 | 2024-04-22 11:08:19 +0100 | [diff] [blame] | 170 | export const ThriftTestDriverPromise = function (client, callback) { |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 171 | test("Promise Client Tests", function (assert) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 172 | const checkRecursively = makeRecursiveCheck(assert); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 173 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 174 | function makeAsserter(assertionFn) { |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 175 | return function (c) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 176 | const fnName = c[0]; |
| 177 | const expected = c[1]; |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 178 | client[fnName](expected) |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 179 | .then(function (actual) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 180 | assertionFn(actual, expected, fnName); |
| 181 | }) |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 182 | .catch(() => assert.fail("fnName")); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 183 | }; |
| 184 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 185 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 186 | testCases.simple.forEach( |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 187 | makeAsserter(function (a, e, m) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 188 | if (a instanceof Int64) { |
| 189 | const e64 = e instanceof Int64 ? e : new Int64(e); |
| 190 | assert.deepEqual(a.buffer, e64.buffer, m); |
| 191 | } else { |
| 192 | assert.equal(a, e, m); |
| 193 | } |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 194 | }), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 195 | ); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 196 | testCases.deep.forEach(makeAsserter(assert.deepEqual)); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 197 | testCases.deepUnordered.forEach( |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 198 | makeAsserter(makeUnorderedDeepEqual(assert)), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 199 | ); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 200 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 201 | client |
| 202 | .testStruct(testCases.out) |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 203 | .then(function (response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 204 | checkRecursively(testCases.out, response, "testStruct"); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 205 | }) |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 206 | .catch(() => assert.fail("testStruct")); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 207 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 208 | client |
| 209 | .testNest(testCases.out2) |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 210 | .then(function (response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 211 | checkRecursively(testCases.out2, response, "testNest"); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 212 | }) |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 213 | .catch(() => assert.fail("testNest")); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 214 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 215 | client |
| 216 | .testInsanity(testCases.crazy) |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 217 | .then(function (response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 218 | checkRecursively(testCases.insanity, response, "testInsanity"); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 219 | }) |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 220 | .catch(() => assert.fail("testInsanity")); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 221 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 222 | client |
| 223 | .testInsanity(testCases.crazy2) |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 224 | .then(function (response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 225 | checkRecursively(testCases.insanity, response, "testInsanity2"); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 226 | }) |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 227 | .catch(() => assert.fail("testInsanity2")); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 228 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 229 | client |
| 230 | .testException("TException") |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 231 | .then(function () { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 232 | assert.fail("testException: TException"); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 233 | }) |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 234 | .catch(function (err) { |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 235 | assert.ok(err instanceof TException); |
| 236 | }); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 237 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 238 | client |
| 239 | .testException("Xception") |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 240 | .then(function () { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 241 | assert.fail("testException: Xception"); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 242 | }) |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 243 | .catch(function (err) { |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 244 | assert.ok(err instanceof ttypes.Xception); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 245 | assert.equal(err.errorCode, 1001); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 246 | assert.equal("Xception", err.message); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 247 | }); |
| 248 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 249 | client |
| 250 | .testException("no Exception") |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 251 | .then(function (response) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 252 | assert.equal(undefined, response); //void |
| 253 | }) |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 254 | .catch(() => assert.fail("testException")); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 255 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 256 | client |
| 257 | .testOneway(0) |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 258 | .then(function (response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 259 | assert.strictEqual(response, undefined, "testOneway: void response"); |
bforbis | f2867c2 | 2018-07-17 12:19:49 -0400 | [diff] [blame] | 260 | }) |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 261 | .catch(() => assert.fail("testOneway: should not reject")); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 262 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 263 | checkOffByOne(function (done) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 264 | client |
| 265 | .testI32(-1) |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 266 | .then(function (response) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 267 | assert.equal(-1, response); |
| 268 | assert.end(); |
| 269 | done(); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 270 | }) |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 271 | .catch(() => assert.fail("checkOffByOne")); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 272 | }, callback); |
| 273 | }); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 274 | }; |
| 275 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 276 | // Helper Functions |
| 277 | // ========================================================= |
| 278 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 279 | function makeRecursiveCheck(assert) { |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 280 | return function (map1, map2, msg) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 281 | const equal = checkRecursively(map1, map2); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 282 | |
| 283 | assert.ok(equal, msg); |
| 284 | |
| 285 | // deepEqual doesn't work with fields using node-int64 |
| 286 | function checkRecursively(map1, map2) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 287 | if (typeof map1 !== "function" && typeof map2 !== "function") { |
| 288 | if (!map1 || typeof map1 !== "object") { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 289 | //Handle int64 types (which use node-int64 in Node.js JavaScript) |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 290 | if ( |
| 291 | typeof map1 === "number" && |
| 292 | typeof map2 === "object" && |
| 293 | map2.buffer && |
| 294 | map2.buffer instanceof Buffer && |
| 295 | map2.buffer.length === 8 |
| 296 | ) { |
| 297 | const n = new Int64(map2.buffer); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 298 | return map1 === n.toNumber(); |
| 299 | } else { |
| 300 | return map1 == map2; |
| 301 | } |
| 302 | } else { |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 303 | return Object.keys(map1).every(function (key) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 304 | return checkRecursively(map1[key], map2[key]); |
| 305 | }); |
| 306 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 309 | }; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | function checkOffByOne(done, callback) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 313 | const retry_limit = 30; |
| 314 | const retry_interval = 100; |
| 315 | let test_complete = false; |
| 316 | let retrys = 0; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 317 | |
| 318 | /** |
| 319 | * redo a simple test after the oneway to make sure we aren't "off by one" -- |
| 320 | * if the server treated oneway void like normal void, this next test will |
| 321 | * fail since it will get the void confirmation rather than the correct |
| 322 | * result. In this circumstance, the client will throw the exception: |
| 323 | * |
| 324 | * Because this is the last test against the server, when it completes |
| 325 | * the entire suite is complete by definition (the tests run serially). |
| 326 | */ |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 327 | done(function () { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 328 | test_complete = true; |
| 329 | }); |
| 330 | |
| 331 | //We wait up to retry_limit * retry_interval for the test suite to complete |
| 332 | function TestForCompletion() { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 333 | if (test_complete && callback) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 334 | callback("Server successfully tested!"); |
| 335 | } else { |
| 336 | if (++retrys < retry_limit) { |
| 337 | setTimeout(TestForCompletion, retry_interval); |
| 338 | } else if (callback) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 339 | callback( |
| 340 | "Server test failed to complete after " + |
| 341 | (retry_limit * retry_interval) / 1000 + |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 342 | " seconds", |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 343 | ); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | setTimeout(TestForCompletion, retry_interval); |
| 349 | } |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 350 | |
| 351 | function makeUnorderedDeepEqual(assert) { |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 352 | return function (actual, expected, name) { |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 353 | assert.equal(actual.length, expected.length, name); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 354 | for (const k in actual) { |
| 355 | let found = false; |
| 356 | for (const k2 in expected) { |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 357 | if (actual[k] === expected[k2]) { |
| 358 | found = true; |
| 359 | } |
| 360 | } |
| 361 | if (!found) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 362 | assert.fail("Unexpected value " + actual[k] + " with key " + k); |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | }; |
| 366 | } |