File tree Expand file tree Collapse file tree 7 files changed +104
-2
lines changed
Expand file tree Collapse file tree 7 files changed +104
-2
lines changed Original file line number Diff line number Diff line change @@ -57,6 +57,7 @@ class TimeoutMonitor {
5757 void setCallback (const Callback &cb) { cb_ = cb; }
5858
5959 void add (const T &value);
60+ void clear ();
6061
6162 void cleanup ();
6263
Original file line number Diff line number Diff line change @@ -73,15 +73,31 @@ void TimeoutMonitor<T>::add(const T &value)
7373 ++value_number_;
7474}
7575
76+ template <typename T>
77+ void TimeoutMonitor<T>::clear()
78+ {
79+ if (value_number_ > 0 ) {
80+ value_number_ = 0 ;
81+ sp_timer_->disable ();
82+
83+ PollItem *item = curr_item_;
84+ do {
85+ item->items .clear ();
86+ item = item->next ;
87+ } while (item != curr_item_);
88+ }
89+ }
90+
7691template <typename T>
7792void TimeoutMonitor<T>::cleanup()
7893{
7994 if (curr_item_ == nullptr )
8095 return ;
8196
82- if (value_number_ > 0 )
97+ if (value_number_ > 0 ) {
8398 sp_timer_->disable ();
84- value_number_ = 0 ;
99+ value_number_ = 0 ;
100+ }
85101
86102 PollItem *item = curr_item_->next ;
87103 curr_item_->next = nullptr ;
Original file line number Diff line number Diff line change @@ -55,6 +55,39 @@ TEST(TimeoutMonitor, Basic)
5555 EXPECT_TRUE (run);
5656}
5757
58+ // ! 测试中途clear()的操作,观察有没有被误触发
59+ TEST (TimeoutMonitor, Clear)
60+ {
61+ auto sp_loop = Loop::New ();
62+ auto sp_timer = sp_loop->newTimerEvent ();
63+ SetScopeExitAction (
64+ [=] {
65+ delete sp_loop;
66+ delete sp_timer;
67+ }
68+ );
69+ sp_timer->initialize (std::chrono::milliseconds (25 ), Event::Mode::kOneshot );
70+
71+ TimeoutMonitor<int > tm (sp_loop);
72+ tm.initialize (milliseconds (10 ), 3 );
73+
74+ sp_timer->setCallback ([&] { tm.clear (); });
75+ sp_timer->enable ();
76+
77+ bool run = false ;
78+ tm.setCallback ([&] (int value) {
79+ run = true ;
80+ });
81+
82+ tm.add (100 );
83+ tm.add (101 );
84+
85+ sp_loop->exitLoop (milliseconds (120 ));
86+ sp_loop->runLoop ();
87+
88+ EXPECT_FALSE (run);
89+ }
90+
5891}
5992}
6093}
Original file line number Diff line number Diff line change @@ -36,6 +36,8 @@ class Proto {
3636 using RecvRespondStrCallback = std::function<void (const std::string &id, const Response &response)>;
3737 using SendDataCallback = std::function<void (const void * data_ptr, size_t data_size)>;
3838
39+ virtual ~Proto () {}
40+
3941 void setRecvCallback (RecvRequestIntCallback &&req_cb, RecvRespondIntCallback &&rsp_cb);
4042 void setRecvCallback (RecvRequestStrCallback &&req_cb, RecvRespondStrCallback &&rsp_cb);
4143 void setSendCallback (SendDataCallback &&cb);
Original file line number Diff line number Diff line change @@ -199,6 +199,17 @@ void Rpc::setStrIdGenFunc(StrIdGenFunc &&func)
199199 }
200200}
201201
202+ void Rpc::clear ()
203+ {
204+ int_id_alloc_ = 0 ;
205+ request_callback_.clear ();
206+ tobe_respond_.clear ();
207+ request_timeout_.clear ();
208+ respond_timeout_.clear ();
209+ int_to_str_map_.clear ();
210+ str_to_int_map_.clear ();
211+ }
212+
202213void Rpc::onRecvRequestInt (int int_id, const std::string &method, const Json &js_params)
203214{
204215 RECORD_SCOPE ();
Original file line number Diff line number Diff line change @@ -89,6 +89,9 @@ class Rpc {
8989 // ! 设置string id生成函数,默认为UUIDv4
9090 void setStrIdGenFunc (StrIdGenFunc &&func);
9191
92+ // ! 清除缓存数据,恢复到没有收发数据之前的状态
93+ void clear ();
94+
9295 protected:
9396 void onRecvRequestInt (int int_id, const std::string &method, const Json ¶ms);
9497 void onRecvRespondInt (int int_id, const Response &response);
Original file line number Diff line number Diff line change @@ -205,5 +205,41 @@ TEST(RpcInt, RequestTimeout) {
205205 EXPECT_TRUE (is_method_cb_invoke);
206206}
207207
208+ // ! 发了一个请求,在回复之前被clear
209+ TEST (RpcInt, Clear) {
210+ auto loop = event::Loop::New ();
211+ auto timer = loop->newTimerEvent ();
212+ SetScopeExitAction (
213+ [=] {
214+ delete loop;
215+ delete timer;
216+ }
217+ );
218+
219+ timer->initialize (std::chrono::milliseconds (50 ), event::Event::Mode::kOneshot );
220+
221+ Rpc rpc (loop);
222+ RawStreamProto proto;
223+ rpc.initialize (&proto, 1 );
224+
225+ timer->setCallback ([&] { rpc.clear (); });
226+ timer->enable ();
227+
228+ bool is_method_cb_invoke = false ;
229+ loop->run (
230+ [&] {
231+ rpc.request (" A" , Json (),
232+ [&] (const Response &r) {
233+ is_method_cb_invoke = true ;
234+ }
235+ );
236+ }
237+ );
238+ loop->exitLoop (std::chrono::milliseconds (1001 ));
239+ loop->runLoop ();
240+
241+ EXPECT_FALSE (is_method_cb_invoke);
242+ }
243+
208244}
209245}
You can’t perform that action at this time.
0 commit comments