blob: 21132530865ac655219b2048c9e8833eeafe7031 [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',
65 'testStruct',
66 'testNest',
67 'testMap',
68 'testStringMap',
69 'testSet',
70 'testList',
71 'testEnum',
72 'testTypedef'
73];
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
83function testMapMap(hello) {
Randy Abernethyd8187c52015-02-16 01:25:53 -080084 //console.log('testMapMap(' + hello + ')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080085
86 var mapmap = [];
87 var pos = [];
88 var neg = [];
89 for (var i = 1; i < 5; i++) {
90 pos[i] = i;
91 neg[-i] = -i;
92 }
93 mapmap[4] = pos;
94 mapmap[-4] = neg;
95
96 return mapmap;
97}
98
99function testInsanity(argument) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800100 //console.log('testInsanity(');
101 //console.log(argument);
102 //console.log(')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800103
104 var hello = new ttypes.Xtruct();
105 hello.string_thing = 'Hello2';
106 hello.byte_thing = 2;
107 hello.i32_thing = 2;
108 hello.i64_thing = 2;
109
110 var goodbye = new ttypes.Xtruct();
111 goodbye.string_thing = 'Goodbye4';
112 goodbye.byte_thing = 4;
113 goodbye.i32_thing = 4;
114 goodbye.i64_thing = 4;
115
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800116 var first_map = [];
117 var second_map = [];
118
Jens Geyerd629ea02015-09-23 21:16:50 +0200119 first_map[ttypes.Numberz.TWO] = argument;
120 first_map[ttypes.Numberz.THREE] = argument;
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800121
122 var looney = new ttypes.Insanity();
123 second_map[ttypes.Numberz.SIX] = looney;
124
125 var insane = [];
126 insane[1] = first_map;
127 insane[2] = second_map;
128
Randy Abernethyd8187c52015-02-16 01:25:53 -0800129 //console.log('insane result:');
130 //console.log(insane);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800131 return insane;
132}
133
134function testMulti(arg0, arg1, arg2, arg3, arg4, arg5) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800135 //console.log('testMulti()');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800136
137 var hello = new ttypes.Xtruct();
138 hello.string_thing = 'Hello2';
139 hello.byte_thing = arg0;
140 hello.i32_thing = arg1;
141 hello.i64_thing = arg2;
142 return hello;
143}
144
145function testMultiAsync(arg0, arg1, arg2, arg3, arg4, arg5, result) {
146 var hello = testMulti(arg0, arg1, arg2, arg3, arg4, arg5);
147 result(null, hello);
148}
149
150function testException(arg) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800151 //console.log('testException('+arg+')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800152 if (arg === 'Xception') {
153 var x = new ttypes.Xception();
154 x.errorCode = 1001;
155 x.message = arg;
156 throw x;
157 } else if (arg === 'TException') {
158 throw new TException(arg);
159 } else {
160 return;
161 }
162}
163
164function testExceptionAsync(arg, result) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800165 //console.log('testException('+arg+')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800166 if (arg === 'Xception') {
167 var x = new ttypes.Xception();
168 x.errorCode = 1001;
169 x.message = arg;
170 result(x);
171 } else if (arg === 'TException') {
172 result(new TException(arg));
173 } else {
174 result(null);
175 }
176}
177
178function testMultiException(arg0, arg1) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800179 //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800180 if (arg0 === ('Xception')) {
181 var x = new ttypes.Xception();
182 x.errorCode = 1001;
183 x.message = 'This is an Xception';
184 throw x;
185 } else if (arg0 === ('Xception2')) {
186 var x2 = new ttypes.Xception2();
187 x2.errorCode = 2002;
188 x2.struct_thing = new ttypes.Xtruct();
189 x2.struct_thing.string_thing = 'This is an Xception2';
190 throw x2;
191 }
192
193 var res = new ttypes.Xtruct();
194 res.string_thing = arg1;
195 return res;
196}
197
198function testMultiExceptionAsync(arg0, arg1, result) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800199 //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800200 if (arg0 === ('Xception')) {
201 var x = new ttypes.Xception();
202 x.errorCode = 1001;
203 x.message = 'This is an Xception';
204 result(x);
205 } else if (arg0 === ('Xception2')) {
206 var x2 = new ttypes.Xception2();
207 x2.errorCode = 2002;
208 x2.struct_thing = new ttypes.Xtruct();
209 x2.struct_thing.string_thing = 'This is an Xception2';
210 result(x2);
Randy Abernethybd60b922015-02-26 16:59:14 -0800211 } else {
212 var res = new ttypes.Xtruct();
213 res.string_thing = arg1;
214 result(null, res);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800215 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800216}
217
218function testOneway(sleepFor) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800219 //console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800220}
221
222function testOnewayAsync(sleepFor, result) {
223 testOneway(sleepFor);
224}
225
226identityHandlers.forEach(function(label) {
227 syncHandlers[label] = makeSyncHandler(label);
228 asyncHandlers[label] = makeAsyncHandler(label);
229});
230
231['testMapMap', 'testInsanity'].forEach(function(label) {
232 asyncHandlers[label] = makeAsyncHandler(label);
233});
234
235exports.ThriftTestHandler = asyncHandlers;
236
237exports.AsyncThriftTestHandler = asyncHandlers;
238exports.SyncThriftTestHandler = asyncHandlers;