2015-02-16 08:51:24 +00:00
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* 'License'); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
// This is the Node.js test driver for the standard Apache Thrift
|
|
|
|
// test service. The driver invokes every function defined in the
|
|
|
|
// Thrift Test service with a representative range of parameters.
|
|
|
|
//
|
|
|
|
// The ThriftTestDriver function requires a client object
|
|
|
|
// connected to a server hosting the Thrift Test service and
|
|
|
|
// supports an optional callback function which is called with
|
|
|
|
// a status message when the test is complete.
|
|
|
|
|
|
|
|
const test = require("tape");
|
|
|
|
|
|
|
|
const helpers = require("./helpers");
|
|
|
|
const ttypes = require(`./${helpers.genPath}/ThriftTest_types`);
|
|
|
|
const TException = require("thrift").Thrift.TException;
|
|
|
|
const Int64 = require("node-int64");
|
|
|
|
const testCases = require("./test-cases");
|
2015-02-16 08:51:24 +00:00
|
|
|
|
|
|
|
exports.ThriftTestDriver = function(client, callback) {
|
2018-10-28 15:27:38 +00:00
|
|
|
test(
|
|
|
|
"NodeJS Style Callback Client Tests",
|
|
|
|
{ skip: helpers.ecmaMode === "es6" },
|
|
|
|
function(assert) {
|
|
|
|
const checkRecursively = makeRecursiveCheck(assert);
|
|
|
|
|
|
|
|
function makeAsserter(assertionFn) {
|
|
|
|
return function(c) {
|
|
|
|
const fnName = c[0];
|
|
|
|
const expected = c[1];
|
|
|
|
client[fnName](expected, function(err, actual) {
|
|
|
|
assert.error(err, fnName + ": no callback error");
|
|
|
|
assertionFn(actual, expected, fnName);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2015-02-16 08:51:24 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
testCases.simple.forEach(
|
|
|
|
makeAsserter(function(a, e, m) {
|
|
|
|
if (a instanceof Int64) {
|
|
|
|
const e64 = e instanceof Int64 ? e : new Int64(e);
|
|
|
|
assert.deepEqual(a.buffer, e64.buffer, m);
|
|
|
|
} else {
|
|
|
|
assert.equal(a, e, m);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
testCases.deep.forEach(makeAsserter(assert.deepEqual));
|
|
|
|
testCases.deepUnordered.forEach(
|
|
|
|
makeAsserter(makeUnorderedDeepEqual(assert))
|
|
|
|
);
|
|
|
|
|
|
|
|
const arr = [];
|
|
|
|
for (let i = 0; i < 256; ++i) {
|
|
|
|
arr[i] = 255 - i;
|
|
|
|
}
|
|
|
|
let buf = new Buffer(arr);
|
|
|
|
client.testBinary(buf, function(err, response) {
|
|
|
|
assert.error(err, "testBinary: no callback error");
|
|
|
|
assert.equal(response.length, 256, "testBinary");
|
|
|
|
assert.deepEqual(response, buf, "testBinary(Buffer)");
|
|
|
|
});
|
|
|
|
buf = new Buffer(arr);
|
|
|
|
client.testBinary(buf.toString("binary"), function(err, response) {
|
|
|
|
assert.error(err, "testBinary: no callback error");
|
|
|
|
assert.equal(response.length, 256, "testBinary");
|
|
|
|
assert.deepEqual(response, buf, "testBinary(string)");
|
|
|
|
});
|
2015-02-16 08:51:24 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client.testMapMap(42, function(err, response) {
|
|
|
|
const expected = {
|
|
|
|
"4": { "1": 1, "2": 2, "3": 3, "4": 4 },
|
|
|
|
"-4": { "-4": -4, "-3": -3, "-2": -2, "-1": -1 }
|
|
|
|
};
|
|
|
|
assert.error(err, "testMapMap: no callback error");
|
|
|
|
assert.deepEqual(expected, response, "testMapMap");
|
|
|
|
});
|
2015-02-16 08:51:24 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client.testStruct(testCases.out, function(err, response) {
|
|
|
|
assert.error(err, "testStruct: no callback error");
|
|
|
|
checkRecursively(testCases.out, response, "testStruct");
|
|
|
|
});
|
2015-02-16 08:51:24 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client.testNest(testCases.out2, function(err, response) {
|
|
|
|
assert.error(err, "testNest: no callback error");
|
|
|
|
checkRecursively(testCases.out2, response, "testNest");
|
|
|
|
});
|
2015-02-16 08:51:24 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client.testInsanity(testCases.crazy, function(err, response) {
|
|
|
|
assert.error(err, "testInsanity: no callback error");
|
|
|
|
checkRecursively(testCases.insanity, response, "testInsanity");
|
|
|
|
});
|
2015-02-16 08:51:24 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client.testInsanity(testCases.crazy2, function(err, response) {
|
|
|
|
assert.error(err, "testInsanity2: no callback error");
|
|
|
|
checkRecursively(testCases.insanity, response, "testInsanity2");
|
2015-02-16 09:25:53 +00:00
|
|
|
});
|
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client.testException("TException", function(err, response) {
|
|
|
|
assert.ok(
|
|
|
|
err instanceof TException,
|
|
|
|
"testException: correct error type"
|
|
|
|
);
|
|
|
|
assert.ok(!response, "testException: no response");
|
|
|
|
});
|
2015-02-16 08:51:24 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client.testException("Xception", function(err, response) {
|
|
|
|
assert.ok(
|
|
|
|
err instanceof ttypes.Xception,
|
|
|
|
"testException: correct error type"
|
|
|
|
);
|
|
|
|
assert.ok(!response, "testException: no response");
|
|
|
|
assert.equal(err.errorCode, 1001, "testException: correct error code");
|
|
|
|
assert.equal(
|
|
|
|
"Xception",
|
|
|
|
err.message,
|
|
|
|
"testException: correct error message"
|
|
|
|
);
|
|
|
|
});
|
2015-02-16 08:51:24 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client.testException("no Exception", function(err, response) {
|
|
|
|
assert.error(err, "testException: no callback error");
|
|
|
|
assert.ok(!response, "testException: no response");
|
|
|
|
});
|
2015-02-16 09:25:53 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client.testOneway(0, function(err, response) {
|
|
|
|
assert.error(err, "testOneway: no callback error");
|
|
|
|
assert.strictEqual(response, undefined, "testOneway: void response");
|
|
|
|
});
|
2015-02-16 09:25:53 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
checkOffByOne(function(done) {
|
|
|
|
client.testI32(-1, function(err, response) {
|
|
|
|
assert.error(err, "checkOffByOne: no callback error");
|
|
|
|
assert.equal(-1, response);
|
|
|
|
assert.end();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}, callback);
|
2015-02-16 09:25:53 +00:00
|
|
|
}
|
2018-10-28 15:27:38 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// ES6 does not support callback style
|
|
|
|
if (helpers.ecmaMode === "es6") {
|
|
|
|
checkOffByOne(done => done(), callback);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.ThriftTestDriverPromise = function(client, callback) {
|
|
|
|
test("Promise Client Tests", function(assert) {
|
|
|
|
const checkRecursively = makeRecursiveCheck(assert);
|
2015-02-16 09:25:53 +00:00
|
|
|
|
|
|
|
function makeAsserter(assertionFn) {
|
|
|
|
return function(c) {
|
2018-10-28 15:27:38 +00:00
|
|
|
const fnName = c[0];
|
|
|
|
const expected = c[1];
|
2015-02-16 09:25:53 +00:00
|
|
|
client[fnName](expected)
|
|
|
|
.then(function(actual) {
|
|
|
|
assertionFn(actual, expected, fnName);
|
|
|
|
})
|
2018-10-28 15:27:38 +00:00
|
|
|
.catch(() => assert.fail("fnName"));
|
2015-02-16 09:25:53 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
testCases.simple.forEach(
|
|
|
|
makeAsserter(function(a, e, m) {
|
|
|
|
if (a instanceof Int64) {
|
|
|
|
const e64 = e instanceof Int64 ? e : new Int64(e);
|
|
|
|
assert.deepEqual(a.buffer, e64.buffer, m);
|
|
|
|
} else {
|
|
|
|
assert.equal(a, e, m);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2015-02-16 09:25:53 +00:00
|
|
|
testCases.deep.forEach(makeAsserter(assert.deepEqual));
|
2018-10-28 15:27:38 +00:00
|
|
|
testCases.deepUnordered.forEach(
|
|
|
|
makeAsserter(makeUnorderedDeepEqual(assert))
|
|
|
|
);
|
2015-02-16 09:25:53 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client
|
|
|
|
.testStruct(testCases.out)
|
2015-02-16 09:25:53 +00:00
|
|
|
.then(function(response) {
|
2018-10-28 15:27:38 +00:00
|
|
|
checkRecursively(testCases.out, response, "testStruct");
|
2015-02-16 09:25:53 +00:00
|
|
|
})
|
2018-10-28 15:27:38 +00:00
|
|
|
.catch(() => assert.fail("testStruct"));
|
2015-02-16 09:25:53 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client
|
|
|
|
.testNest(testCases.out2)
|
2015-02-16 09:25:53 +00:00
|
|
|
.then(function(response) {
|
2018-10-28 15:27:38 +00:00
|
|
|
checkRecursively(testCases.out2, response, "testNest");
|
2015-02-16 09:25:53 +00:00
|
|
|
})
|
2018-10-28 15:27:38 +00:00
|
|
|
.catch(() => assert.fail("testNest"));
|
2015-02-16 09:25:53 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client
|
|
|
|
.testInsanity(testCases.crazy)
|
2015-02-16 09:25:53 +00:00
|
|
|
.then(function(response) {
|
2018-10-28 15:27:38 +00:00
|
|
|
checkRecursively(testCases.insanity, response, "testInsanity");
|
2015-02-16 09:25:53 +00:00
|
|
|
})
|
2018-10-28 15:27:38 +00:00
|
|
|
.catch(() => assert.fail("testInsanity"));
|
2015-02-16 09:25:53 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client
|
|
|
|
.testInsanity(testCases.crazy2)
|
2015-06-25 12:31:41 +00:00
|
|
|
.then(function(response) {
|
2018-10-28 15:27:38 +00:00
|
|
|
checkRecursively(testCases.insanity, response, "testInsanity2");
|
2015-06-25 12:31:41 +00:00
|
|
|
})
|
2018-10-28 15:27:38 +00:00
|
|
|
.catch(() => assert.fail("testInsanity2"));
|
2015-06-25 12:31:41 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client
|
|
|
|
.testException("TException")
|
|
|
|
.then(function() {
|
|
|
|
assert.fail("testException: TException");
|
2015-02-16 09:25:53 +00:00
|
|
|
})
|
2018-10-28 15:27:38 +00:00
|
|
|
.catch(function(err) {
|
2015-02-27 00:59:14 +00:00
|
|
|
assert.ok(err instanceof TException);
|
|
|
|
});
|
2015-02-16 08:51:24 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client
|
|
|
|
.testException("Xception")
|
|
|
|
.then(function() {
|
|
|
|
assert.fail("testException: Xception");
|
2015-02-16 08:51:24 +00:00
|
|
|
})
|
2018-10-28 15:27:38 +00:00
|
|
|
.catch(function(err) {
|
2015-02-27 00:59:14 +00:00
|
|
|
assert.ok(err instanceof ttypes.Xception);
|
2015-02-16 09:25:53 +00:00
|
|
|
assert.equal(err.errorCode, 1001);
|
2018-10-28 15:27:38 +00:00
|
|
|
assert.equal("Xception", err.message);
|
2015-02-16 08:51:24 +00:00
|
|
|
});
|
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client
|
|
|
|
.testException("no Exception")
|
2015-02-16 09:25:53 +00:00
|
|
|
.then(function(response) {
|
|
|
|
assert.equal(undefined, response); //void
|
|
|
|
})
|
2018-10-28 15:27:38 +00:00
|
|
|
.catch(() => assert.fail("testException"));
|
2015-02-16 09:25:53 +00:00
|
|
|
|
2018-10-28 15:27:38 +00:00
|
|
|
client
|
|
|
|
.testOneway(0)
|
2018-07-17 16:19:49 +00:00
|
|
|
.then(function(response) {
|
2018-10-28 15:27:38 +00:00
|
|
|
assert.strictEqual(response, undefined, "testOneway: void response");
|
2018-07-17 16:19:49 +00:00
|
|
|
})
|
2018-10-28 15:27:38 +00:00
|
|
|
.catch(() => assert.fail("testOneway: should not reject"));
|
2015-02-16 09:25:53 +00:00
|
|
|
|
|
|
|
checkOffByOne(function(done) {
|
2018-10-28 15:27:38 +00:00
|
|
|
client
|
|
|
|
.testI32(-1)
|
2015-02-16 09:25:53 +00:00
|
|
|
.then(function(response) {
|
2018-10-28 15:27:38 +00:00
|
|
|
assert.equal(-1, response);
|
|
|
|
assert.end();
|
|
|
|
done();
|
2015-02-16 09:25:53 +00:00
|
|
|
})
|
2018-10-28 15:27:38 +00:00
|
|
|
.catch(() => assert.fail("checkOffByOne"));
|
2015-02-16 09:25:53 +00:00
|
|
|
}, callback);
|
|
|
|
});
|
2015-02-16 08:51:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Helper Functions
|
|
|
|
// =========================================================
|
|
|
|
|
2015-02-16 09:25:53 +00:00
|
|
|
function makeRecursiveCheck(assert) {
|
2018-10-28 15:27:38 +00:00
|
|
|
return function(map1, map2, msg) {
|
|
|
|
const equal = checkRecursively(map1, map2);
|
2015-02-16 09:25:53 +00:00
|
|
|
|
|
|
|
assert.ok(equal, msg);
|
|
|
|
|
|
|
|
// deepEqual doesn't work with fields using node-int64
|
|
|
|
function checkRecursively(map1, map2) {
|
2018-10-28 15:27:38 +00:00
|
|
|
if (typeof map1 !== "function" && typeof map2 !== "function") {
|
|
|
|
if (!map1 || typeof map1 !== "object") {
|
2015-02-16 09:25:53 +00:00
|
|
|
//Handle int64 types (which use node-int64 in Node.js JavaScript)
|
2018-10-28 15:27:38 +00:00
|
|
|
if (
|
|
|
|
typeof map1 === "number" &&
|
|
|
|
typeof map2 === "object" &&
|
|
|
|
map2.buffer &&
|
|
|
|
map2.buffer instanceof Buffer &&
|
|
|
|
map2.buffer.length === 8
|
|
|
|
) {
|
|
|
|
const n = new Int64(map2.buffer);
|
2015-02-16 09:25:53 +00:00
|
|
|
return map1 === n.toNumber();
|
|
|
|
} else {
|
|
|
|
return map1 == map2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Object.keys(map1).every(function(key) {
|
|
|
|
return checkRecursively(map1[key], map2[key]);
|
|
|
|
});
|
|
|
|
}
|
2015-02-16 08:51:24 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-28 15:27:38 +00:00
|
|
|
};
|
2015-02-16 08:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkOffByOne(done, callback) {
|
2018-10-28 15:27:38 +00:00
|
|
|
const retry_limit = 30;
|
|
|
|
const retry_interval = 100;
|
|
|
|
let test_complete = false;
|
|
|
|
let retrys = 0;
|
2015-02-16 08:51:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* redo a simple test after the oneway to make sure we aren't "off by one" --
|
|
|
|
* if the server treated oneway void like normal void, this next test will
|
|
|
|
* fail since it will get the void confirmation rather than the correct
|
|
|
|
* result. In this circumstance, the client will throw the exception:
|
|
|
|
*
|
|
|
|
* Because this is the last test against the server, when it completes
|
|
|
|
* the entire suite is complete by definition (the tests run serially).
|
|
|
|
*/
|
|
|
|
done(function() {
|
|
|
|
test_complete = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
//We wait up to retry_limit * retry_interval for the test suite to complete
|
|
|
|
function TestForCompletion() {
|
2018-10-28 15:27:38 +00:00
|
|
|
if (test_complete && callback) {
|
2015-02-16 08:51:24 +00:00
|
|
|
callback("Server successfully tested!");
|
|
|
|
} else {
|
|
|
|
if (++retrys < retry_limit) {
|
|
|
|
setTimeout(TestForCompletion, retry_interval);
|
|
|
|
} else if (callback) {
|
2018-10-28 15:27:38 +00:00
|
|
|
callback(
|
|
|
|
"Server test failed to complete after " +
|
|
|
|
(retry_limit * retry_interval) / 1000 +
|
|
|
|
" seconds"
|
|
|
|
);
|
2015-02-16 08:51:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(TestForCompletion, retry_interval);
|
|
|
|
}
|
2015-10-09 19:28:57 +00:00
|
|
|
|
|
|
|
function makeUnorderedDeepEqual(assert) {
|
|
|
|
return function(actual, expected, name) {
|
|
|
|
assert.equal(actual.length, expected.length, name);
|
2018-10-28 15:27:38 +00:00
|
|
|
for (const k in actual) {
|
|
|
|
let found = false;
|
|
|
|
for (const k2 in expected) {
|
2015-10-09 19:28:57 +00:00
|
|
|
if (actual[k] === expected[k2]) {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2018-10-28 15:27:38 +00:00
|
|
|
assert.fail("Unexpected value " + actual[k] + " with key " + k);
|
2015-10-09 19:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|