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