Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [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 | */ |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 19 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 20 | /* test a C client with a C++ server (that makes sense...) */ |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 21 | |
Christopher Friedt | 9c0de2d | 2022-11-24 20:13:21 -0500 | [diff] [blame^] | 22 | #include <thrift/config.h> |
| 23 | |
| 24 | #ifdef HAVE_UNISTD_H |
| 25 | #include <unistd.h> |
| 26 | #endif |
| 27 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 28 | #include <signal.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/wait.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 31 | #include <thrift/protocol/TBinaryProtocol.h> |
| 32 | #include <thrift/protocol/TDebugProtocol.h> |
| 33 | #include <thrift/server/TSimpleServer.h> |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 34 | #include <memory> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 35 | #include <thrift/transport/TServerSocket.h> |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 36 | #include "ThriftTest.h" |
| 37 | #include "ThriftTest_types.h" |
| 38 | |
| 39 | #include <iostream> |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 40 | #include <map> |
| 41 | #include <set> |
| 42 | #include <string> |
| 43 | #include <vector> |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 44 | |
| 45 | using namespace apache::thrift; |
| 46 | using namespace apache::thrift::concurrency; |
| 47 | using namespace apache::thrift::protocol; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 48 | using namespace apache::thrift::server; |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 49 | using namespace apache::thrift::transport; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 50 | |
| 51 | using namespace thrift::test; |
| 52 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 53 | using std::cout; |
| 54 | using std::endl; |
| 55 | using std::fixed; |
| 56 | using std::make_pair; |
| 57 | using std::map; |
| 58 | using std::set; |
| 59 | using std::string; |
| 60 | using std::vector; |
| 61 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 62 | #define TEST_PORT 9980 |
| 63 | |
| 64 | // Extra functions required for ThriftTest_types to work |
| 65 | namespace thrift { namespace test { |
| 66 | |
| 67 | bool Insanity::operator<(thrift::test::Insanity const& other) const { |
| 68 | using apache::thrift::ThriftDebugString; |
| 69 | return ThriftDebugString(*this) < ThriftDebugString(other); |
| 70 | } |
| 71 | |
| 72 | }} |
| 73 | |
| 74 | class TestHandler : public ThriftTestIf { |
| 75 | public: |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 76 | TestHandler() = default; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 77 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 78 | void testVoid() override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 79 | cout << "[C -> C++] testVoid()" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 82 | void testString(string& out, const string &thing) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 83 | cout << "[C -> C++] testString(\"" << thing << "\")" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 84 | out = thing; |
| 85 | } |
| 86 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 87 | bool testBool(const bool thing) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 88 | cout << "[C -> C++] testBool(" << (thing ? "true" : "false") << ")" << endl; |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 89 | return thing; |
| 90 | } |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 91 | int8_t testByte(const int8_t thing) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 92 | cout << "[C -> C++] testByte(" << (int)thing << ")" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 93 | return thing; |
| 94 | } |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 95 | int32_t testI32(const int32_t thing) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 96 | cout << "[C -> C++] testI32(" << thing << ")" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 97 | return thing; |
| 98 | } |
| 99 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 100 | int64_t testI64(const int64_t thing) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 101 | cout << "[C -> C++] testI64(" << thing << ")" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 102 | return thing; |
| 103 | } |
| 104 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 105 | double testDouble(const double thing) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 106 | cout.precision(6); |
| 107 | cout << "[C -> C++] testDouble(" << fixed << thing << ")" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 108 | return thing; |
| 109 | } |
| 110 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 111 | void testBinary(string& out, const string &thing) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 112 | cout << "[C -> C++] testBinary(\"" << thing << "\")" << endl; |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 113 | out = thing; |
| 114 | } |
| 115 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 116 | void testStruct(Xtruct& out, const Xtruct &thing) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 117 | cout << "[C -> C++] testStruct({\"" << thing.string_thing << "\", " << (int)thing.byte_thing << ", " << thing.i32_thing << ", " << thing.i64_thing << "})" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 118 | out = thing; |
| 119 | } |
| 120 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 121 | void testNest(Xtruct2& out, const Xtruct2& nest) override { |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 122 | const Xtruct &thing = nest.struct_thing; |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 123 | cout << "[C -> C++] testNest({" << (int)nest.byte_thing << ", {\"" << thing.string_thing << "\", " << (int)thing.byte_thing << ", " << thing.i32_thing << ", " << thing.i64_thing << "}, " << nest.i32_thing << "})" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 124 | out = nest; |
| 125 | } |
| 126 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 127 | void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 128 | cout << "[C -> C++] testMap({"; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 129 | map<int32_t, int32_t>::const_iterator m_iter; |
| 130 | bool first = true; |
| 131 | for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) { |
| 132 | if (first) { |
| 133 | first = false; |
| 134 | } else { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 135 | cout << ", "; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 136 | } |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 137 | cout << m_iter->first << " => " << m_iter->second; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 138 | } |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 139 | cout << "})" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 140 | out = thing; |
| 141 | } |
| 142 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 143 | void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 144 | cout << "[C -> C++] testStringMap({"; |
Roger Meier | a1c416f | 2011-06-17 19:40:48 +0000 | [diff] [blame] | 145 | map<std::string, std::string>::const_iterator m_iter; |
| 146 | bool first = true; |
| 147 | for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) { |
| 148 | if (first) { |
| 149 | first = false; |
| 150 | } else { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 151 | cout << ", "; |
Roger Meier | a1c416f | 2011-06-17 19:40:48 +0000 | [diff] [blame] | 152 | } |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 153 | cout << "\"" << m_iter->first << "\" => \"" << m_iter->second << "\""; |
Roger Meier | a1c416f | 2011-06-17 19:40:48 +0000 | [diff] [blame] | 154 | } |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 155 | cout << "})" << endl; |
Roger Meier | a1c416f | 2011-06-17 19:40:48 +0000 | [diff] [blame] | 156 | out = thing; |
| 157 | } |
| 158 | |
| 159 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 160 | void testSet(set<int32_t> &out, const set<int32_t> &thing) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 161 | cout << "[C -> C++] testSet({"; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 162 | set<int32_t>::const_iterator s_iter; |
| 163 | bool first = true; |
| 164 | for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) { |
| 165 | if (first) { |
| 166 | first = false; |
| 167 | } else { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 168 | cout << ", "; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 169 | } |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 170 | cout << *s_iter; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 171 | } |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 172 | cout << "})" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 173 | out = thing; |
| 174 | } |
| 175 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 176 | void testList(vector<int32_t> &out, const vector<int32_t> &thing) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 177 | cout << "[C -> C++] testList({"; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 178 | vector<int32_t>::const_iterator l_iter; |
| 179 | bool first = true; |
| 180 | for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) { |
| 181 | if (first) { |
| 182 | first = false; |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 183 | } else { |
| 184 | cout << ", "; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 185 | } |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 186 | cout << *l_iter; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 187 | } |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 188 | cout << "})" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 189 | out = thing; |
| 190 | } |
| 191 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 192 | Numberz::type testEnum(const Numberz::type thing) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 193 | cout << "[C -> C++] testEnum(" << thing << ")" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 194 | return thing; |
| 195 | } |
| 196 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 197 | UserId testTypedef(const UserId thing) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 198 | cout << "[C -> C++] testTypedef(" << thing << ")" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 199 | return thing; } |
| 200 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 201 | void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 202 | cout << "[C -> C++] testMapMap(" << hello << ")" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 203 | |
| 204 | map<int32_t,int32_t> pos; |
| 205 | map<int32_t,int32_t> neg; |
| 206 | for (int i = 1; i < 5; i++) { |
| 207 | pos.insert(make_pair(i,i)); |
| 208 | neg.insert(make_pair(-i,-i)); |
| 209 | } |
| 210 | |
| 211 | mapmap.insert(make_pair(4, pos)); |
| 212 | mapmap.insert(make_pair(-4, neg)); |
| 213 | |
| 214 | } |
| 215 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 216 | void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) override { |
Simon South | 38e7155 | 2015-08-03 10:51:16 +0000 | [diff] [blame] | 217 | THRIFT_UNUSED_VARIABLE (argument); |
| 218 | |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 219 | cout << "[C -> C++] testInsanity()" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 220 | |
| 221 | Xtruct hello; |
| 222 | hello.string_thing = "Hello2"; |
| 223 | hello.byte_thing = 2; |
| 224 | hello.i32_thing = 2; |
| 225 | hello.i64_thing = 2; |
| 226 | |
| 227 | Xtruct goodbye; |
| 228 | goodbye.string_thing = "Goodbye4"; |
| 229 | goodbye.byte_thing = 4; |
| 230 | goodbye.i32_thing = 4; |
| 231 | goodbye.i64_thing = 4; |
| 232 | |
| 233 | Insanity crazy; |
| 234 | crazy.userMap.insert(make_pair(Numberz::EIGHT, 8)); |
| 235 | crazy.xtructs.push_back(goodbye); |
| 236 | |
| 237 | Insanity looney; |
| 238 | crazy.userMap.insert(make_pair(Numberz::FIVE, 5)); |
| 239 | crazy.xtructs.push_back(hello); |
| 240 | |
| 241 | map<Numberz::type, Insanity> first_map; |
| 242 | map<Numberz::type, Insanity> second_map; |
| 243 | |
| 244 | first_map.insert(make_pair(Numberz::TWO, crazy)); |
| 245 | first_map.insert(make_pair(Numberz::THREE, crazy)); |
| 246 | |
| 247 | second_map.insert(make_pair(Numberz::SIX, looney)); |
| 248 | |
| 249 | insane.insert(make_pair(1, first_map)); |
| 250 | insane.insert(make_pair(2, second_map)); |
| 251 | |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 252 | cout << "return = {"; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 253 | map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter; |
| 254 | for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 255 | cout << i_iter->first << " => {"; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 256 | map<Numberz::type,Insanity>::const_iterator i2_iter; |
| 257 | for (i2_iter = i_iter->second.begin(); |
| 258 | i2_iter != i_iter->second.end(); |
| 259 | ++i2_iter) { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 260 | cout << i2_iter->first << " => {"; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 261 | map<Numberz::type, UserId> userMap = i2_iter->second.userMap; |
| 262 | map<Numberz::type, UserId>::const_iterator um; |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 263 | cout << "{"; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 264 | for (um = userMap.begin(); um != userMap.end(); ++um) { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 265 | cout << um->first << " => " << um->second << ", "; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 266 | } |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 267 | cout << "}, "; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 268 | |
| 269 | vector<Xtruct> xtructs = i2_iter->second.xtructs; |
| 270 | vector<Xtruct>::const_iterator x; |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 271 | cout << "{"; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 272 | for (x = xtructs.begin(); x != xtructs.end(); ++x) { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 273 | cout << "{\"" << x->string_thing << "\", " << (int)x->byte_thing << ", " << x->i32_thing << ", " << x->i64_thing << "}, "; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 274 | } |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 275 | cout << "}"; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 276 | |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 277 | cout << "}, "; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 278 | } |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 279 | cout << "}, "; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 280 | } |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 281 | cout << "}" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 282 | |
| 283 | |
| 284 | } |
| 285 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 286 | void testMulti(Xtruct &hello, const int8_t arg0, const int32_t arg1, const int64_t arg2, const std::map<int16_t, std::string> &arg3, const Numberz::type arg4, const UserId arg5) override { |
Simon South | 38e7155 | 2015-08-03 10:51:16 +0000 | [diff] [blame] | 287 | THRIFT_UNUSED_VARIABLE (arg3); |
| 288 | THRIFT_UNUSED_VARIABLE (arg4); |
| 289 | THRIFT_UNUSED_VARIABLE (arg5); |
| 290 | |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 291 | cout << "[C -> C++] testMulti()" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 292 | |
| 293 | hello.string_thing = "Hello2"; |
| 294 | hello.byte_thing = arg0; |
| 295 | hello.i32_thing = arg1; |
| 296 | hello.i64_thing = (int64_t)arg2; |
| 297 | } |
| 298 | |
| 299 | void testException(const std::string &arg) |
zeshuai007 | c821d25 | 2020-08-14 15:44:02 +0800 | [diff] [blame] | 300 | noexcept(false) override |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 301 | { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 302 | cout << "[C -> C++] testException(" << arg << ")" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 303 | if (arg.compare("Xception") == 0) { |
| 304 | Xception e; |
| 305 | e.errorCode = 1001; |
| 306 | e.message = arg; |
| 307 | throw e; |
| 308 | } else if (arg.compare("ApplicationException") == 0) { |
| 309 | apache::thrift::TException e; |
| 310 | throw e; |
| 311 | } else { |
| 312 | Xtruct result; |
| 313 | result.string_thing = arg; |
| 314 | return; |
| 315 | } |
| 316 | } |
| 317 | |
zeshuai007 | c821d25 | 2020-08-14 15:44:02 +0800 | [diff] [blame] | 318 | void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) noexcept(false) override { |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 319 | |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 320 | cout << "[C -> C++] testMultiException(" << arg0 << ", " << arg1 << ")" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 321 | |
| 322 | if (arg0.compare("Xception") == 0) { |
| 323 | Xception e; |
| 324 | e.errorCode = 1001; |
| 325 | e.message = "This is an Xception"; |
| 326 | throw e; |
| 327 | } else if (arg0.compare("Xception2") == 0) { |
| 328 | Xception2 e; |
| 329 | e.errorCode = 2002; |
| 330 | e.struct_thing.string_thing = "This is an Xception2"; |
| 331 | throw e; |
| 332 | } else { |
| 333 | result.string_thing = arg1; |
| 334 | return; |
| 335 | } |
| 336 | } |
| 337 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 338 | void testOneway(int sleepFor) override { |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 339 | cout << "testOneway(" << sleepFor << "): Sleeping..." << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 340 | sleep(sleepFor); |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 341 | cout << "testOneway(" << sleepFor << "): done sleeping!" << endl; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 342 | } |
| 343 | }; |
| 344 | |
| 345 | // C CLIENT |
| 346 | extern "C" { |
| 347 | |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 348 | #undef THRIFT_SOCKET /* from lib/cpp */ |
| 349 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 350 | #include "t_test_thrift_test.h" |
| 351 | #include "t_test_thrift_test_types.h" |
Roger Meier | e3da768 | 2013-01-11 11:41:53 +0100 | [diff] [blame] | 352 | #include <thrift/c_glib/transport/thrift_socket.h> |
| 353 | #include <thrift/c_glib/protocol/thrift_protocol.h> |
| 354 | #include <thrift/c_glib/protocol/thrift_binary_protocol.h> |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 355 | |
| 356 | static void |
| 357 | test_thrift_client (void) |
| 358 | { |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 359 | ThriftSocket *tsocket = nullptr; |
| 360 | ThriftBinaryProtocol *protocol = nullptr; |
| 361 | TTestThriftTestClient *client = nullptr; |
| 362 | TTestThriftTestIf *iface = nullptr; |
| 363 | GError *error = nullptr; |
| 364 | gchar *string = nullptr; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 365 | gint8 byte = 0; |
| 366 | gint16 i16 = 0; |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 367 | gint32 i32 = 0, another_i32 = 56789; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 368 | gint64 i64 = 0; |
| 369 | double dbl = 0.0; |
| 370 | TTestXtruct *xtruct_in, *xtruct_out; |
| 371 | TTestXtruct2 *xtruct2_in, *xtruct2_out; |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 372 | GHashTable *map_in = nullptr, *map_out = nullptr; |
| 373 | GHashTable *set_in = nullptr, *set_out = nullptr; |
| 374 | GArray *list_in = nullptr, *list_out = nullptr; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 375 | TTestNumberz enum_in, enum_out; |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 376 | TTestUserId user_id_in, user_id_out; |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 377 | GHashTable *insanity_in = nullptr; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 378 | TTestXtruct *xtruct1, *xtruct2; |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 379 | TTestInsanity *insanity_out = nullptr; |
| 380 | TTestXtruct *multi_in = nullptr; |
| 381 | GHashTable *multi_map_out = nullptr; |
| 382 | TTestXception *xception = nullptr; |
| 383 | TTestXception2 *xception2 = nullptr; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 384 | |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame] | 385 | #if (!GLIB_CHECK_VERSION (2, 36, 0)) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 386 | // initialize gobject |
| 387 | g_type_init (); |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame] | 388 | #endif |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 389 | |
| 390 | // create a C client |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 391 | tsocket = (ThriftSocket *) g_object_new (THRIFT_TYPE_SOCKET, |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 392 | "hostname", "localhost", |
zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 393 | "port", TEST_PORT, nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 394 | protocol = (ThriftBinaryProtocol *) g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, |
| 395 | "transport", |
zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 396 | tsocket, nullptr); |
| 397 | client = (TTestThriftTestClient *) g_object_new (T_TEST_TYPE_THRIFT_TEST_CLIENT, "input_protocol", protocol, "output_protocol", protocol, nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 398 | iface = T_TEST_THRIFT_TEST_IF (client); |
| 399 | |
| 400 | // open and send |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 401 | thrift_transport_open (THRIFT_TRANSPORT(tsocket), nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 402 | |
| 403 | assert (t_test_thrift_test_client_test_void (iface, &error) == TRUE); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 404 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 405 | |
| 406 | assert (t_test_thrift_test_client_test_string (iface, &string, "test123", &error) == TRUE); |
| 407 | assert (strcmp (string, "test123") == 0); |
| 408 | g_free (string); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 409 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 410 | |
| 411 | assert (t_test_thrift_test_client_test_byte (iface, &byte, (gint8) 5, &error) == TRUE); |
| 412 | assert (byte == 5); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 413 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 414 | |
| 415 | assert (t_test_thrift_test_client_test_i32 (iface, &i32, 123, &error) == TRUE); |
| 416 | assert (i32 == 123); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 417 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 418 | |
| 419 | assert (t_test_thrift_test_client_test_i64 (iface, &i64, 12345, &error) == TRUE); |
| 420 | assert (i64 == 12345); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 421 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 422 | |
| 423 | assert (t_test_thrift_test_client_test_double (iface, &dbl, 5.6, &error) == TRUE); |
| 424 | assert (dbl == 5.6); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 425 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 426 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 427 | xtruct_out = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 428 | xtruct_out->byte_thing = 1; |
| 429 | xtruct_out->__isset_byte_thing = TRUE; |
| 430 | xtruct_out->i32_thing = 15; |
| 431 | xtruct_out->__isset_i32_thing = TRUE; |
| 432 | xtruct_out->i64_thing = 151; |
| 433 | xtruct_out->__isset_i64_thing = TRUE; |
| 434 | xtruct_out->string_thing = g_strdup ("abc123"); |
| 435 | xtruct_out->__isset_string_thing = TRUE; |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 436 | xtruct_in = (TTestXtruct *) g_object_new(T_TEST_TYPE_XTRUCT, nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 437 | assert (t_test_thrift_test_client_test_struct (iface, &xtruct_in, xtruct_out, &error) == TRUE); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 438 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 439 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 440 | xtruct2_out = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 441 | xtruct2_out->byte_thing = 1; |
| 442 | xtruct2_out->__isset_byte_thing = TRUE; |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 443 | if (xtruct2_out->struct_thing != nullptr) |
Roger Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 444 | g_object_unref(xtruct2_out->struct_thing); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 445 | xtruct2_out->struct_thing = xtruct_out; |
| 446 | xtruct2_out->__isset_struct_thing = TRUE; |
| 447 | xtruct2_out->i32_thing = 123; |
| 448 | xtruct2_out->__isset_i32_thing = TRUE; |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 449 | xtruct2_in = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 450 | assert (t_test_thrift_test_client_test_nest (iface, &xtruct2_in, xtruct2_out, &error) == TRUE); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 451 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 452 | |
| 453 | g_object_unref (xtruct2_out); |
| 454 | g_object_unref (xtruct2_in); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 455 | g_object_unref (xtruct_in); |
| 456 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 457 | map_out = g_hash_table_new (nullptr, nullptr); |
| 458 | map_in = g_hash_table_new (nullptr, nullptr); g_hash_table_insert (map_out, &i32, &i32); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 459 | assert (t_test_thrift_test_client_test_map (iface, &map_in, map_out, &error) == TRUE); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 460 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 461 | g_hash_table_destroy (map_out); |
| 462 | g_hash_table_destroy (map_in); |
| 463 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 464 | map_out = g_hash_table_new (nullptr, nullptr); |
| 465 | map_in = g_hash_table_new (nullptr, nullptr); |
Roger Meier | a1c416f | 2011-06-17 19:40:48 +0000 | [diff] [blame] | 466 | g_hash_table_insert (map_out, g_strdup ("a"), g_strdup ("123")); |
| 467 | g_hash_table_insert (map_out, g_strdup ("a b"), g_strdup ("with spaces ")); |
| 468 | g_hash_table_insert (map_out, g_strdup ("same"), g_strdup ("same")); |
| 469 | g_hash_table_insert (map_out, g_strdup ("0"), g_strdup ("numeric key")); |
| 470 | assert (t_test_thrift_test_client_test_string_map (iface, &map_in, map_out, &error) == TRUE); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 471 | assert (error == nullptr); |
Roger Meier | a1c416f | 2011-06-17 19:40:48 +0000 | [diff] [blame] | 472 | g_hash_table_destroy (map_out); |
| 473 | g_hash_table_destroy (map_in); |
| 474 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 475 | set_out = g_hash_table_new (nullptr, nullptr); |
| 476 | set_in = g_hash_table_new (nullptr, nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 477 | g_hash_table_insert (set_out, &i32, &i32); |
| 478 | assert (t_test_thrift_test_client_test_set (iface, &set_in, set_out, &error) == TRUE); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 479 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 480 | g_hash_table_destroy (set_out); |
| 481 | g_hash_table_destroy (set_in); |
| 482 | |
| 483 | list_out = g_array_new(TRUE, TRUE, sizeof(gint32)); |
| 484 | list_in = g_array_new(TRUE, TRUE, sizeof(gint32)); |
| 485 | another_i32 = 456; |
| 486 | g_array_append_val (list_out, i32); |
| 487 | g_array_append_val (list_out, another_i32); |
| 488 | assert (t_test_thrift_test_client_test_list (iface, &list_in, list_out, &error) == TRUE); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 489 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 490 | g_array_free (list_out, TRUE); |
| 491 | g_array_free (list_in, TRUE); |
| 492 | |
| 493 | enum_out = T_TEST_NUMBERZ_ONE; |
| 494 | assert (t_test_thrift_test_client_test_enum (iface, &enum_in, enum_out, &error) == TRUE); |
| 495 | assert (enum_in == enum_out); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 496 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 497 | |
| 498 | user_id_out = 12345; |
| 499 | assert (t_test_thrift_test_client_test_typedef (iface, &user_id_in, user_id_out, &error) == TRUE); |
| 500 | assert (user_id_in == user_id_out); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 501 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 502 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 503 | map_in = g_hash_table_new (nullptr, nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 504 | assert (t_test_thrift_test_client_test_map_map (iface, &map_in, i32, &error) == TRUE); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 505 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 506 | g_hash_table_destroy (map_in); |
| 507 | |
| 508 | // insanity |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 509 | insanity_out = (TTestInsanity *) g_object_new (T_TEST_TYPE_INSANITY, nullptr); |
| 510 | insanity_out->userMap = g_hash_table_new (nullptr, nullptr); |
Roger Meier | 0cc6d3c | 2014-09-04 00:24:17 +0200 | [diff] [blame] | 511 | g_hash_table_insert (insanity_out->userMap, GINT_TO_POINTER (enum_out), &user_id_out); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 512 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 513 | xtruct1 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 514 | xtruct1->byte_thing = 1; |
| 515 | xtruct1->__isset_byte_thing = TRUE; |
| 516 | xtruct1->i32_thing = 15; |
| 517 | xtruct1->__isset_i32_thing = TRUE; |
| 518 | xtruct1->i64_thing = 151; |
| 519 | xtruct1->__isset_i64_thing = TRUE; |
| 520 | xtruct1->string_thing = g_strdup ("abc123"); |
| 521 | xtruct1->__isset_string_thing = TRUE; |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 522 | xtruct2 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 523 | xtruct2->byte_thing = 1; |
| 524 | xtruct2->__isset_byte_thing = TRUE; |
| 525 | xtruct2->i32_thing = 15; |
| 526 | xtruct2->__isset_i32_thing = TRUE; |
| 527 | xtruct2->i64_thing = 151; |
| 528 | xtruct2->__isset_i64_thing = TRUE; |
| 529 | xtruct2->string_thing = g_strdup ("abc123"); |
| 530 | xtruct2->__isset_string_thing = TRUE; |
| 531 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 532 | insanity_in = g_hash_table_new (nullptr, nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 533 | g_ptr_array_add (insanity_out->xtructs, xtruct1); |
| 534 | g_ptr_array_add (insanity_out->xtructs, xtruct2); |
| 535 | assert (t_test_thrift_test_client_test_insanity (iface, &insanity_in, insanity_out, &error) == TRUE); |
| 536 | |
| 537 | g_hash_table_unref (insanity_in); |
| 538 | g_ptr_array_free (insanity_out->xtructs, TRUE); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 539 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 540 | multi_map_out = g_hash_table_new (nullptr, nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 541 | string = g_strdup ("abc123"); |
| 542 | g_hash_table_insert (multi_map_out, &i16, string); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 543 | multi_in = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 544 | assert (t_test_thrift_test_client_test_multi (iface, &multi_in, byte, i32, i64, multi_map_out, enum_out, user_id_out, &error) == TRUE); |
| 545 | assert (multi_in->i32_thing == i32); |
| 546 | assert (multi_in->i64_thing == i64); |
| 547 | g_object_unref (multi_in); |
| 548 | g_hash_table_unref (multi_map_out); |
Roger Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 549 | g_free (string); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 550 | |
| 551 | assert (t_test_thrift_test_client_test_exception (iface, "Xception", &xception, &error) == FALSE); |
| 552 | assert (xception->errorCode == 1001); |
| 553 | g_error_free (error); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 554 | error = nullptr; |
Roger Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 555 | g_object_unref (xception); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 556 | xception = nullptr; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 557 | |
| 558 | assert (t_test_thrift_test_client_test_exception (iface, "ApplicationException", &xception, &error) == FALSE); |
| 559 | g_error_free (error); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 560 | error = nullptr; |
| 561 | assert (xception == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 562 | |
| 563 | assert (t_test_thrift_test_client_test_exception (iface, "Test", &xception, &error) == TRUE); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 564 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 565 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 566 | multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr); |
| 567 | assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception", nullptr, &xception, &xception2, &error) == FALSE); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 568 | assert (xception->errorCode == 1001); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 569 | assert (xception2 == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 570 | g_error_free (error); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 571 | error = nullptr; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 572 | g_object_unref (xception); |
Roger Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 573 | g_object_unref (multi_in); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 574 | xception = nullptr; |
| 575 | multi_in = nullptr; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 576 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 577 | multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr); |
| 578 | assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception2", nullptr, &xception, &xception2, &error) == FALSE); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 579 | assert (xception2->errorCode == 2002); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 580 | assert (xception == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 581 | g_error_free (error); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 582 | error = nullptr; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 583 | g_object_unref (xception2); |
Roger Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 584 | g_object_unref (multi_in); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 585 | xception2 = nullptr; |
| 586 | multi_in = nullptr; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 587 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 588 | multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr); |
| 589 | assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, nullptr , nullptr, &xception, &xception2, &error) == TRUE); |
| 590 | assert (error == nullptr); |
Roger Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 591 | g_object_unref(multi_in); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 592 | multi_in = nullptr; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 593 | |
| 594 | assert (t_test_thrift_test_client_test_oneway (iface, 1, &error) == TRUE); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 595 | assert (error == nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 596 | |
| 597 | /* sleep to let the oneway call go through */ |
| 598 | sleep (5); |
| 599 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 600 | thrift_transport_close (THRIFT_TRANSPORT(tsocket), nullptr); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 601 | g_object_unref (client); |
| 602 | g_object_unref (protocol); |
| 603 | g_object_unref (tsocket); |
| 604 | } |
| 605 | |
| 606 | |
| 607 | } /* extern "C" */ |
| 608 | |
| 609 | |
| 610 | static void |
| 611 | bailout (int signum) |
| 612 | { |
Simon South | 38e7155 | 2015-08-03 10:51:16 +0000 | [diff] [blame] | 613 | THRIFT_UNUSED_VARIABLE (signum); |
| 614 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 615 | exit (1); |
| 616 | } |
| 617 | |
| 618 | int |
Simon South | 38e7155 | 2015-08-03 10:51:16 +0000 | [diff] [blame] | 619 | main (void) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 620 | { |
| 621 | int status; |
| 622 | int pid = fork (); |
| 623 | assert (pid >= 0); |
| 624 | |
| 625 | if (pid == 0) /* child */ |
| 626 | { |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 627 | std::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); |
| 628 | std::shared_ptr<TestHandler> testHandler(new TestHandler()); |
| 629 | std::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler)); |
| 630 | std::shared_ptr<TServerSocket> serverSocket(new TServerSocket(TEST_PORT)); |
| 631 | std::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 632 | TSimpleServer simpleServer(testProcessor, serverSocket, transportFactory, protocolFactory); |
| 633 | signal (SIGALRM, bailout); |
| 634 | alarm (60); |
| 635 | simpleServer.serve(); |
| 636 | } else { |
| 637 | sleep (1); |
| 638 | test_thrift_client (); |
| 639 | kill (pid, SIGINT); |
Simon South | 38e7155 | 2015-08-03 10:51:16 +0000 | [diff] [blame] | 640 | assert (wait (&status) == pid); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |