forked from sanyaade-mobiledev/chromium.src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc_sync_channel_unittest.cc
1795 lines (1526 loc) · 55.9 KB
/
ipc_sync_channel_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ipc/ipc_sync_channel.h"
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/process/process_handle.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread.h"
#include "ipc/ipc_listener.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_sender.h"
#include "ipc/ipc_sync_message_filter.h"
#include "ipc/ipc_sync_message_unittest.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::WaitableEvent;
namespace IPC {
namespace {
// Base class for a "process" with listener and IPC threads.
class Worker : public Listener, public Sender {
public:
// Will create a channel without a name.
Worker(Channel::Mode mode, const std::string& thread_name)
: done_(new WaitableEvent(false, false)),
channel_created_(new WaitableEvent(false, false)),
mode_(mode),
ipc_thread_((thread_name + "_ipc").c_str()),
listener_thread_((thread_name + "_listener").c_str()),
overrided_thread_(NULL),
shutdown_event_(true, false),
is_shutdown_(false) {
}
// Will create a named channel and use this name for the threads' name.
Worker(const std::string& channel_name, Channel::Mode mode)
: done_(new WaitableEvent(false, false)),
channel_created_(new WaitableEvent(false, false)),
channel_name_(channel_name),
mode_(mode),
ipc_thread_((channel_name + "_ipc").c_str()),
listener_thread_((channel_name + "_listener").c_str()),
overrided_thread_(NULL),
shutdown_event_(true, false),
is_shutdown_(false) {
}
virtual ~Worker() {
// Shutdown() must be called before destruction.
CHECK(is_shutdown_);
}
void AddRef() { }
void Release() { }
virtual bool Send(Message* msg) OVERRIDE { return channel_->Send(msg); }
void WaitForChannelCreation() { channel_created_->Wait(); }
void CloseChannel() {
DCHECK(base::MessageLoop::current() == ListenerThread()->message_loop());
channel_->Close();
}
void Start() {
StartThread(&listener_thread_, base::MessageLoop::TYPE_DEFAULT);
ListenerThread()->message_loop()->PostTask(
FROM_HERE, base::Bind(&Worker::OnStart, this));
}
void Shutdown() {
// The IPC thread needs to outlive SyncChannel. We can't do this in
// ~Worker(), since that'll reset the vtable pointer (to Worker's), which
// may result in a race conditions. See http://crbug.com/25841.
WaitableEvent listener_done(false, false), ipc_done(false, false);
ListenerThread()->message_loop()->PostTask(
FROM_HERE, base::Bind(&Worker::OnListenerThreadShutdown1, this,
&listener_done, &ipc_done));
listener_done.Wait();
ipc_done.Wait();
ipc_thread_.Stop();
listener_thread_.Stop();
is_shutdown_ = true;
}
void OverrideThread(base::Thread* overrided_thread) {
DCHECK(overrided_thread_ == NULL);
overrided_thread_ = overrided_thread;
}
bool SendAnswerToLife(bool pump, bool succeed) {
int answer = 0;
SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer);
if (pump)
msg->EnableMessagePumping();
bool result = Send(msg);
DCHECK_EQ(result, succeed);
DCHECK_EQ(answer, (succeed ? 42 : 0));
return result;
}
bool SendDouble(bool pump, bool succeed) {
int answer = 0;
SyncMessage* msg = new SyncChannelTestMsg_Double(5, &answer);
if (pump)
msg->EnableMessagePumping();
bool result = Send(msg);
DCHECK_EQ(result, succeed);
DCHECK_EQ(answer, (succeed ? 10 : 0));
return result;
}
const std::string& channel_name() { return channel_name_; }
Channel::Mode mode() { return mode_; }
WaitableEvent* done_event() { return done_.get(); }
WaitableEvent* shutdown_event() { return &shutdown_event_; }
void ResetChannel() { channel_.reset(); }
// Derived classes need to call this when they've completed their part of
// the test.
void Done() { done_->Signal(); }
protected:
SyncChannel* channel() { return channel_.get(); }
// Functions for dervied classes to implement if they wish.
virtual void Run() { }
virtual void OnAnswer(int* answer) { NOTREACHED(); }
virtual void OnAnswerDelay(Message* reply_msg) {
// The message handler map below can only take one entry for
// SyncChannelTestMsg_AnswerToLife, so since some classes want
// the normal version while other want the delayed reply, we
// call the normal version if the derived class didn't override
// this function.
int answer;
OnAnswer(&answer);
SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, answer);
Send(reply_msg);
}
virtual void OnDouble(int in, int* out) { NOTREACHED(); }
virtual void OnDoubleDelay(int in, Message* reply_msg) {
int result;
OnDouble(in, &result);
SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result);
Send(reply_msg);
}
virtual void OnNestedTestMsg(Message* reply_msg) {
NOTREACHED();
}
virtual SyncChannel* CreateChannel() {
scoped_ptr<SyncChannel> channel = SyncChannel::Create(
channel_name_, mode_, this, ipc_thread_.message_loop_proxy().get(),
true, &shutdown_event_);
return channel.release();
}
base::Thread* ListenerThread() {
return overrided_thread_ ? overrided_thread_ : &listener_thread_;
}
const base::Thread& ipc_thread() const { return ipc_thread_; }
private:
// Called on the listener thread to create the sync channel.
void OnStart() {
// Link ipc_thread_, listener_thread_ and channel_ altogether.
StartThread(&ipc_thread_, base::MessageLoop::TYPE_IO);
channel_.reset(CreateChannel());
channel_created_->Signal();
Run();
}
void OnListenerThreadShutdown1(WaitableEvent* listener_event,
WaitableEvent* ipc_event) {
// SyncChannel needs to be destructed on the thread that it was created on.
channel_.reset();
base::RunLoop().RunUntilIdle();
ipc_thread_.message_loop()->PostTask(
FROM_HERE, base::Bind(&Worker::OnIPCThreadShutdown, this,
listener_event, ipc_event));
}
void OnIPCThreadShutdown(WaitableEvent* listener_event,
WaitableEvent* ipc_event) {
base::RunLoop().RunUntilIdle();
ipc_event->Signal();
listener_thread_.message_loop()->PostTask(
FROM_HERE, base::Bind(&Worker::OnListenerThreadShutdown2, this,
listener_event));
}
void OnListenerThreadShutdown2(WaitableEvent* listener_event) {
base::RunLoop().RunUntilIdle();
listener_event->Signal();
}
virtual bool OnMessageReceived(const Message& message) OVERRIDE {
IPC_BEGIN_MESSAGE_MAP(Worker, message)
IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay)
IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife,
OnAnswerDelay)
IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelNestedTestMsg_String,
OnNestedTestMsg)
IPC_END_MESSAGE_MAP()
return true;
}
void StartThread(base::Thread* thread, base::MessageLoop::Type type) {
base::Thread::Options options;
options.message_loop_type = type;
thread->StartWithOptions(options);
}
scoped_ptr<WaitableEvent> done_;
scoped_ptr<WaitableEvent> channel_created_;
std::string channel_name_;
Channel::Mode mode_;
scoped_ptr<SyncChannel> channel_;
base::Thread ipc_thread_;
base::Thread listener_thread_;
base::Thread* overrided_thread_;
base::WaitableEvent shutdown_event_;
bool is_shutdown_;
DISALLOW_COPY_AND_ASSIGN(Worker);
};
// Starts the test with the given workers. This function deletes the workers
// when it's done.
void RunTest(std::vector<Worker*> workers) {
// First we create the workers that are channel servers, or else the other
// workers' channel initialization might fail because the pipe isn't created..
for (size_t i = 0; i < workers.size(); ++i) {
if (workers[i]->mode() & Channel::MODE_SERVER_FLAG) {
workers[i]->Start();
workers[i]->WaitForChannelCreation();
}
}
// now create the clients
for (size_t i = 0; i < workers.size(); ++i) {
if (workers[i]->mode() & Channel::MODE_CLIENT_FLAG)
workers[i]->Start();
}
// wait for all the workers to finish
for (size_t i = 0; i < workers.size(); ++i)
workers[i]->done_event()->Wait();
for (size_t i = 0; i < workers.size(); ++i) {
workers[i]->Shutdown();
delete workers[i];
}
}
class IPCSyncChannelTest : public testing::Test {
private:
base::MessageLoop message_loop_;
};
//------------------------------------------------------------------------------
class SimpleServer : public Worker {
public:
explicit SimpleServer(bool pump_during_send)
: Worker(Channel::MODE_SERVER, "simpler_server"),
pump_during_send_(pump_during_send) { }
virtual void Run() OVERRIDE {
SendAnswerToLife(pump_during_send_, true);
Done();
}
bool pump_during_send_;
};
class SimpleClient : public Worker {
public:
SimpleClient() : Worker(Channel::MODE_CLIENT, "simple_client") { }
virtual void OnAnswer(int* answer) OVERRIDE {
*answer = 42;
Done();
}
};
void Simple(bool pump_during_send) {
std::vector<Worker*> workers;
workers.push_back(new SimpleServer(pump_during_send));
workers.push_back(new SimpleClient());
RunTest(workers);
}
// Tests basic synchronous call
TEST_F(IPCSyncChannelTest, Simple) {
Simple(false);
Simple(true);
}
//------------------------------------------------------------------------------
// Worker classes which override how the sync channel is created to use the
// two-step initialization (calling the lightweight constructor and then
// ChannelProxy::Init separately) process.
class TwoStepServer : public Worker {
public:
explicit TwoStepServer(bool create_pipe_now)
: Worker(Channel::MODE_SERVER, "simpler_server"),
create_pipe_now_(create_pipe_now) { }
virtual void Run() OVERRIDE {
SendAnswerToLife(false, true);
Done();
}
virtual SyncChannel* CreateChannel() OVERRIDE {
SyncChannel* channel =
SyncChannel::Create(channel_name(), mode(), this,
ipc_thread().message_loop_proxy().get(),
create_pipe_now_,
shutdown_event()).release();
return channel;
}
bool create_pipe_now_;
};
class TwoStepClient : public Worker {
public:
TwoStepClient(bool create_pipe_now)
: Worker(Channel::MODE_CLIENT, "simple_client"),
create_pipe_now_(create_pipe_now) { }
virtual void OnAnswer(int* answer) OVERRIDE {
*answer = 42;
Done();
}
virtual SyncChannel* CreateChannel() OVERRIDE {
SyncChannel* channel =
SyncChannel::Create(channel_name(), mode(), this,
ipc_thread().message_loop_proxy().get(),
create_pipe_now_,
shutdown_event()).release();
return channel;
}
bool create_pipe_now_;
};
void TwoStep(bool create_server_pipe_now, bool create_client_pipe_now) {
std::vector<Worker*> workers;
workers.push_back(new TwoStepServer(create_server_pipe_now));
workers.push_back(new TwoStepClient(create_client_pipe_now));
RunTest(workers);
}
// Tests basic two-step initialization, where you call the lightweight
// constructor then Init.
TEST_F(IPCSyncChannelTest, TwoStepInitialization) {
TwoStep(false, false);
TwoStep(false, true);
TwoStep(true, false);
TwoStep(true, true);
}
//------------------------------------------------------------------------------
class DelayClient : public Worker {
public:
DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { }
virtual void OnAnswerDelay(Message* reply_msg) OVERRIDE {
SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
Send(reply_msg);
Done();
}
};
void DelayReply(bool pump_during_send) {
std::vector<Worker*> workers;
workers.push_back(new SimpleServer(pump_during_send));
workers.push_back(new DelayClient());
RunTest(workers);
}
// Tests that asynchronous replies work
TEST_F(IPCSyncChannelTest, DelayReply) {
DelayReply(false);
DelayReply(true);
}
//------------------------------------------------------------------------------
class NoHangServer : public Worker {
public:
NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send)
: Worker(Channel::MODE_SERVER, "no_hang_server"),
got_first_reply_(got_first_reply),
pump_during_send_(pump_during_send) { }
virtual void Run() OVERRIDE {
SendAnswerToLife(pump_during_send_, true);
got_first_reply_->Signal();
SendAnswerToLife(pump_during_send_, false);
Done();
}
WaitableEvent* got_first_reply_;
bool pump_during_send_;
};
class NoHangClient : public Worker {
public:
explicit NoHangClient(WaitableEvent* got_first_reply)
: Worker(Channel::MODE_CLIENT, "no_hang_client"),
got_first_reply_(got_first_reply) { }
virtual void OnAnswerDelay(Message* reply_msg) OVERRIDE {
// Use the DELAY_REPLY macro so that we can force the reply to be sent
// before this function returns (when the channel will be reset).
SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
Send(reply_msg);
got_first_reply_->Wait();
CloseChannel();
Done();
}
WaitableEvent* got_first_reply_;
};
void NoHang(bool pump_during_send) {
WaitableEvent got_first_reply(false, false);
std::vector<Worker*> workers;
workers.push_back(new NoHangServer(&got_first_reply, pump_during_send));
workers.push_back(new NoHangClient(&got_first_reply));
RunTest(workers);
}
// Tests that caller doesn't hang if receiver dies
TEST_F(IPCSyncChannelTest, NoHang) {
NoHang(false);
NoHang(true);
}
//------------------------------------------------------------------------------
class UnblockServer : public Worker {
public:
UnblockServer(bool pump_during_send, bool delete_during_send)
: Worker(Channel::MODE_SERVER, "unblock_server"),
pump_during_send_(pump_during_send),
delete_during_send_(delete_during_send) { }
virtual void Run() OVERRIDE {
if (delete_during_send_) {
// Use custom code since race conditions mean the answer may or may not be
// available.
int answer = 0;
SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer);
if (pump_during_send_)
msg->EnableMessagePumping();
Send(msg);
} else {
SendAnswerToLife(pump_during_send_, true);
}
Done();
}
virtual void OnDoubleDelay(int in, Message* reply_msg) OVERRIDE {
SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
Send(reply_msg);
if (delete_during_send_)
ResetChannel();
}
bool pump_during_send_;
bool delete_during_send_;
};
class UnblockClient : public Worker {
public:
explicit UnblockClient(bool pump_during_send)
: Worker(Channel::MODE_CLIENT, "unblock_client"),
pump_during_send_(pump_during_send) { }
virtual void OnAnswer(int* answer) OVERRIDE {
SendDouble(pump_during_send_, true);
*answer = 42;
Done();
}
bool pump_during_send_;
};
void Unblock(bool server_pump, bool client_pump, bool delete_during_send) {
std::vector<Worker*> workers;
workers.push_back(new UnblockServer(server_pump, delete_during_send));
workers.push_back(new UnblockClient(client_pump));
RunTest(workers);
}
// Tests that the caller unblocks to answer a sync message from the receiver.
TEST_F(IPCSyncChannelTest, Unblock) {
Unblock(false, false, false);
Unblock(false, true, false);
Unblock(true, false, false);
Unblock(true, true, false);
}
//------------------------------------------------------------------------------
// Tests that the the SyncChannel object can be deleted during a Send.
TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) {
Unblock(false, false, true);
Unblock(false, true, true);
Unblock(true, false, true);
Unblock(true, true, true);
}
//------------------------------------------------------------------------------
class RecursiveServer : public Worker {
public:
RecursiveServer(bool expected_send_result, bool pump_first, bool pump_second)
: Worker(Channel::MODE_SERVER, "recursive_server"),
expected_send_result_(expected_send_result),
pump_first_(pump_first), pump_second_(pump_second) {}
virtual void Run() OVERRIDE {
SendDouble(pump_first_, expected_send_result_);
Done();
}
virtual void OnDouble(int in, int* out) OVERRIDE {
*out = in * 2;
SendAnswerToLife(pump_second_, expected_send_result_);
}
bool expected_send_result_, pump_first_, pump_second_;
};
class RecursiveClient : public Worker {
public:
RecursiveClient(bool pump_during_send, bool close_channel)
: Worker(Channel::MODE_CLIENT, "recursive_client"),
pump_during_send_(pump_during_send), close_channel_(close_channel) {}
virtual void OnDoubleDelay(int in, Message* reply_msg) OVERRIDE {
SendDouble(pump_during_send_, !close_channel_);
if (close_channel_) {
delete reply_msg;
} else {
SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
Send(reply_msg);
}
Done();
}
virtual void OnAnswerDelay(Message* reply_msg) OVERRIDE {
if (close_channel_) {
delete reply_msg;
CloseChannel();
} else {
SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
Send(reply_msg);
}
}
bool pump_during_send_, close_channel_;
};
void Recursive(
bool server_pump_first, bool server_pump_second, bool client_pump) {
std::vector<Worker*> workers;
workers.push_back(
new RecursiveServer(true, server_pump_first, server_pump_second));
workers.push_back(new RecursiveClient(client_pump, false));
RunTest(workers);
}
// Tests a server calling Send while another Send is pending.
TEST_F(IPCSyncChannelTest, Recursive) {
Recursive(false, false, false);
Recursive(false, false, true);
Recursive(false, true, false);
Recursive(false, true, true);
Recursive(true, false, false);
Recursive(true, false, true);
Recursive(true, true, false);
Recursive(true, true, true);
}
//------------------------------------------------------------------------------
void RecursiveNoHang(
bool server_pump_first, bool server_pump_second, bool client_pump) {
std::vector<Worker*> workers;
workers.push_back(
new RecursiveServer(false, server_pump_first, server_pump_second));
workers.push_back(new RecursiveClient(client_pump, true));
RunTest(workers);
}
// Tests that if a caller makes a sync call during an existing sync call and
// the receiver dies, neither of the Send() calls hang.
TEST_F(IPCSyncChannelTest, RecursiveNoHang) {
RecursiveNoHang(false, false, false);
RecursiveNoHang(false, false, true);
RecursiveNoHang(false, true, false);
RecursiveNoHang(false, true, true);
RecursiveNoHang(true, false, false);
RecursiveNoHang(true, false, true);
RecursiveNoHang(true, true, false);
RecursiveNoHang(true, true, true);
}
//------------------------------------------------------------------------------
class MultipleServer1 : public Worker {
public:
explicit MultipleServer1(bool pump_during_send)
: Worker("test_channel1", Channel::MODE_SERVER),
pump_during_send_(pump_during_send) { }
virtual void Run() OVERRIDE {
SendDouble(pump_during_send_, true);
Done();
}
bool pump_during_send_;
};
class MultipleClient1 : public Worker {
public:
MultipleClient1(WaitableEvent* client1_msg_received,
WaitableEvent* client1_can_reply) :
Worker("test_channel1", Channel::MODE_CLIENT),
client1_msg_received_(client1_msg_received),
client1_can_reply_(client1_can_reply) { }
virtual void OnDouble(int in, int* out) OVERRIDE {
client1_msg_received_->Signal();
*out = in * 2;
client1_can_reply_->Wait();
Done();
}
private:
WaitableEvent *client1_msg_received_, *client1_can_reply_;
};
class MultipleServer2 : public Worker {
public:
MultipleServer2() : Worker("test_channel2", Channel::MODE_SERVER) { }
virtual void OnAnswer(int* result) OVERRIDE {
*result = 42;
Done();
}
};
class MultipleClient2 : public Worker {
public:
MultipleClient2(
WaitableEvent* client1_msg_received, WaitableEvent* client1_can_reply,
bool pump_during_send)
: Worker("test_channel2", Channel::MODE_CLIENT),
client1_msg_received_(client1_msg_received),
client1_can_reply_(client1_can_reply),
pump_during_send_(pump_during_send) { }
virtual void Run() OVERRIDE {
client1_msg_received_->Wait();
SendAnswerToLife(pump_during_send_, true);
client1_can_reply_->Signal();
Done();
}
private:
WaitableEvent *client1_msg_received_, *client1_can_reply_;
bool pump_during_send_;
};
void Multiple(bool server_pump, bool client_pump) {
std::vector<Worker*> workers;
// A shared worker thread so that server1 and server2 run on one thread.
base::Thread worker_thread("Multiple");
ASSERT_TRUE(worker_thread.Start());
// Server1 sends a sync msg to client1, which blocks the reply until
// server2 (which runs on the same worker thread as server1) responds
// to a sync msg from client2.
WaitableEvent client1_msg_received(false, false);
WaitableEvent client1_can_reply(false, false);
Worker* worker;
worker = new MultipleServer2();
worker->OverrideThread(&worker_thread);
workers.push_back(worker);
worker = new MultipleClient2(
&client1_msg_received, &client1_can_reply, client_pump);
workers.push_back(worker);
worker = new MultipleServer1(server_pump);
worker->OverrideThread(&worker_thread);
workers.push_back(worker);
worker = new MultipleClient1(
&client1_msg_received, &client1_can_reply);
workers.push_back(worker);
RunTest(workers);
}
// Tests that multiple SyncObjects on the same listener thread can unblock each
// other.
TEST_F(IPCSyncChannelTest, Multiple) {
Multiple(false, false);
Multiple(false, true);
Multiple(true, false);
Multiple(true, true);
}
//------------------------------------------------------------------------------
// This class provides server side functionality to test the case where
// multiple sync channels are in use on the same thread on the client and
// nested calls are issued.
class QueuedReplyServer : public Worker {
public:
QueuedReplyServer(base::Thread* listener_thread,
const std::string& channel_name,
const std::string& reply_text)
: Worker(channel_name, Channel::MODE_SERVER),
reply_text_(reply_text) {
Worker::OverrideThread(listener_thread);
}
virtual void OnNestedTestMsg(Message* reply_msg) OVERRIDE {
VLOG(1) << __FUNCTION__ << " Sending reply: " << reply_text_;
SyncChannelNestedTestMsg_String::WriteReplyParams(reply_msg, reply_text_);
Send(reply_msg);
Done();
}
private:
std::string reply_text_;
};
// The QueuedReplyClient class provides functionality to test the case where
// multiple sync channels are in use on the same thread and they make nested
// sync calls, i.e. while the first channel waits for a response it makes a
// sync call on another channel.
// The callstack should unwind correctly, i.e. the outermost call should
// complete first, and so on.
class QueuedReplyClient : public Worker {
public:
QueuedReplyClient(base::Thread* listener_thread,
const std::string& channel_name,
const std::string& expected_text,
bool pump_during_send)
: Worker(channel_name, Channel::MODE_CLIENT),
pump_during_send_(pump_during_send),
expected_text_(expected_text) {
Worker::OverrideThread(listener_thread);
}
virtual void Run() OVERRIDE {
std::string response;
SyncMessage* msg = new SyncChannelNestedTestMsg_String(&response);
if (pump_during_send_)
msg->EnableMessagePumping();
bool result = Send(msg);
DCHECK(result);
DCHECK_EQ(response, expected_text_);
VLOG(1) << __FUNCTION__ << " Received reply: " << response;
Done();
}
private:
bool pump_during_send_;
std::string expected_text_;
};
void QueuedReply(bool client_pump) {
std::vector<Worker*> workers;
// A shared worker thread for servers
base::Thread server_worker_thread("QueuedReply_ServerListener");
ASSERT_TRUE(server_worker_thread.Start());
base::Thread client_worker_thread("QueuedReply_ClientListener");
ASSERT_TRUE(client_worker_thread.Start());
Worker* worker;
worker = new QueuedReplyServer(&server_worker_thread,
"QueuedReply_Server1",
"Got first message");
workers.push_back(worker);
worker = new QueuedReplyServer(&server_worker_thread,
"QueuedReply_Server2",
"Got second message");
workers.push_back(worker);
worker = new QueuedReplyClient(&client_worker_thread,
"QueuedReply_Server1",
"Got first message",
client_pump);
workers.push_back(worker);
worker = new QueuedReplyClient(&client_worker_thread,
"QueuedReply_Server2",
"Got second message",
client_pump);
workers.push_back(worker);
RunTest(workers);
}
// While a blocking send is in progress, the listener thread might answer other
// synchronous messages. This tests that if during the response to another
// message the reply to the original messages comes, it is queued up correctly
// and the original Send is unblocked later.
// We also test that the send call stacks unwind correctly when the channel
// pumps messages while waiting for a response.
TEST_F(IPCSyncChannelTest, QueuedReply) {
QueuedReply(false);
QueuedReply(true);
}
//------------------------------------------------------------------------------
class ChattyClient : public Worker {
public:
ChattyClient() :
Worker(Channel::MODE_CLIENT, "chatty_client") { }
virtual void OnAnswer(int* answer) OVERRIDE {
// The PostMessage limit is 10k. Send 20% more than that.
const int kMessageLimit = 10000;
const int kMessagesToSend = kMessageLimit * 120 / 100;
for (int i = 0; i < kMessagesToSend; ++i) {
if (!SendDouble(false, true))
break;
}
*answer = 42;
Done();
}
};
void ChattyServer(bool pump_during_send) {
std::vector<Worker*> workers;
workers.push_back(new UnblockServer(pump_during_send, false));
workers.push_back(new ChattyClient());
RunTest(workers);
}
// Tests http://b/1093251 - that sending lots of sync messages while
// the receiver is waiting for a sync reply does not overflow the PostMessage
// queue.
TEST_F(IPCSyncChannelTest, ChattyServer) {
ChattyServer(false);
ChattyServer(true);
}
//------------------------------------------------------------------------------
void NestedCallback(Worker* server) {
// Sleep a bit so that we wake up after the reply has been received.
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(250));
server->SendAnswerToLife(true, true);
}
bool timeout_occurred = false;
void TimeoutCallback() {
timeout_occurred = true;
}
class DoneEventRaceServer : public Worker {
public:
DoneEventRaceServer()
: Worker(Channel::MODE_SERVER, "done_event_race_server") { }
virtual void Run() OVERRIDE {
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(&NestedCallback, this));
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&TimeoutCallback),
base::TimeDelta::FromSeconds(9));
// Even though we have a timeout on the Send, it will succeed since for this
// bug, the reply message comes back and is deserialized, however the done
// event wasn't set. So we indirectly use the timeout task to notice if a
// timeout occurred.
SendAnswerToLife(true, true);
DCHECK(!timeout_occurred);
Done();
}
};
// Tests http://b/1474092 - that if after the done_event is set but before
// OnObjectSignaled is called another message is sent out, then after its
// reply comes back OnObjectSignaled will be called for the first message.
TEST_F(IPCSyncChannelTest, DoneEventRace) {
std::vector<Worker*> workers;
workers.push_back(new DoneEventRaceServer());
workers.push_back(new SimpleClient());
RunTest(workers);
}
//------------------------------------------------------------------------------
class TestSyncMessageFilter : public SyncMessageFilter {
public:
TestSyncMessageFilter(base::WaitableEvent* shutdown_event,
Worker* worker,
scoped_refptr<base::MessageLoopProxy> message_loop)
: SyncMessageFilter(shutdown_event),
worker_(worker),
message_loop_(message_loop) {
}
virtual void OnFilterAdded(Sender* sender) OVERRIDE {
SyncMessageFilter::OnFilterAdded(sender);
message_loop_->PostTask(
FROM_HERE,
base::Bind(&TestSyncMessageFilter::SendMessageOnHelperThread, this));
}
void SendMessageOnHelperThread() {
int answer = 0;
bool result = Send(new SyncChannelTestMsg_AnswerToLife(&answer));
DCHECK(result);
DCHECK_EQ(answer, 42);
worker_->Done();
}
private:
virtual ~TestSyncMessageFilter() {}
Worker* worker_;
scoped_refptr<base::MessageLoopProxy> message_loop_;
};
class SyncMessageFilterServer : public Worker {
public:
SyncMessageFilterServer()
: Worker(Channel::MODE_SERVER, "sync_message_filter_server"),
thread_("helper_thread") {
base::Thread::Options options;
options.message_loop_type = base::MessageLoop::TYPE_DEFAULT;
thread_.StartWithOptions(options);
filter_ = new TestSyncMessageFilter(shutdown_event(), this,
thread_.message_loop_proxy());
}
virtual void Run() OVERRIDE {
channel()->AddFilter(filter_.get());
}
base::Thread thread_;
scoped_refptr<TestSyncMessageFilter> filter_;
};
// This class provides functionality to test the case that a Send on the sync
// channel does not crash after the channel has been closed.
class ServerSendAfterClose : public Worker {
public:
ServerSendAfterClose()
: Worker(Channel::MODE_SERVER, "simpler_server"),
send_result_(true) {
}
bool SendDummy() {
ListenerThread()->message_loop()->PostTask(
FROM_HERE, base::Bind(base::IgnoreResult(&ServerSendAfterClose::Send),
this, new SyncChannelTestMsg_NoArgs));
return true;
}
bool send_result() const {
return send_result_;
}
private: