blob: 5c89f7acd92da70004957f3c8ae64321bde675ed [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.
22
23var ttypes = require('./gen-nodejs/ThriftTest_types');
Randy Abernethyd60f9782014-03-28 10:36:38 -070024var TException = require('thrift').Thrift.TException;
raa2e4e562014-03-29 01:14:48 -070025
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080026function makeSyncHandler(label) {
27 return function(thing) {
Randy Abernethyd8187c52015-02-16 01:25:53 -080028 //console.log(label + '(\'' + thing + '\')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080029 return thing;
raa2e4e562014-03-29 01:14:48 -070030 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080031}
32
33var syncHandlers = {
34 testVoid: testVoid,
35 testMapMap: testMapMap,
36 testInsanity: testInsanity,
37 testMulti: testMulti,
38 testException: testException,
39 testMultiException: testMultiException,
40 testOneway: testOneway
41};
42
43function makeAsyncHandler(label) {
44 return function(thing, result) {
45 thing = syncHandlers[label](thing);
46 result(null, thing);
47 }
48}
49
50var asyncHandlers = {
51 testVoid: testVoidAsync,
52 testMulti: testMultiAsync,
53 testException: testExceptionAsync,
54 testMultiException: testMultiExceptionAsync,
55 testOneway: testOnewayAsync
56};
57
58var identityHandlers = [
59 'testString',
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090060 'testBool',
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080061 'testByte',
62 'testI32',
63 'testI64',
64 'testDouble',
Randy Abernethy983bf7d2015-10-09 12:28:57 -070065 'testBinary',
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080066 'testStruct',
67 'testNest',
68 'testMap',
69 'testStringMap',
70 'testSet',
71 'testList',
72 'testEnum',
73 'testTypedef'
74];
75
76function testVoid() {
Randy Abernethyd8187c52015-02-16 01:25:53 -080077 //console.log('testVoid()');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080078}
79
80function testVoidAsync(result) {
81 result(testVoid());
82}
83
84function testMapMap(hello) {
Randy Abernethyd8187c52015-02-16 01:25:53 -080085 //console.log('testMapMap(' + hello + ')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080086
87 var mapmap = [];
88 var pos = [];
89 var neg = [];
90 for (var i = 1; i < 5; i++) {
91 pos[i] = i;
92 neg[-i] = -i;
93 }
94 mapmap[4] = pos;
95 mapmap[-4] = neg;
96
97 return mapmap;
98}
99
100function testInsanity(argument) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800101 //console.log('testInsanity(');
102 //console.log(argument);
103 //console.log(')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800104
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800105 var first_map = [];
106 var second_map = [];
107
Jens Geyerd629ea02015-09-23 21:16:50 +0200108 first_map[ttypes.Numberz.TWO] = argument;
109 first_map[ttypes.Numberz.THREE] = argument;
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800110
111 var looney = new ttypes.Insanity();
112 second_map[ttypes.Numberz.SIX] = looney;
113
114 var insane = [];
115 insane[1] = first_map;
116 insane[2] = second_map;
117
Randy Abernethyd8187c52015-02-16 01:25:53 -0800118 //console.log('insane result:');
119 //console.log(insane);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800120 return insane;
121}
122
123function testMulti(arg0, arg1, arg2, arg3, arg4, arg5) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800124 //console.log('testMulti()');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800125
126 var hello = new ttypes.Xtruct();
127 hello.string_thing = 'Hello2';
128 hello.byte_thing = arg0;
129 hello.i32_thing = arg1;
130 hello.i64_thing = arg2;
131 return hello;
132}
133
134function testMultiAsync(arg0, arg1, arg2, arg3, arg4, arg5, result) {
135 var hello = testMulti(arg0, arg1, arg2, arg3, arg4, arg5);
136 result(null, hello);
137}
138
139function testException(arg) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800140 //console.log('testException('+arg+')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800141 if (arg === 'Xception') {
142 var x = new ttypes.Xception();
143 x.errorCode = 1001;
144 x.message = arg;
145 throw x;
146 } else if (arg === 'TException') {
147 throw new TException(arg);
148 } else {
149 return;
150 }
151}
152
153function testExceptionAsync(arg, result) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800154 //console.log('testException('+arg+')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800155 if (arg === 'Xception') {
156 var x = new ttypes.Xception();
157 x.errorCode = 1001;
158 x.message = arg;
159 result(x);
160 } else if (arg === 'TException') {
161 result(new TException(arg));
162 } else {
163 result(null);
164 }
165}
166
167function testMultiException(arg0, arg1) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800168 //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800169 if (arg0 === ('Xception')) {
170 var x = new ttypes.Xception();
171 x.errorCode = 1001;
172 x.message = 'This is an Xception';
173 throw x;
174 } else if (arg0 === ('Xception2')) {
175 var x2 = new ttypes.Xception2();
176 x2.errorCode = 2002;
177 x2.struct_thing = new ttypes.Xtruct();
178 x2.struct_thing.string_thing = 'This is an Xception2';
179 throw x2;
180 }
181
182 var res = new ttypes.Xtruct();
183 res.string_thing = arg1;
184 return res;
185}
186
187function testMultiExceptionAsync(arg0, arg1, result) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800188 //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800189 if (arg0 === ('Xception')) {
190 var x = new ttypes.Xception();
191 x.errorCode = 1001;
192 x.message = 'This is an Xception';
193 result(x);
194 } else if (arg0 === ('Xception2')) {
195 var x2 = new ttypes.Xception2();
196 x2.errorCode = 2002;
197 x2.struct_thing = new ttypes.Xtruct();
198 x2.struct_thing.string_thing = 'This is an Xception2';
199 result(x2);
Randy Abernethybd60b922015-02-26 16:59:14 -0800200 } else {
201 var res = new ttypes.Xtruct();
202 res.string_thing = arg1;
203 result(null, res);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800204 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800205}
206
207function testOneway(sleepFor) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800208 //console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800209}
210
211function testOnewayAsync(sleepFor, result) {
212 testOneway(sleepFor);
213}
214
215identityHandlers.forEach(function(label) {
216 syncHandlers[label] = makeSyncHandler(label);
217 asyncHandlers[label] = makeAsyncHandler(label);
218});
219
220['testMapMap', 'testInsanity'].forEach(function(label) {
221 asyncHandlers[label] = makeAsyncHandler(label);
222});
223
224exports.ThriftTestHandler = asyncHandlers;
225
226exports.AsyncThriftTestHandler = asyncHandlers;
227exports.SyncThriftTestHandler = asyncHandlers;