ra | a2e4e56 | 2014-03-29 01:14:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Abernethy | 96f4f07 | 2015-02-10 02:29:15 -0800 | [diff] [blame] | 20 | //This is the server side Node test handler for the standard |
ra | a2e4e56 | 2014-03-29 01:14:48 -0700 | [diff] [blame] | 21 | // Apache Thrift test service. |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 22 | const helpers = require("./helpers"); |
| 23 | const ttypes = require(`./${helpers.genPath}/ThriftTest_types`); |
| 24 | const TException = require("thrift").Thrift.TException; |
ra | a2e4e56 | 2014-03-29 01:14:48 -0700 | [diff] [blame] | 25 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 26 | function makeSyncHandler() { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 27 | return function(thing) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 28 | return thing; |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 29 | }; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 30 | } |
| 31 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 32 | const syncHandlers = { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 33 | testVoid: testVoid, |
| 34 | testMapMap: testMapMap, |
| 35 | testInsanity: testInsanity, |
| 36 | testMulti: testMulti, |
| 37 | testException: testException, |
| 38 | testMultiException: testMultiException, |
| 39 | testOneway: testOneway |
| 40 | }; |
| 41 | |
| 42 | function makeAsyncHandler(label) { |
| 43 | return function(thing, result) { |
| 44 | thing = syncHandlers[label](thing); |
| 45 | result(null, thing); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 46 | }; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 47 | } |
| 48 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 49 | const asyncHandlers = { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 50 | testVoid: testVoidAsync, |
| 51 | testMulti: testMultiAsync, |
| 52 | testException: testExceptionAsync, |
| 53 | testMultiException: testMultiExceptionAsync, |
| 54 | testOneway: testOnewayAsync |
| 55 | }; |
| 56 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 57 | const 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 Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 73 | ]; |
| 74 | |
| 75 | function testVoid() { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 76 | //console.log('testVoid()'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | function testVoidAsync(result) { |
| 80 | result(testVoid()); |
| 81 | } |
| 82 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 83 | function testMapMap() { |
| 84 | const mapmap = []; |
| 85 | const pos = []; |
| 86 | const neg = []; |
| 87 | for (let i = 1; i < 5; i++) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 88 | pos[i] = i; |
| 89 | neg[-i] = -i; |
| 90 | } |
| 91 | mapmap[4] = pos; |
| 92 | mapmap[-4] = neg; |
| 93 | |
| 94 | return mapmap; |
| 95 | } |
| 96 | |
| 97 | function testInsanity(argument) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 98 | //console.log('testInsanity('); |
| 99 | //console.log(argument); |
| 100 | //console.log(')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 101 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 102 | const first_map = []; |
| 103 | const second_map = []; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 104 | |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 105 | first_map[ttypes.Numberz.TWO] = argument; |
| 106 | first_map[ttypes.Numberz.THREE] = argument; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 107 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 108 | const looney = new ttypes.Insanity(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 109 | second_map[ttypes.Numberz.SIX] = looney; |
| 110 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 111 | const insane = []; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 112 | insane[1] = first_map; |
| 113 | insane[2] = second_map; |
| 114 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 115 | //console.log('insane result:'); |
| 116 | //console.log(insane); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 117 | return insane; |
| 118 | } |
| 119 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 120 | function testMulti(arg0, arg1, arg2) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 121 | //console.log('testMulti()'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 122 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 123 | const hello = new ttypes.Xtruct(); |
| 124 | hello.string_thing = "Hello2"; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 125 | hello.byte_thing = arg0; |
| 126 | hello.i32_thing = arg1; |
| 127 | hello.i64_thing = arg2; |
| 128 | return hello; |
| 129 | } |
| 130 | |
| 131 | function testMultiAsync(arg0, arg1, arg2, arg3, arg4, arg5, result) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 132 | const hello = testMulti(arg0, arg1, arg2, arg3, arg4, arg5); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 133 | result(null, hello); |
| 134 | } |
| 135 | |
| 136 | function testException(arg) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 137 | //console.log('testException('+arg+')'); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 138 | if (arg === "Xception") { |
| 139 | const x = new ttypes.Xception(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 140 | x.errorCode = 1001; |
| 141 | x.message = arg; |
| 142 | throw x; |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 143 | } else if (arg === "TException") { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 144 | throw new TException(arg); |
| 145 | } else { |
| 146 | return; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | function testExceptionAsync(arg, result) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 151 | //console.log('testException('+arg+')'); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 152 | if (arg === "Xception") { |
| 153 | const x = new ttypes.Xception(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 154 | x.errorCode = 1001; |
| 155 | x.message = arg; |
| 156 | result(x); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 157 | } else if (arg === "TException") { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 158 | result(new TException(arg)); |
| 159 | } else { |
| 160 | result(null); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | function testMultiException(arg0, arg1) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 165 | //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')'); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 166 | if (arg0 === "Xception") { |
| 167 | const x = new ttypes.Xception(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 168 | x.errorCode = 1001; |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 169 | x.message = "This is an Xception"; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 170 | throw x; |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 171 | } else if (arg0 === "Xception2") { |
| 172 | const x2 = new ttypes.Xception2(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 173 | x2.errorCode = 2002; |
| 174 | x2.struct_thing = new ttypes.Xtruct(); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 175 | x2.struct_thing.string_thing = "This is an Xception2"; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 176 | throw x2; |
| 177 | } |
| 178 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 179 | const res = new ttypes.Xtruct(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 180 | res.string_thing = arg1; |
| 181 | return res; |
| 182 | } |
| 183 | |
| 184 | function testMultiExceptionAsync(arg0, arg1, result) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 185 | //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')'); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 186 | if (arg0 === "Xception") { |
| 187 | const x = new ttypes.Xception(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 188 | x.errorCode = 1001; |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 189 | x.message = "This is an Xception"; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 190 | result(x); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 191 | } else if (arg0 === "Xception2") { |
| 192 | const x2 = new ttypes.Xception2(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 193 | x2.errorCode = 2002; |
| 194 | x2.struct_thing = new ttypes.Xtruct(); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 195 | x2.struct_thing.string_thing = "This is an Xception2"; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 196 | result(x2); |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 197 | } else { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 198 | const res = new ttypes.Xtruct(); |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 199 | res.string_thing = arg1; |
| 200 | result(null, res); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 201 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 202 | } |
| 203 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 204 | //console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!'); |
| 205 | function testOneway() {} |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 206 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 207 | function testOnewayAsync(sleepFor) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 208 | testOneway(sleepFor); |
| 209 | } |
| 210 | |
| 211 | identityHandlers.forEach(function(label) { |
| 212 | syncHandlers[label] = makeSyncHandler(label); |
| 213 | asyncHandlers[label] = makeAsyncHandler(label); |
| 214 | }); |
| 215 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 216 | ["testMapMap", "testInsanity"].forEach(function(label) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 217 | asyncHandlers[label] = makeAsyncHandler(label); |
| 218 | }); |
| 219 | |
| 220 | exports.ThriftTestHandler = asyncHandlers; |