blob: af5f7bd9c175897e3f3390f7533a39e6572c8238 [file] [log] [blame]
Konrad Grochowskif9c4be82015-01-09 11:32:26 +01001/*
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
Kazuki Matsudab909a382016-02-13 19:36:09 +090020//This is the server side Node test handler for the standard
Konrad Grochowskif9c4be82015-01-09 11:32:26 +010021// Apache Thrift test service.
22
Brian Forbisb5d6ea32018-08-25 23:39:29 -040023const es6Mode = process.argv.includes('--es6');
24const genFolder = es6Mode ? 'gen-nodejs-es6' : 'gen-nodejs';
25const ttypes = require(`./${genFolder}/ThriftTest_types`);
26const TException = require('../../nodejs/lib/thrift').TException;
Konrad Grochowskif9c4be82015-01-09 11:32:26 +010027
Brian Forbisb5d6ea32018-08-25 23:39:29 -040028exports.ThriftTestHandler = {
Konrad Grochowskif9c4be82015-01-09 11:32:26 +010029 testVoid: function(result) {
30 console.log('testVoid()');
31 result(null);
32 },
33 testString: function(thing, result) {
34 console.log('testString(\'' + thing + '\')');
35 result(null, thing);
36 },
37 testByte: function(thing, result) {
38 console.log('testByte(' + thing + ')');
39 result(null, thing);
40 },
41 testI32: function(thing, result) {
42 console.log('testI32(' + thing + ')');
43 result(null, thing);
44 },
45 testI64: function(thing, result) {
46 console.log('testI64(' + thing + ')');
47 result(null, thing);
48 },
49 testDouble: function(thing, result) {
50 console.log('testDouble(' + thing + ')');
51 result(null, thing);
52 },
53 testBinary: function(thing, result) {
54 console.log('testBinary(\'' + thing + '\')');
55 result(null, thing);
56 },
57 testStruct: function(thing, result) {
58 console.log('testStruct(');
59 console.log(thing);
60 console.log(')');
61 result(null, thing);
62 },
63 testNest: function(nest, result) {
64 console.log('testNest(');
65 console.log(nest);
66 console.log(')');
67 result(null, nest);
68 },
69 testMap: function(thing, result) {
70 console.log('testMap(');
71 console.log(thing);
72 console.log(')');
73 result(null, thing);
74 },
75 testStringMap: function(thing, result) {
76 console.log('testStringMap(');
77 console.log(thing);
78 console.log(')');
79 result(null, thing);
80 },
81 testSet: function(thing, result) {
82 console.log('testSet(');
83 console.log(thing);
84 console.log(')');
85 result(null, thing);
86 },
87 testList: function(thing, result) {
88 console.log('testList(');
89 console.log(thing);
90 console.log(')');
91 result(null, thing);
92 },
93 testEnum: function(thing, result) {
94 console.log('testEnum(' + thing + ')');
95 result(null, thing);
96 },
97 testTypedef: function(thing, result) {
98 console.log('testTypedef(' + thing + ')');
99 result(null, thing);
100 },
101 testMapMap: function(hello, result) {
102 console.log('testMapMap(' + hello + ')');
103
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400104 const mapmap = [];
105 const pos = [];
106 const neg = [];
107 for (let i = 1; i < 5; i++) {
Konrad Grochowskif9c4be82015-01-09 11:32:26 +0100108 pos[i] = i;
109 neg[-i] = -i;
110 }
111 mapmap[4] = pos;
112 mapmap[-4] = neg;
113
114 result(null, mapmap);
115 },
116 testInsanity: function(argument, result) {
117 console.log('testInsanity(');
118 console.log(argument);
119 console.log(')');
120
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400121 const hello = new ttypes.Xtruct();
Konrad Grochowskif9c4be82015-01-09 11:32:26 +0100122 hello.string_thing = 'Hello2';
123 hello.byte_thing = 2;
124 hello.i32_thing = 2;
125 hello.i64_thing = 2;
126
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400127 const goodbye = new ttypes.Xtruct();
Konrad Grochowskif9c4be82015-01-09 11:32:26 +0100128 goodbye.string_thing = 'Goodbye4';
129 goodbye.byte_thing = 4;
130 goodbye.i32_thing = 4;
131 goodbye.i64_thing = 4;
132
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400133 const crazy = new ttypes.Insanity();
Konrad Grochowskif9c4be82015-01-09 11:32:26 +0100134 crazy.userMap = [];
135 crazy.userMap[ttypes.Numberz.EIGHT] = 8;
136 crazy.userMap[ttypes.Numberz.FIVE] = 5;
137 crazy.xtructs = [goodbye, hello];
138
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400139 const first_map = [];
140 const second_map = [];
Konrad Grochowskif9c4be82015-01-09 11:32:26 +0100141
142 first_map[ttypes.Numberz.TWO] = crazy;
143 first_map[ttypes.Numberz.THREE] = crazy;
144
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400145 const looney = new ttypes.Insanity();
Konrad Grochowskif9c4be82015-01-09 11:32:26 +0100146 second_map[ttypes.Numberz.SIX] = looney;
147
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400148 const insane = [];
Konrad Grochowskif9c4be82015-01-09 11:32:26 +0100149 insane[1] = first_map;
150 insane[2] = second_map;
151
152 console.log('insane result:');
153 console.log(insane);
154 result(null, insane);
155 },
156 testMulti: function(arg0, arg1, arg2, arg3, arg4, arg5, result) {
157 console.log('testMulti()');
158
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400159 const hello = new ttypes.Xtruct();
Konrad Grochowskif9c4be82015-01-09 11:32:26 +0100160 hello.string_thing = 'Hello2';
161 hello.byte_thing = arg0;
162 hello.i32_thing = arg1;
163 hello.i64_thing = arg2;
164 result(null, hello);
165 },
166 testException: function(arg, result) {
Kazuki Matsudab909a382016-02-13 19:36:09 +0900167 console.log('testException(' + arg + ')');
Konrad Grochowskif9c4be82015-01-09 11:32:26 +0100168 if (arg === 'Xception') {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400169 const x = new ttypes.Xception();
Konrad Grochowskif9c4be82015-01-09 11:32:26 +0100170 x.errorCode = 1001;
171 x.message = arg;
172 result(x);
173 } else if (arg === 'TException') {
174 result(new TException(arg));
175 } else {
176 result(null);
177 }
178 },
179 testMultiException: function(arg0, arg1, result) {
180 console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
181 if (arg0 === ('Xception')) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400182 const x = new ttypes.Xception();
Konrad Grochowskif9c4be82015-01-09 11:32:26 +0100183 x.errorCode = 1001;
184 x.message = 'This is an Xception';
185 result(x);
186 } else if (arg0 === ('Xception2')) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400187 const x2 = new ttypes.Xception2();
Konrad Grochowskif9c4be82015-01-09 11:32:26 +0100188 x2.errorCode = 2002;
189 x2.struct_thing = new ttypes.Xtruct();
190 x2.struct_thing.string_thing = 'This is an Xception2';
191 result(x2);
192 }
193
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400194 const res = new ttypes.Xtruct();
Konrad Grochowskif9c4be82015-01-09 11:32:26 +0100195 res.string_thing = arg1;
196 result(null, res);
197 },
198 testOneway: function(sleepFor, result) {
199 console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!');
200 }
201}; //ThriftTestSvcHandler