blob: 317a7c81016a3f76539713443ddd102c46cb4ff9 [file] [log] [blame]
raa2e4e562014-03-29 01:14:48 -07001/*
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
Randy Abernethy96f4f072015-02-10 02:29:15 -080020//This is the server side Node test handler for the standard
raa2e4e562014-03-29 01:14:48 -070021// Apache Thrift test service.
bforbisda1169d2018-10-28 11:27:38 -040022const helpers = require("./helpers");
23const ttypes = require(`./${helpers.genPath}/ThriftTest_types`);
24const TException = require("thrift").Thrift.TException;
raa2e4e562014-03-29 01:14:48 -070025
bforbisda1169d2018-10-28 11:27:38 -040026function makeSyncHandler() {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080027 return function(thing) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080028 return thing;
bforbisda1169d2018-10-28 11:27:38 -040029 };
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080030}
31
bforbisda1169d2018-10-28 11:27:38 -040032const syncHandlers = {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080033 testVoid: testVoid,
34 testMapMap: testMapMap,
35 testInsanity: testInsanity,
36 testMulti: testMulti,
37 testException: testException,
38 testMultiException: testMultiException,
39 testOneway: testOneway
40};
41
42function makeAsyncHandler(label) {
43 return function(thing, result) {
44 thing = syncHandlers[label](thing);
45 result(null, thing);
bforbisda1169d2018-10-28 11:27:38 -040046 };
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080047}
48
bforbisda1169d2018-10-28 11:27:38 -040049const asyncHandlers = {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080050 testVoid: testVoidAsync,
51 testMulti: testMultiAsync,
52 testException: testExceptionAsync,
53 testMultiException: testMultiExceptionAsync,
54 testOneway: testOnewayAsync
55};
56
bforbisda1169d2018-10-28 11:27:38 -040057const identityHandlers = [
58 "testString",
59 "testBool",
60 "testByte",
61 "testI32",
62 "testI64",
63 "testDouble",
64 "testBinary",
65 "testStruct",
66 "testNest",
67 "testMap",
68 "testStringMap",
69 "testSet",
70 "testList",
71 "testEnum",
72 "testTypedef"
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080073];
74
75function testVoid() {
Randy Abernethyd8187c52015-02-16 01:25:53 -080076 //console.log('testVoid()');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080077}
78
79function testVoidAsync(result) {
80 result(testVoid());
81}
82
bforbisda1169d2018-10-28 11:27:38 -040083function testMapMap() {
84 const mapmap = [];
85 const pos = [];
86 const neg = [];
87 for (let i = 1; i < 5; i++) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080088 pos[i] = i;
89 neg[-i] = -i;
90 }
91 mapmap[4] = pos;
92 mapmap[-4] = neg;
93
94 return mapmap;
95}
96
97function testInsanity(argument) {
Randy Abernethyd8187c52015-02-16 01:25:53 -080098 //console.log('testInsanity(');
99 //console.log(argument);
100 //console.log(')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800101
bforbisda1169d2018-10-28 11:27:38 -0400102 const first_map = [];
103 const second_map = [];
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800104
Jens Geyerd629ea02015-09-23 21:16:50 +0200105 first_map[ttypes.Numberz.TWO] = argument;
106 first_map[ttypes.Numberz.THREE] = argument;
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800107
bforbisda1169d2018-10-28 11:27:38 -0400108 const looney = new ttypes.Insanity();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800109 second_map[ttypes.Numberz.SIX] = looney;
110
bforbisda1169d2018-10-28 11:27:38 -0400111 const insane = [];
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800112 insane[1] = first_map;
113 insane[2] = second_map;
114
Randy Abernethyd8187c52015-02-16 01:25:53 -0800115 //console.log('insane result:');
116 //console.log(insane);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800117 return insane;
118}
119
bforbisda1169d2018-10-28 11:27:38 -0400120function testMulti(arg0, arg1, arg2) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800121 //console.log('testMulti()');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800122
bforbisda1169d2018-10-28 11:27:38 -0400123 const hello = new ttypes.Xtruct();
124 hello.string_thing = "Hello2";
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800125 hello.byte_thing = arg0;
126 hello.i32_thing = arg1;
127 hello.i64_thing = arg2;
128 return hello;
129}
130
131function testMultiAsync(arg0, arg1, arg2, arg3, arg4, arg5, result) {
bforbisda1169d2018-10-28 11:27:38 -0400132 const hello = testMulti(arg0, arg1, arg2, arg3, arg4, arg5);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800133 result(null, hello);
134}
135
136function testException(arg) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800137 //console.log('testException('+arg+')');
bforbisda1169d2018-10-28 11:27:38 -0400138 if (arg === "Xception") {
139 const x = new ttypes.Xception();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800140 x.errorCode = 1001;
141 x.message = arg;
142 throw x;
bforbisda1169d2018-10-28 11:27:38 -0400143 } else if (arg === "TException") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800144 throw new TException(arg);
145 } else {
146 return;
147 }
148}
149
150function testExceptionAsync(arg, result) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800151 //console.log('testException('+arg+')');
bforbisda1169d2018-10-28 11:27:38 -0400152 if (arg === "Xception") {
153 const x = new ttypes.Xception();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800154 x.errorCode = 1001;
155 x.message = arg;
156 result(x);
bforbisda1169d2018-10-28 11:27:38 -0400157 } else if (arg === "TException") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800158 result(new TException(arg));
159 } else {
160 result(null);
161 }
162}
163
164function testMultiException(arg0, arg1) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800165 //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
bforbisda1169d2018-10-28 11:27:38 -0400166 if (arg0 === "Xception") {
167 const x = new ttypes.Xception();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800168 x.errorCode = 1001;
bforbisda1169d2018-10-28 11:27:38 -0400169 x.message = "This is an Xception";
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800170 throw x;
bforbisda1169d2018-10-28 11:27:38 -0400171 } else if (arg0 === "Xception2") {
172 const x2 = new ttypes.Xception2();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800173 x2.errorCode = 2002;
174 x2.struct_thing = new ttypes.Xtruct();
bforbisda1169d2018-10-28 11:27:38 -0400175 x2.struct_thing.string_thing = "This is an Xception2";
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800176 throw x2;
177 }
178
bforbisda1169d2018-10-28 11:27:38 -0400179 const res = new ttypes.Xtruct();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800180 res.string_thing = arg1;
181 return res;
182}
183
184function testMultiExceptionAsync(arg0, arg1, result) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800185 //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
bforbisda1169d2018-10-28 11:27:38 -0400186 if (arg0 === "Xception") {
187 const x = new ttypes.Xception();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800188 x.errorCode = 1001;
bforbisda1169d2018-10-28 11:27:38 -0400189 x.message = "This is an Xception";
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800190 result(x);
bforbisda1169d2018-10-28 11:27:38 -0400191 } else if (arg0 === "Xception2") {
192 const x2 = new ttypes.Xception2();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800193 x2.errorCode = 2002;
194 x2.struct_thing = new ttypes.Xtruct();
bforbisda1169d2018-10-28 11:27:38 -0400195 x2.struct_thing.string_thing = "This is an Xception2";
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800196 result(x2);
Randy Abernethybd60b922015-02-26 16:59:14 -0800197 } else {
bforbisda1169d2018-10-28 11:27:38 -0400198 const res = new ttypes.Xtruct();
Randy Abernethybd60b922015-02-26 16:59:14 -0800199 res.string_thing = arg1;
200 result(null, res);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800201 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800202}
203
bforbisda1169d2018-10-28 11:27:38 -0400204//console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!');
205function testOneway() {}
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800206
bforbisda1169d2018-10-28 11:27:38 -0400207function testOnewayAsync(sleepFor) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800208 testOneway(sleepFor);
209}
210
211identityHandlers.forEach(function(label) {
212 syncHandlers[label] = makeSyncHandler(label);
213 asyncHandlers[label] = makeAsyncHandler(label);
214});
215
bforbisda1169d2018-10-28 11:27:38 -0400216["testMapMap", "testInsanity"].forEach(function(label) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800217 asyncHandlers[label] = makeAsyncHandler(label);
218});
219
220exports.ThriftTestHandler = asyncHandlers;