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. |
| 22 | |
| 23 | var ttypes = require('./gen-nodejs/ThriftTest_types'); |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 24 | var TException = require('thrift').Thrift.TException; |
ra | a2e4e56 | 2014-03-29 01:14:48 -0700 | [diff] [blame] | 25 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 26 | function makeSyncHandler(label) { |
| 27 | return function(thing) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 28 | //console.log(label + '(\'' + thing + '\')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 29 | return thing; |
ra | a2e4e56 | 2014-03-29 01:14:48 -0700 | [diff] [blame] | 30 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | var syncHandlers = { |
| 34 | testVoid: testVoid, |
| 35 | testMapMap: testMapMap, |
| 36 | testInsanity: testInsanity, |
| 37 | testMulti: testMulti, |
| 38 | testException: testException, |
| 39 | testMultiException: testMultiException, |
| 40 | testOneway: testOneway |
| 41 | }; |
| 42 | |
| 43 | function makeAsyncHandler(label) { |
| 44 | return function(thing, result) { |
| 45 | thing = syncHandlers[label](thing); |
| 46 | result(null, thing); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | var asyncHandlers = { |
| 51 | testVoid: testVoidAsync, |
| 52 | testMulti: testMultiAsync, |
| 53 | testException: testExceptionAsync, |
| 54 | testMultiException: testMultiExceptionAsync, |
| 55 | testOneway: testOnewayAsync |
| 56 | }; |
| 57 | |
| 58 | var identityHandlers = [ |
| 59 | 'testString', |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame^] | 60 | 'testBool', |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 61 | '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 | |
| 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 | |
| 83 | function testMapMap(hello) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 84 | //console.log('testMapMap(' + hello + ')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 85 | |
| 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 | |
| 99 | function testInsanity(argument) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 100 | //console.log('testInsanity('); |
| 101 | //console.log(argument); |
| 102 | //console.log(')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 103 | |
| 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 | |
| 116 | var crazy = new ttypes.Insanity(); |
| 117 | crazy.userMap = []; |
| 118 | crazy.userMap[ttypes.Numberz.EIGHT] = 8; |
| 119 | crazy.userMap[ttypes.Numberz.FIVE] = 5; |
| 120 | crazy.xtructs = [goodbye, hello]; |
| 121 | |
| 122 | var first_map = []; |
| 123 | var second_map = []; |
| 124 | |
| 125 | first_map[ttypes.Numberz.TWO] = crazy; |
| 126 | first_map[ttypes.Numberz.THREE] = crazy; |
| 127 | |
| 128 | var looney = new ttypes.Insanity(); |
| 129 | second_map[ttypes.Numberz.SIX] = looney; |
| 130 | |
| 131 | var insane = []; |
| 132 | insane[1] = first_map; |
| 133 | insane[2] = second_map; |
| 134 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 135 | //console.log('insane result:'); |
| 136 | //console.log(insane); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 137 | return insane; |
| 138 | } |
| 139 | |
| 140 | function testMulti(arg0, arg1, arg2, arg3, arg4, arg5) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 141 | //console.log('testMulti()'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 142 | |
| 143 | var hello = new ttypes.Xtruct(); |
| 144 | hello.string_thing = 'Hello2'; |
| 145 | hello.byte_thing = arg0; |
| 146 | hello.i32_thing = arg1; |
| 147 | hello.i64_thing = arg2; |
| 148 | return hello; |
| 149 | } |
| 150 | |
| 151 | function testMultiAsync(arg0, arg1, arg2, arg3, arg4, arg5, result) { |
| 152 | var hello = testMulti(arg0, arg1, arg2, arg3, arg4, arg5); |
| 153 | result(null, hello); |
| 154 | } |
| 155 | |
| 156 | function testException(arg) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 157 | //console.log('testException('+arg+')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 158 | if (arg === 'Xception') { |
| 159 | var x = new ttypes.Xception(); |
| 160 | x.errorCode = 1001; |
| 161 | x.message = arg; |
| 162 | throw x; |
| 163 | } else if (arg === 'TException') { |
| 164 | throw new TException(arg); |
| 165 | } else { |
| 166 | return; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | function testExceptionAsync(arg, result) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 171 | //console.log('testException('+arg+')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 172 | if (arg === 'Xception') { |
| 173 | var x = new ttypes.Xception(); |
| 174 | x.errorCode = 1001; |
| 175 | x.message = arg; |
| 176 | result(x); |
| 177 | } else if (arg === 'TException') { |
| 178 | result(new TException(arg)); |
| 179 | } else { |
| 180 | result(null); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | function testMultiException(arg0, arg1) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 185 | //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 186 | if (arg0 === ('Xception')) { |
| 187 | var x = new ttypes.Xception(); |
| 188 | x.errorCode = 1001; |
| 189 | x.message = 'This is an Xception'; |
| 190 | throw x; |
| 191 | } else if (arg0 === ('Xception2')) { |
| 192 | var x2 = new ttypes.Xception2(); |
| 193 | x2.errorCode = 2002; |
| 194 | x2.struct_thing = new ttypes.Xtruct(); |
| 195 | x2.struct_thing.string_thing = 'This is an Xception2'; |
| 196 | throw x2; |
| 197 | } |
| 198 | |
| 199 | var res = new ttypes.Xtruct(); |
| 200 | res.string_thing = arg1; |
| 201 | return res; |
| 202 | } |
| 203 | |
| 204 | function testMultiExceptionAsync(arg0, arg1, result) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 205 | //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 206 | if (arg0 === ('Xception')) { |
| 207 | var x = new ttypes.Xception(); |
| 208 | x.errorCode = 1001; |
| 209 | x.message = 'This is an Xception'; |
| 210 | result(x); |
| 211 | } else if (arg0 === ('Xception2')) { |
| 212 | var x2 = new ttypes.Xception2(); |
| 213 | x2.errorCode = 2002; |
| 214 | x2.struct_thing = new ttypes.Xtruct(); |
| 215 | x2.struct_thing.string_thing = 'This is an Xception2'; |
| 216 | result(x2); |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 217 | } else { |
| 218 | var res = new ttypes.Xtruct(); |
| 219 | res.string_thing = arg1; |
| 220 | result(null, res); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 221 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | function testOneway(sleepFor) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 225 | //console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | function testOnewayAsync(sleepFor, result) { |
| 229 | testOneway(sleepFor); |
| 230 | } |
| 231 | |
| 232 | identityHandlers.forEach(function(label) { |
| 233 | syncHandlers[label] = makeSyncHandler(label); |
| 234 | asyncHandlers[label] = makeAsyncHandler(label); |
| 235 | }); |
| 236 | |
| 237 | ['testMapMap', 'testInsanity'].forEach(function(label) { |
| 238 | asyncHandlers[label] = makeAsyncHandler(label); |
| 239 | }); |
| 240 | |
| 241 | exports.ThriftTestHandler = asyncHandlers; |
| 242 | |
| 243 | exports.AsyncThriftTestHandler = asyncHandlers; |
| 244 | exports.SyncThriftTestHandler = asyncHandlers; |