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