10 #include <thrift/protocol/TBinaryProtocol.h>
11 #include <thrift/transport/THttpClient.h>
12 #include <thrift/transport/TSocket.h>
13 #include <boost/shared_ptr.hpp>
27 if (args->ipv4 == 123456789) {
32 kv.values.push_back(val);
33 result().key_values.push_back(kv);
37 kv.key.name =
"country";
40 kv.values.push_back(val);
41 result().key_values.push_back(kv);
43 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
51 tdbg(
"ip_service_async2::service(" <<
this <<
"): entered" << std::endl);
56 if (args->ipv4 == 123456789) {
61 kv.values.push_back(val);
62 result().key_values.push_back(kv);
66 kv.key.name =
"country";
69 kv.values.push_back(val);
70 result().key_values.push_back(kv);
72 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
78 tdbg(
"ip_service_async2::service(" <<
this <<
"): finished" << std::endl);
98 using namespace apache::thrift::protocol;
99 using namespace apache::thrift::transport;
100 boost::shared_ptr<THttpClient> transport(
new THttpClient(
"Localhost", 18080,
"/test4"));
101 boost::shared_ptr<TBinaryProtocol> protocol(
new TBinaryProtocol(transport));
102 IpServiceClient client(protocol);
105 struct Socket : THttpClient {
107 static boost::shared_ptr<TTransport>
get(THttpClient* x) {
return x->*(&Socket::transport_); }
109 boost::shared_ptr<TSocket> tsock = boost::dynamic_pointer_cast<TSocket>(Socket::get(transport.get()));
111 tsock->setRecvTimeout(100);
116 int32_t ip = 123456789;
119 client.lookup(r, ip, ipv6);
121 CPPUNIT_ASSERT_MESSAGE(
"TTransportException expected",
false);
122 }
catch (TTransportException& e) {
123 CPPUNIT_ASSERT_MESSAGE(e.what(), std::string(e.what()).find(
"timed out") != std::string::npos);
128 std::this_thread::sleep_for(std::chrono::seconds(1));
130 std::this_thread::sleep_for(std::chrono::seconds(1));
132 tsock->setRecvTimeout(800);
137 int32_t ip = 123456789;
140 client.lookup(r, ip, ipv6);
142 CPPUNIT_ASSERT_MESSAGE(
"TTransportException expected",
false);
143 }
catch (TTransportException& e) {
144 CPPUNIT_ASSERT_MESSAGE(e.what(), std::string(e.what()).find(
"timed out") != std::string::npos);
149 std::this_thread::sleep_for(std::chrono::seconds(1));
151 std::this_thread::sleep_for(std::chrono::seconds(1));
155 using namespace apache::thrift::protocol;
156 using namespace apache::thrift::transport;
157 boost::shared_ptr<THttpClient> transport(
new THttpClient(
"Localhost", 18080, url));
158 boost::shared_ptr<TBinaryProtocol> protocol(
new TBinaryProtocol(transport));
159 IpServiceClient client(protocol);
164 int32_t ip = 123456789;
167 client.lookup(r, ip, ipv6);
169 CPPUNIT_ASSERT(r.key_values.size() == 2);
170 CPPUNIT_ASSERT(r.key_values[0].key.id == 1);
171 CPPUNIT_ASSERT(r.key_values[0].key.name ==
"city");
172 CPPUNIT_ASSERT(r.key_values[0].values.size() == 1);
173 CPPUNIT_ASSERT(r.key_values[0].values[0].id == 123456);
174 CPPUNIT_ASSERT(r.key_values[0].values[0].name ==
"Berlin");
175 CPPUNIT_ASSERT(r.key_values[1].key.id == 2);
176 CPPUNIT_ASSERT(r.key_values[1].key.name ==
"country");
177 CPPUNIT_ASSERT(r.key_values[1].values.size() == 1);
178 CPPUNIT_ASSERT(r.key_values[1].values[0].id == 3345677);
179 CPPUNIT_ASSERT(r.key_values[1].values[0].name ==
"Germany");
182 }
catch (TTransportException& e) {
183 CPPUNIT_ASSERT_MESSAGE(std::string(
"TTransportException: ") + e.what(),
false);
192 client.lookup(r, ip, ipv6);
196 CPPUNIT_ASSERT_MESSAGE(
"TTransportException expected",
false);
197 }
catch (TTransportException& e) {
198 CPPUNIT_ASSERT(e.what() == std::string(
"Bad Status: HTTP/1.1"));
std::unique_ptr< net_io_task > m_srv3
void request_finish_exec()
static void add_task(net_io_task *task)
auto result() -> decltype((m_result.success))
Return a reference to the thrift result object. A thrift handler uses this to return data...
void request(std::string url)
void service(std::shared_ptr< args_t > args)
void set_error(std::string msg)
Report an error.
std::unique_ptr< net_io_task > m_srv2
void service(std::shared_ptr< args_t > args)
std::atomic< bool > g_finished(false)
void exec(exec_task::func_t f)
Execute code in a separate executor thread.
void request_finish_exec_timeout()
std::unique_ptr< net_io_task > m_srv1
void request_finish_in_worker_ctx()
void finish()
Async handlers call this method to trigger the processor callback.