blob: 825c5675c8cac8d6d75c30524ab55c14f13f460d [file] [log] [blame]
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +03001/*
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 server side Node test handler for the standard
21// Apache Thrift test service.
22
Cameron Martincaef0ed2025-01-15 11:58:39 +010023const es6Mode = process.argv.includes("--es6");
24const genFolder = es6Mode ? "gen-nodejs-es6" : "gen-nodejs";
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030025const ttypes = require(`./${genFolder}/ThriftTest_types`);
Cameron Martincaef0ed2025-01-15 11:58:39 +010026const TException = require("../../nodejs/lib/thrift").TException;
27const Int64 = require("node-int64");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030028
29exports.ThriftTestHandler = {
Cameron Martincaef0ed2025-01-15 11:58:39 +010030 testVoid: function (result) {
31 console.log("testVoid()");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030032 result(null);
33 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010034 testString: function (thing, result) {
35 console.log("testString('" + thing + "')");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030036 result(null, thing);
37 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010038 testByte: function (thing, result) {
39 console.log("testByte(" + thing + ")");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030040 result(null, thing);
41 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010042 testI32: function (thing, result) {
43 console.log("testI32(" + thing + ")");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030044 result(null, thing);
45 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010046 testI64: function (thing, result) {
47 console.log("testI64(" + thing + ")");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030048 result(null, thing);
49 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010050 testDouble: function (thing, result) {
51 console.log("testDouble(" + thing + ")");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030052 result(null, thing);
53 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010054 testBinary: function (thing, result) {
55 console.log("testBinary('" + thing + "')");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030056 result(null, thing);
57 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010058 testStruct: function (thing, result) {
59 console.log("testStruct(");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030060 console.log(thing);
Cameron Martincaef0ed2025-01-15 11:58:39 +010061 console.log(")");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030062 result(null, thing);
63 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010064 testNest: function (nest, result) {
65 console.log("testNest(");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030066 console.log(nest);
Cameron Martincaef0ed2025-01-15 11:58:39 +010067 console.log(")");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030068 result(null, nest);
69 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010070 testMap: function (thing, result) {
71 console.log("testMap(");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030072 console.log(thing);
Cameron Martincaef0ed2025-01-15 11:58:39 +010073 console.log(")");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030074 result(null, thing);
75 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010076 testStringMap: function (thing, result) {
77 console.log("testStringMap(");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030078 console.log(thing);
Cameron Martincaef0ed2025-01-15 11:58:39 +010079 console.log(")");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030080 result(null, thing);
81 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010082 testSet: function (thing, result) {
83 console.log("testSet(");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030084 console.log(thing);
Cameron Martincaef0ed2025-01-15 11:58:39 +010085 console.log(")");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030086 result(null, thing);
87 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010088 testList: function (thing, result) {
89 console.log("testList(");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030090 console.log(thing);
Cameron Martincaef0ed2025-01-15 11:58:39 +010091 console.log(")");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030092 result(null, thing);
93 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010094 testEnum: function (thing, result) {
95 console.log("testEnum(" + thing + ")");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030096 result(null, thing);
97 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010098 testTypedef: function (thing, result) {
99 console.log("testTypedef(" + thing + ")");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300100 result(null, thing);
101 },
Cameron Martincaef0ed2025-01-15 11:58:39 +0100102 testMapMap: function (hello, result) {
103 console.log("testMapMap(" + hello + ")");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300104
105 const mapmap = [];
106 const pos = [];
107 const neg = [];
108 for (let i = 1; i < 5; i++) {
109 pos[i] = i;
110 neg[-i] = -i;
111 }
112 mapmap[4] = pos;
113 mapmap[-4] = neg;
114
115 result(null, mapmap);
116 },
Cameron Martincaef0ed2025-01-15 11:58:39 +0100117 testInsanity: function (argument, result) {
118 console.log("testInsanity(");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300119 console.log(argument);
Cameron Martincaef0ed2025-01-15 11:58:39 +0100120 console.log(")");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300121
122 const hello = new ttypes.Xtruct();
Cameron Martincaef0ed2025-01-15 11:58:39 +0100123 hello.string_thing = "Hello2";
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300124 hello.byte_thing = 2;
125 hello.i32_thing = 2;
126 hello.i64_thing = new Int64(2);
127
128 const goodbye = new ttypes.Xtruct();
Cameron Martincaef0ed2025-01-15 11:58:39 +0100129 goodbye.string_thing = "Goodbye4";
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300130 goodbye.byte_thing = 4;
131 goodbye.i32_thing = 4;
132 goodbye.i64_thing = new Int64(4);
133
134 const crazy = new ttypes.Insanity();
135 crazy.userMap = [];
136 crazy.userMap[ttypes.Numberz.EIGHT] = 8;
137 crazy.userMap[ttypes.Numberz.FIVE] = 5;
138 crazy.xtructs = [goodbye, hello];
139
140 const first_map = [];
141 const second_map = [];
142
143 first_map[ttypes.Numberz.TWO] = crazy;
144 first_map[ttypes.Numberz.THREE] = crazy;
145
146 const looney = new ttypes.Insanity();
147 second_map[ttypes.Numberz.SIX] = looney;
148
149 const insane = [];
150 insane[1] = first_map;
151 insane[2] = second_map;
152
Cameron Martincaef0ed2025-01-15 11:58:39 +0100153 console.log("insane result:");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300154 console.log(insane);
155 result(null, insane);
156 },
Cameron Martincaef0ed2025-01-15 11:58:39 +0100157 testMulti: function (arg0, arg1, arg2, arg3, arg4, arg5, result) {
158 console.log("testMulti()");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300159
160 const hello = new ttypes.Xtruct();
Cameron Martincaef0ed2025-01-15 11:58:39 +0100161 hello.string_thing = "Hello2";
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300162 hello.byte_thing = arg0;
163 hello.i32_thing = arg1;
164 hello.i64_thing = arg2;
165 result(null, hello);
166 },
Cameron Martincaef0ed2025-01-15 11:58:39 +0100167 testException: function (arg, result) {
168 console.log("testException(" + arg + ")");
169 if (arg === "Xception") {
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300170 const x = new ttypes.Xception();
171 x.errorCode = 1001;
172 x.message = arg;
173 result(x);
Cameron Martincaef0ed2025-01-15 11:58:39 +0100174 } else if (arg === "TException") {
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300175 result(new TException(arg));
176 } else {
177 result(null);
178 }
179 },
Cameron Martincaef0ed2025-01-15 11:58:39 +0100180 testMultiException: function (arg0, arg1, result) {
181 console.log("testMultiException(" + arg0 + ", " + arg1 + ")");
182 if (arg0 === "Xception") {
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300183 const x = new ttypes.Xception();
184 x.errorCode = 1001;
Cameron Martincaef0ed2025-01-15 11:58:39 +0100185 x.message = "This is an Xception";
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300186 result(x);
Cameron Martincaef0ed2025-01-15 11:58:39 +0100187 } else if (arg0 === "Xception2") {
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300188 const x2 = new ttypes.Xception2();
189 x2.errorCode = 2002;
190 x2.struct_thing = new ttypes.Xtruct();
Cameron Martincaef0ed2025-01-15 11:58:39 +0100191 x2.struct_thing.string_thing = "This is an Xception2";
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300192 result(x2);
193 }
194
195 const res = new ttypes.Xtruct();
196 res.string_thing = arg1;
197 result(null, res);
198 },
Cameron Martincaef0ed2025-01-15 11:58:39 +0100199 testOneway: function (sleepFor, result) {
200 console.log(
201 "testOneway(" + sleepFor + ") => JavaScript (like Rust) never sleeps!",
202 );
203 },
204}; //ThriftTestSvcHandler