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