Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [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 | |
| 20 | $:.push File.dirname(__FILE__) + '/..' |
| 21 | |
| 22 | require 'test_helper' |
| 23 | require 'thrift' |
| 24 | require 'thrift_test' |
| 25 | |
| 26 | class SimpleHandler |
| 27 | [:testVoid, :testString, :testByte, :testI32, :testI64, :testDouble, |
| 28 | :testStruct, :testMap, :testSet, :testList, :testNest, |
| 29 | :testEnum, :testTypedef, :testMultiException].each do |meth| |
| 30 | |
| 31 | define_method(meth) do |thing| |
| 32 | thing |
| 33 | end |
| 34 | |
| 35 | end |
| 36 | |
| 37 | def testVoid() |
| 38 | end |
| 39 | |
| 40 | def testInsanity(thing) |
| 41 | num, uid = thing.userMap.find { true } |
| 42 | return {uid => {num => thing}} |
| 43 | end |
| 44 | |
| 45 | def testMapMap(thing) |
| 46 | return {thing => {thing => thing}} |
| 47 | end |
| 48 | |
| 49 | def testEnum(thing) |
| 50 | return thing |
| 51 | end |
| 52 | |
| 53 | def testTypedef(thing) |
| 54 | return thing |
| 55 | end |
| 56 | |
| 57 | def testException(thing) |
| 58 | if thing == "Xception" |
| 59 | raise Thrift::Test::Xception, :message => thing |
| 60 | elsif thing == "TException" |
| 61 | raise Thrift::Test::TException, :message => thing |
| 62 | else |
| 63 | return arg1 |
| 64 | end |
| 65 | end |
| 66 | |
| 67 | def testMultiException(arg0, arg1) |
| 68 | if arg0 == "Xception2" |
| 69 | raise Thrift::Test::Xception2, :message => 'This is an Xception2' |
| 70 | elsif arg0 == "Xception" |
| 71 | raise Thrift::Test::Xception, :message => 'This is an Xception' |
| 72 | else |
| 73 | return arg1 |
| 74 | end |
| 75 | end |
| 76 | |
| 77 | end |
| 78 | |
| 79 | protocol = "binary" |
| 80 | port = 9090 |
| 81 | transport = "buffered" |
| 82 | @transportFactory = Thrift::BufferedTransportFactory.new |
| 83 | @protocolFactory = Thrift::BinaryProtocolFactory.new |
| 84 | ARGV.each do|a| |
| 85 | if a == "--help" |
| 86 | puts "Allowed options:" |
| 87 | puts "\t -h [ --help ] \t produce help message" |
| 88 | puts "\t--port arg (=9090) \t Port number to listen" |
| 89 | puts "\t--protocol arg (=binary) \t protocol: binary, accel" |
| 90 | puts "\t--transport arg (=buffered) transport: buffered, framed, http" |
| 91 | exit |
| 92 | elsif a.start_with?("--protocol") |
| 93 | protocol = a.split("=")[1] |
| 94 | elsif a.start_with?("--transport") |
| 95 | transport = a.split("=")[1] |
| 96 | elsif a.start_with?("--port") |
| 97 | port = a.split("=")[1].to_i |
| 98 | end |
| 99 | end |
| 100 | |
| 101 | if protocol == "binary" |
| 102 | @protocolFactory = Thrift::BinaryProtocolFactory.new |
| 103 | elsif protocol == "" |
| 104 | @protocolFactory = Thrift::BinaryProtocolFactory.new |
| 105 | elsif protocol == "compact" |
| 106 | @protocolFactory = Thrift::CompactProtocolFactory.new |
| 107 | elsif protocol == "json" |
| 108 | @protocolFactory = Thrift::JsonProtocolFactory.new |
| 109 | elsif protocol == "accel" |
| 110 | @protocolFactory = Thrift::BinaryProtocolAcceleratedFactory.new |
| 111 | else |
| 112 | raise 'Unknown protocol type' |
| 113 | end |
| 114 | |
| 115 | if transport == "buffered" |
| 116 | @transportFactory = Thrift::BufferedTransportFactory.new |
| 117 | elsif transport == "" |
| 118 | @transportFactory = Thrift::BufferedTransportFactory.new |
| 119 | elsif transport == "framed" |
| 120 | @transportFactory = Thrift::FramedTransportFactory.new |
| 121 | else |
| 122 | raise 'Unknown transport type' |
| 123 | end |
| 124 | |
| 125 | @handler = SimpleHandler.new |
| 126 | @processor = Thrift::Test::ThriftTest::Processor.new(@handler) |
| 127 | @transport = Thrift::ServerSocket.new(port) |
| 128 | @server = Thrift::ThreadedServer.new(@processor, @transport, @transportFactory, @protocolFactory) |
| 129 | @server.serve |