Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 1 | #!/usr/bin/env ruby |
| 2 | |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 3 | # |
| 4 | # Licensed to the Apache Software Foundation (ASF) under one |
| 5 | # or more contributor license agreements. See the NOTICE file |
| 6 | # distributed with this work for additional information |
| 7 | # regarding copyright ownership. The ASF licenses this file |
| 8 | # to you under the Apache License, Version 2.0 (the |
| 9 | # "License"); you may not use this file except in compliance |
| 10 | # with the License. You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, |
| 15 | # software distributed under the License is distributed on an |
| 16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | # KIND, either express or implied. See the License for the |
| 18 | # specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # |
| 21 | |
| 22 | $:.push File.dirname(__FILE__) + '/..' |
| 23 | |
| 24 | require 'test_helper' |
| 25 | require 'thrift' |
| 26 | require 'thrift_test' |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 27 | require 'thrift_test_types' |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 28 | |
| 29 | class SimpleHandler |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 30 | [:testVoid, :testString, :testBool, :testByte, :testI32, :testI64, :testDouble, :testBinary, |
| 31 | :testStruct, :testMap, :testStringMap, :testSet, :testList, :testNest, :testEnum, :testTypedef, |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 32 | :testEnum, :testTypedef, :testMultiException].each do |meth| |
| 33 | |
| 34 | define_method(meth) do |thing| |
| 35 | thing |
| 36 | end |
| 37 | |
| 38 | end |
| 39 | |
| 40 | def testVoid() |
| 41 | end |
| 42 | |
| 43 | def testInsanity(thing) |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 44 | return { |
| 45 | 1 => { |
| 46 | 2 => thing, |
| 47 | 3 => thing |
| 48 | }, |
| 49 | 2 => { |
| 50 | 6 => Thrift::Test::Insanity::new() |
| 51 | } |
| 52 | } |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 53 | end |
| 54 | |
| 55 | def testMapMap(thing) |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 56 | return { |
| 57 | -4 => { |
| 58 | -4 => -4, |
| 59 | -3 => -3, |
| 60 | -2 => -2, |
| 61 | -1 => -1, |
| 62 | }, |
| 63 | 4 => { |
| 64 | 4 => 4, |
| 65 | 3 => 3, |
| 66 | 2 => 2, |
| 67 | 1 => 1, |
| 68 | } |
| 69 | } |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 70 | end |
| 71 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 72 | def testMulti(arg0, arg1, arg2, arg3, arg4, arg5) |
| 73 | return Thrift::Test::Xtruct.new({ |
| 74 | 'string_thing' => 'Hello2', |
| 75 | 'byte_thing' => arg0, |
| 76 | 'i32_thing' => arg1, |
| 77 | 'i64_thing' => arg2, |
| 78 | }) |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 79 | end |
| 80 | |
| 81 | def testException(thing) |
| 82 | if thing == "Xception" |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 83 | raise Thrift::Test::Xception, :errorCode => 1001, :message => thing |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 84 | elsif thing == "TException" |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 85 | raise Thrift::Exception, :message => thing |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 86 | else |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 87 | # no-op |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 88 | end |
| 89 | end |
| 90 | |
| 91 | def testMultiException(arg0, arg1) |
| 92 | if arg0 == "Xception2" |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 93 | raise Thrift::Test::Xception2, :errorCode => 2002, :struct_thing => ::Thrift::Test::Xtruct.new({ :string_thing => 'This is an Xception2' }) |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 94 | elsif arg0 == "Xception" |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 95 | raise Thrift::Test::Xception, :errorCode => 1001, :message => 'This is an Xception' |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 96 | else |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 97 | return ::Thrift::Test::Xtruct.new({'string_thing' => arg1}) |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 98 | end |
| 99 | end |
| 100 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 101 | def testOneway(arg0) |
| 102 | sleep(arg0) |
| 103 | end |
| 104 | |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 105 | end |
| 106 | |
| 107 | protocol = "binary" |
| 108 | port = 9090 |
| 109 | transport = "buffered" |
| 110 | @transportFactory = Thrift::BufferedTransportFactory.new |
| 111 | @protocolFactory = Thrift::BinaryProtocolFactory.new |
| 112 | ARGV.each do|a| |
| 113 | if a == "--help" |
| 114 | puts "Allowed options:" |
| 115 | puts "\t -h [ --help ] \t produce help message" |
| 116 | puts "\t--port arg (=9090) \t Port number to listen" |
| 117 | puts "\t--protocol arg (=binary) \t protocol: binary, accel" |
| 118 | puts "\t--transport arg (=buffered) transport: buffered, framed, http" |
| 119 | exit |
| 120 | elsif a.start_with?("--protocol") |
| 121 | protocol = a.split("=")[1] |
| 122 | elsif a.start_with?("--transport") |
| 123 | transport = a.split("=")[1] |
| 124 | elsif a.start_with?("--port") |
| 125 | port = a.split("=")[1].to_i |
| 126 | end |
| 127 | end |
| 128 | |
| 129 | if protocol == "binary" |
| 130 | @protocolFactory = Thrift::BinaryProtocolFactory.new |
| 131 | elsif protocol == "" |
| 132 | @protocolFactory = Thrift::BinaryProtocolFactory.new |
| 133 | elsif protocol == "compact" |
| 134 | @protocolFactory = Thrift::CompactProtocolFactory.new |
| 135 | elsif protocol == "json" |
| 136 | @protocolFactory = Thrift::JsonProtocolFactory.new |
| 137 | elsif protocol == "accel" |
| 138 | @protocolFactory = Thrift::BinaryProtocolAcceleratedFactory.new |
| 139 | else |
| 140 | raise 'Unknown protocol type' |
| 141 | end |
| 142 | |
| 143 | if transport == "buffered" |
| 144 | @transportFactory = Thrift::BufferedTransportFactory.new |
| 145 | elsif transport == "" |
| 146 | @transportFactory = Thrift::BufferedTransportFactory.new |
| 147 | elsif transport == "framed" |
| 148 | @transportFactory = Thrift::FramedTransportFactory.new |
| 149 | else |
| 150 | raise 'Unknown transport type' |
| 151 | end |
| 152 | |
| 153 | @handler = SimpleHandler.new |
| 154 | @processor = Thrift::Test::ThriftTest::Processor.new(@handler) |
| 155 | @transport = Thrift::ServerSocket.new(port) |
| 156 | @server = Thrift::ThreadedServer.new(@processor, @transport, @transportFactory, @protocolFactory) |
| 157 | @server.serve |