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