Skip to content

Commit 1ae62c5

Browse files
committed
Fix testTCPClientDisconnectImmediately failed bug : DNSResolve::Cancel bug. Fix EventLoop::QueueInLoop notified_ bug. see Qihoo360#15 Qihoo360#12
1 parent 50cae36 commit 1ae62c5

File tree

5 files changed

+51
-43
lines changed

5 files changed

+51
-43
lines changed

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ script:
3535
after_success:
3636
- cd $CI_HOME
3737
- pwd
38-
- cd build/bin
39-
- ./evpp_unittest
40-
- cd ../../build-release/bin
41-
- ./evpp_unittest
38+
- build/bin/evpp_unittest
39+
- build-release/bin/evpp_unittest
4240
# - coveralls --exclude dependencies --exclude test --exclude include/rpc/msgpack --exclude include/rcp/msgpack.hpp --gcov /usr/bin/gcov-5
4341

evpp/dns_resolver.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ void DNSResolver::Cancel() {
6565
assert(loop_->IsInLoopThread());
6666
if (timer_) {
6767
timer_->Cancel();
68+
timer_.reset();
6869
}
6970
functor_ = Functor(); // Release the callback
7071
}
@@ -95,7 +96,6 @@ void DNSResolver::OnCanceled() {
9596
evdns_getaddrinfo_cancel(dns_req_);
9697
dns_req_ = nullptr;
9798
#endif
98-
timer_.reset();
9999
}
100100

101101

evpp/event_loop.cc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ void EventLoop::RunInLoop(const Functor& functor) {
161161
}
162162

163163
void EventLoop::QueueInLoop(const Functor& cb) {
164+
//LOG_INFO << "this=" << this << " tid=" << std::this_thread::get_id() << " QueueInLoop notified_=" << notified_.load() << " pending_functor_count_=" << pending_functor_count_;
164165
{
165166
#ifdef H_HAVE_BOOST
166167
auto f = new Functor(cb);
@@ -172,14 +173,21 @@ void EventLoop::QueueInLoop(const Functor& cb) {
172173
#endif
173174
}
174175
++pending_functor_count_;
175-
176+
//LOG_INFO << "this=" << this << " tid=" << std::this_thread::get_id() << " QueueInLoop notified_=" << notified_.load() << ", queue a new Functor. pending_functor_count_=" << pending_functor_count_;
176177
if (!notified_.load()) {
178+
//LOG_INFO << "this=" << this << " tid=" << std::this_thread::get_id() << " QueueInLoop call watcher_->Nofity()";
177179
watcher_->Notify();
178-
notified_.store(true);
180+
181+
//TODO This will cause a bug : miss the notify event and make the functor will never be called.
182+
//TODO performance improvement.
183+
//notified_.store(true);
184+
} else {
185+
//LOG_INFO << "this=" << this << " tid=" << std::this_thread::get_id() << " No need to call watcher_->Nofity()";
179186
}
180187
}
181188

182189
void EventLoop::DoPendingFunctors() {
190+
//LOG_INFO << "this=" << this << " tid=" << std::this_thread::get_id() << " DoPendingFunctors pending_functor_count_=" << pending_functor_count_;
183191

184192
#ifdef H_HAVE_BOOST
185193
Functor* f = nullptr;
@@ -195,12 +203,14 @@ void EventLoop::DoPendingFunctors() {
195203
std::lock_guard<std::mutex> lock(mutex_);
196204
notified_.store(false);
197205
pending_functors_->swap(functors);
206+
//LOG_INFO << "this=" << this << " tid=" << std::this_thread::get_id() << " DoPendingFunctors pending_functor_count_=" << pending_functor_count_ << "notified_=" << notified_.load();
198207
}
199-
208+
//LOG_INFO << "this=" << this << " tid=" << std::this_thread::get_id() << " DoPendingFunctors pending_functor_count_=" << pending_functor_count_ << "notified_=" << notified_.load();
200209
for (size_t i = 0; i < functors.size(); ++i) {
201210
functors[i]();
202211
--pending_functor_count_;
203212
}
213+
//LOG_INFO << "this=" << this << " tid=" << std::this_thread::get_id() << " DoPendingFunctors pending_functor_count_=" << pending_functor_count_ << "notified_=" << notified_.load();
204214
#endif
205215
}
206216

test/dns_resolver_test.cc

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,40 @@
55
#include <evpp/dns_resolver.h>
66
#include <atomic>
77

8-
namespace {
9-
static bool resolved = false;
10-
static bool deleted = false;
11-
static void OnResolved(const std::vector <struct in_addr>& addrs) {
12-
resolved = true;
13-
}
14-
15-
static void DeleteDNSResolver(std::shared_ptr<evpp::DNSResolver> r) {
16-
deleted = true;
17-
r.reset();
18-
}
19-
20-
}
21-
22-
238
TEST_UNIT(testDNSResolver) {
24-
evpp::Duration delay(double(1.0)); // 1s
25-
std::unique_ptr<evpp::EventLoopThread> t(new evpp::EventLoopThread);
26-
t->Start(true);
27-
std::shared_ptr<evpp::DNSResolver> dns_resolver(new evpp::DNSResolver(t->event_loop(), "www.so.com", evpp::Duration(1.0), &OnResolved));
28-
dns_resolver->Start();
29-
30-
while (!resolved) {
31-
usleep(1);
9+
for (int i = 0; i < 6; i++) {
10+
bool resolved = false;
11+
bool deleted = false;
12+
auto fn_resolved = [&resolved](const std::vector <struct in_addr>& addrs) {
13+
LOG_INFO << "Entering fn_resolved";
14+
resolved = true;
15+
};
16+
17+
evpp::Duration delay(double(3.0)); // 3s
18+
std::unique_ptr<evpp::EventLoopThread> t(new evpp::EventLoopThread);
19+
t->Start(true);
20+
std::shared_ptr<evpp::DNSResolver> dns_resolver(new evpp::DNSResolver(t->event_loop(), "www.so.com", evpp::Duration(1.0), fn_resolved));
21+
dns_resolver->Start();
22+
23+
while (!resolved) {
24+
usleep(1);
25+
}
26+
27+
auto fn_deleter = [&deleted, dns_resolver]() {
28+
LOG_INFO << "Entering fn_deleter";
29+
deleted = true;
30+
};
31+
32+
t->event_loop()->QueueInLoop(fn_deleter);
33+
dns_resolver.reset();
34+
while (!deleted) {
35+
usleep(1);
36+
}
37+
38+
t->Stop(true);
39+
t.reset();
40+
if (evpp::GetActiveEventCount() != 0) {
41+
H_TEST_ASSERT(evpp::GetActiveEventCount() == 0);
42+
}
3243
}
33-
34-
t->event_loop()->QueueInLoop(std::bind(&DeleteDNSResolver, dns_resolver));
35-
dns_resolver.reset();
36-
while (!deleted) {
37-
usleep(1);
38-
}
39-
40-
t->Stop(true);
41-
t.reset();
42-
H_TEST_ASSERT(evpp::GetActiveEventCount() == 0);
4344
}

test/http_server_test.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ static void TestAll() {
160160

161161

162162
TEST_UNIT(testHTTPServer1) {
163-
for (int j = 0; j < 1000; j++)
164-
for (int i = 0; i < 10; ++i) {
163+
for (int i = 0; i < 5; ++i) {
165164
evpp::http::Server ph(i);
166165
ph.RegisterDefaultHandler(&DefaultRequestHandler);
167166
ph.RegisterHandler("/push/boot", &RequestHandler);

0 commit comments

Comments
 (0)