Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1024)

Side by Side Diff: mojo/edk/system/data_pipe_impl_unittest.cc

Issue 1412283002: Convert mojo::system::Dispatcher to use our new refcounting stuff (instead of base's). (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: no change Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file contains tests that are shared between different implementations of 5 // This file contains tests that are shared between different implementations of
6 // |DataPipeImpl|. 6 // |DataPipeImpl|.
7 7
8 #include "mojo/edk/system/data_pipe_impl.h" 8 #include "mojo/edk/system/data_pipe_impl.h"
9 9
10 #include <stdint.h> 10 #include <stdint.h>
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 248
249 void Create(const MojoCreateDataPipeOptions& validated_options) override { 249 void Create(const MojoCreateDataPipeOptions& validated_options) override {
250 CHECK(!dp_); 250 CHECK(!dp_);
251 dp_ = DataPipe::CreateLocal(validated_options); 251 dp_ = DataPipe::CreateLocal(validated_options);
252 } 252 }
253 253
254 bool IsStrictCircularBuffer() const override { return false; } 254 bool IsStrictCircularBuffer() const override { return false; }
255 255
256 protected: 256 protected:
257 void SendDispatcher(size_t source_i, 257 void SendDispatcher(size_t source_i,
258 scoped_refptr<Dispatcher> to_send, 258 RefPtr<Dispatcher> to_send,
259 scoped_refptr<Dispatcher>* to_receive) { 259 RefPtr<Dispatcher>* to_receive) {
260 DCHECK(source_i == 0 || source_i == 1); 260 DCHECK(source_i == 0 || source_i == 1);
261 size_t dest_i = source_i ^ 1; 261 size_t dest_i = source_i ^ 1;
262 262
263 // Write the dispatcher to MP |source_i| (port 0). Wait and receive on MP 263 // Write the dispatcher to MP |source_i| (port 0). Wait and receive on MP
264 // |dest_i| (port 0). (Add the waiter first, to avoid any handling the case 264 // |dest_i| (port 0). (Add the waiter first, to avoid any handling the case
265 // where it's already readable.) 265 // where it's already readable.)
266 Waiter waiter; 266 Waiter waiter;
267 waiter.Init(); 267 waiter.Init();
268 ASSERT_EQ(MOJO_RESULT_OK, 268 ASSERT_EQ(MOJO_RESULT_OK,
269 message_pipe(dest_i)->AddAwakable( 269 message_pipe(dest_i)->AddAwakable(
(...skipping 24 matching lines...) Expand all
294 uint32_t read_num_dispatchers = 10; // Maximum to get. 294 uint32_t read_num_dispatchers = 10; // Maximum to get.
295 ASSERT_EQ(MOJO_RESULT_OK, 295 ASSERT_EQ(MOJO_RESULT_OK,
296 message_pipe(dest_i)->ReadMessage( 296 message_pipe(dest_i)->ReadMessage(
297 0, UserPointer<void>(read_buffer), 297 0, UserPointer<void>(read_buffer),
298 MakeUserPointer(&read_buffer_size), &read_dispatchers, 298 MakeUserPointer(&read_buffer_size), &read_dispatchers,
299 &read_num_dispatchers, MOJO_READ_MESSAGE_FLAG_NONE)); 299 &read_num_dispatchers, MOJO_READ_MESSAGE_FLAG_NONE));
300 EXPECT_EQ(0u, static_cast<size_t>(read_buffer_size)); 300 EXPECT_EQ(0u, static_cast<size_t>(read_buffer_size));
301 ASSERT_EQ(1u, read_dispatchers.size()); 301 ASSERT_EQ(1u, read_dispatchers.size());
302 ASSERT_EQ(1u, read_num_dispatchers); 302 ASSERT_EQ(1u, read_num_dispatchers);
303 ASSERT_TRUE(read_dispatchers[0]); 303 ASSERT_TRUE(read_dispatchers[0]);
304 EXPECT_TRUE(read_dispatchers[0]->HasOneRef()); 304 read_dispatchers[0]->AssertHasOneRef();
305 305
306 *to_receive = read_dispatchers[0]; 306 *to_receive = read_dispatchers[0];
307 } 307 }
308 308
309 RefPtr<MessagePipe> message_pipe(size_t i) { return message_pipes_[i]; } 309 RefPtr<MessagePipe> message_pipe(size_t i) { return message_pipes_[i]; }
310 RefPtr<DataPipe> dp() { return dp_; } 310 RefPtr<DataPipe> dp() { return dp_; }
311 311
312 private: 312 private:
313 void EnsureMessagePipeClosed(size_t i) { 313 void EnsureMessagePipeClosed(size_t i) {
314 if (!message_pipes_[i]) 314 if (!message_pipes_[i])
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 // i.e., |dp_| will have a |RemoteProducerDataPipeImpl|. The remote side, of 361 // i.e., |dp_| will have a |RemoteProducerDataPipeImpl|. The remote side, of
362 // course, will have a |RemoteConsumerDataPipeImpl|. 362 // course, will have a |RemoteConsumerDataPipeImpl|.
363 class RemoteProducerDataPipeImplTestHelper 363 class RemoteProducerDataPipeImplTestHelper
364 : public RemoteDataPipeImplTestHelper { 364 : public RemoteDataPipeImplTestHelper {
365 public: 365 public:
366 RemoteProducerDataPipeImplTestHelper() {} 366 RemoteProducerDataPipeImplTestHelper() {}
367 ~RemoteProducerDataPipeImplTestHelper() override {} 367 ~RemoteProducerDataPipeImplTestHelper() override {}
368 368
369 void DoTransfer() override { 369 void DoTransfer() override {
370 // This is the producer dispatcher we'll send. 370 // This is the producer dispatcher we'll send.
371 scoped_refptr<DataPipeProducerDispatcher> to_send = 371 auto to_send = DataPipeProducerDispatcher::Create();
372 DataPipeProducerDispatcher::Create();
373 to_send->Init(dp()); 372 to_send->Init(dp());
374 scoped_refptr<Dispatcher> to_receive; 373 RefPtr<Dispatcher> to_receive;
375 SendDispatcher(0, to_send, &to_receive); 374 SendDispatcher(0, to_send, &to_receive);
376 // |to_send| should have been closed. This is |DCHECK()|ed when it is 375 // |to_send| should have been closed. This is |DCHECK()|ed when it is
377 // destroyed. 376 // destroyed.
378 EXPECT_TRUE(to_send->HasOneRef()); 377 to_send->AssertHasOneRef();
379 to_send = nullptr; 378 to_send = nullptr;
380 379
381 ASSERT_EQ(Dispatcher::Type::DATA_PIPE_PRODUCER, to_receive->GetType()); 380 ASSERT_EQ(Dispatcher::Type::DATA_PIPE_PRODUCER, to_receive->GetType());
382 producer_dispatcher_ = 381 producer_dispatcher_ = RefPtr<DataPipeProducerDispatcher>(
383 static_cast<DataPipeProducerDispatcher*>(to_receive.get()); 382 static_cast<DataPipeProducerDispatcher*>(to_receive.get()));
384 } 383 }
385 384
386 DataPipe* DataPipeForProducer() override { 385 DataPipe* DataPipeForProducer() override {
387 if (producer_dispatcher_) 386 if (producer_dispatcher_)
388 return producer_dispatcher_->GetDataPipeForTest(); 387 return producer_dispatcher_->GetDataPipeForTest();
389 return dp().get(); 388 return dp().get();
390 } 389 }
391 DataPipe* DataPipeForConsumer() override { return dp().get(); } 390 DataPipe* DataPipeForConsumer() override { return dp().get(); }
392 391
393 void ProducerClose() override { 392 void ProducerClose() override {
394 if (producer_dispatcher_) 393 if (producer_dispatcher_)
395 ASSERT_EQ(MOJO_RESULT_OK, producer_dispatcher_->Close()); 394 ASSERT_EQ(MOJO_RESULT_OK, producer_dispatcher_->Close());
396 else 395 else
397 dp()->ProducerClose(); 396 dp()->ProducerClose();
398 } 397 }
399 void ConsumerClose() override { dp()->ConsumerClose(); } 398 void ConsumerClose() override { dp()->ConsumerClose(); }
400 399
401 protected: 400 protected:
402 scoped_refptr<DataPipeProducerDispatcher> producer_dispatcher_; 401 RefPtr<DataPipeProducerDispatcher> producer_dispatcher_;
403 402
404 private: 403 private:
405 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoteProducerDataPipeImplTestHelper); 404 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoteProducerDataPipeImplTestHelper);
406 }; 405 };
407 406
408 // RemoteConsumerDataPipeImplTestHelper ---------------------------------------- 407 // RemoteConsumerDataPipeImplTestHelper ----------------------------------------
409 408
410 // Note about naming confusion: This class is named after the "local" class, 409 // Note about naming confusion: This class is named after the "local" class,
411 // i.e., |dp_| will have a |RemoteConsumerDataPipeImpl|. The remote side, of 410 // i.e., |dp_| will have a |RemoteConsumerDataPipeImpl|. The remote side, of
412 // course, will have a |RemoteProducerDataPipeImpl|. 411 // course, will have a |RemoteProducerDataPipeImpl|.
413 class RemoteConsumerDataPipeImplTestHelper 412 class RemoteConsumerDataPipeImplTestHelper
414 : public RemoteDataPipeImplTestHelper { 413 : public RemoteDataPipeImplTestHelper {
415 public: 414 public:
416 RemoteConsumerDataPipeImplTestHelper() {} 415 RemoteConsumerDataPipeImplTestHelper() {}
417 ~RemoteConsumerDataPipeImplTestHelper() override {} 416 ~RemoteConsumerDataPipeImplTestHelper() override {}
418 417
419 void DoTransfer() override { 418 void DoTransfer() override {
420 // This is the consumer dispatcher we'll send. 419 // This is the consumer dispatcher we'll send.
421 scoped_refptr<DataPipeConsumerDispatcher> to_send = 420 auto to_send = DataPipeConsumerDispatcher::Create();
422 DataPipeConsumerDispatcher::Create();
423 to_send->Init(dp()); 421 to_send->Init(dp());
424 scoped_refptr<Dispatcher> to_receive; 422 RefPtr<Dispatcher> to_receive;
425 SendDispatcher(0, to_send, &to_receive); 423 SendDispatcher(0, to_send, &to_receive);
426 // |to_send| should have been closed. This is |DCHECK()|ed when it is 424 // |to_send| should have been closed. This is |DCHECK()|ed when it is
427 // destroyed. 425 // destroyed.
428 EXPECT_TRUE(to_send->HasOneRef()); 426 to_send->AssertHasOneRef();
429 to_send = nullptr; 427 to_send = nullptr;
430 428
431 ASSERT_EQ(Dispatcher::Type::DATA_PIPE_CONSUMER, to_receive->GetType()); 429 ASSERT_EQ(Dispatcher::Type::DATA_PIPE_CONSUMER, to_receive->GetType());
432 consumer_dispatcher_ = 430 consumer_dispatcher_ = RefPtr<DataPipeConsumerDispatcher>(
433 static_cast<DataPipeConsumerDispatcher*>(to_receive.get()); 431 static_cast<DataPipeConsumerDispatcher*>(to_receive.get()));
434 } 432 }
435 433
436 DataPipe* DataPipeForProducer() override { return dp().get(); } 434 DataPipe* DataPipeForProducer() override { return dp().get(); }
437 DataPipe* DataPipeForConsumer() override { 435 DataPipe* DataPipeForConsumer() override {
438 if (consumer_dispatcher_) 436 if (consumer_dispatcher_)
439 return consumer_dispatcher_->GetDataPipeForTest(); 437 return consumer_dispatcher_->GetDataPipeForTest();
440 return dp().get(); 438 return dp().get();
441 } 439 }
442 440
443 void ProducerClose() override { dp()->ProducerClose(); } 441 void ProducerClose() override { dp()->ProducerClose(); }
444 void ConsumerClose() override { 442 void ConsumerClose() override {
445 if (consumer_dispatcher_) 443 if (consumer_dispatcher_)
446 ASSERT_EQ(MOJO_RESULT_OK, consumer_dispatcher_->Close()); 444 ASSERT_EQ(MOJO_RESULT_OK, consumer_dispatcher_->Close());
447 else 445 else
448 dp()->ConsumerClose(); 446 dp()->ConsumerClose();
449 } 447 }
450 448
451 protected: 449 protected:
452 scoped_refptr<DataPipeConsumerDispatcher> consumer_dispatcher_; 450 RefPtr<DataPipeConsumerDispatcher> consumer_dispatcher_;
453 451
454 private: 452 private:
455 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoteConsumerDataPipeImplTestHelper); 453 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoteConsumerDataPipeImplTestHelper);
456 }; 454 };
457 455
458 // RemoteProducerDataPipeImplTestHelper2 --------------------------------------- 456 // RemoteProducerDataPipeImplTestHelper2 ---------------------------------------
459 457
460 // This is like |RemoteProducerDataPipeImplTestHelper|, but |DoTransfer()| does 458 // This is like |RemoteProducerDataPipeImplTestHelper|, but |DoTransfer()| does
461 // a second transfer. This thus tests passing a producer handle twice, and in 459 // a second transfer. This thus tests passing a producer handle twice, and in
462 // particular tests (some of) |RemoteConsumerDataPipeImpl|'s 460 // particular tests (some of) |RemoteConsumerDataPipeImpl|'s
463 // |ProducerEndSerialize()| (instead of |LocalDataPipeImpl|'s). 461 // |ProducerEndSerialize()| (instead of |LocalDataPipeImpl|'s).
464 // 462 //
465 // Note about naming confusion: This class is named after the "local" class, 463 // Note about naming confusion: This class is named after the "local" class,
466 // i.e., |dp_| will have a |RemoteProducerDataPipeImpl|. The remote side, of 464 // i.e., |dp_| will have a |RemoteProducerDataPipeImpl|. The remote side, of
467 // course, will have a |RemoteConsumerDataPipeImpl|. 465 // course, will have a |RemoteConsumerDataPipeImpl|.
468 class RemoteProducerDataPipeImplTestHelper2 466 class RemoteProducerDataPipeImplTestHelper2
469 : public RemoteProducerDataPipeImplTestHelper { 467 : public RemoteProducerDataPipeImplTestHelper {
470 public: 468 public:
471 RemoteProducerDataPipeImplTestHelper2() {} 469 RemoteProducerDataPipeImplTestHelper2() {}
472 ~RemoteProducerDataPipeImplTestHelper2() override {} 470 ~RemoteProducerDataPipeImplTestHelper2() override {}
473 471
474 void DoTransfer() override { 472 void DoTransfer() override {
475 // This is the producer dispatcher we'll send. 473 // This is the producer dispatcher we'll send.
476 scoped_refptr<DataPipeProducerDispatcher> to_send = 474 auto to_send = DataPipeProducerDispatcher::Create();
477 DataPipeProducerDispatcher::Create();
478 to_send->Init(dp()); 475 to_send->Init(dp());
479 scoped_refptr<Dispatcher> to_receive; 476 RefPtr<Dispatcher> to_receive;
480 SendDispatcher(0, to_send, &to_receive); 477 SendDispatcher(0, to_send, &to_receive);
481 // |to_send| should have been closed. This is |DCHECK()|ed when it is 478 // |to_send| should have been closed. This is |DCHECK()|ed when it is
482 // destroyed. 479 // destroyed.
483 EXPECT_TRUE(to_send->HasOneRef()); 480 to_send->AssertHasOneRef();
484 to_send = nullptr; 481 to_send = nullptr;
485 ASSERT_EQ(Dispatcher::Type::DATA_PIPE_PRODUCER, to_receive->GetType()); 482 ASSERT_EQ(Dispatcher::Type::DATA_PIPE_PRODUCER, to_receive->GetType());
486 to_send = static_cast<DataPipeProducerDispatcher*>(to_receive.get()); 483 to_send = RefPtr<DataPipeProducerDispatcher>(
484 static_cast<DataPipeProducerDispatcher*>(to_receive.get()));
487 to_receive = nullptr; 485 to_receive = nullptr;
488 486
489 // Now send it back the other way. 487 // Now send it back the other way.
490 SendDispatcher(1, to_send, &to_receive); 488 SendDispatcher(1, to_send, &to_receive);
491 // |producer_dispatcher_| should have been closed. This is |DCHECK()|ed when 489 // |producer_dispatcher_| should have been closed. This is |DCHECK()|ed when
492 // it is destroyed. 490 // it is destroyed.
493 EXPECT_TRUE(to_send->HasOneRef()); 491 to_send->AssertHasOneRef();
494 to_send = nullptr; 492 to_send = nullptr;
495 493
496 ASSERT_EQ(Dispatcher::Type::DATA_PIPE_PRODUCER, to_receive->GetType()); 494 ASSERT_EQ(Dispatcher::Type::DATA_PIPE_PRODUCER, to_receive->GetType());
497 producer_dispatcher_ = 495 producer_dispatcher_ = RefPtr<DataPipeProducerDispatcher>(
498 static_cast<DataPipeProducerDispatcher*>(to_receive.get()); 496 static_cast<DataPipeProducerDispatcher*>(to_receive.get()));
499 } 497 }
500 498
501 private: 499 private:
502 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoteProducerDataPipeImplTestHelper2); 500 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoteProducerDataPipeImplTestHelper2);
503 }; 501 };
504 502
505 // RemoteConsumerDataPipeImplTestHelper2 --------------------------------------- 503 // RemoteConsumerDataPipeImplTestHelper2 ---------------------------------------
506 504
507 // This is like |RemoteConsumerDataPipeImplTestHelper|, but |DoTransfer()| does 505 // This is like |RemoteConsumerDataPipeImplTestHelper|, but |DoTransfer()| does
508 // a second transfer. This thus tests passing a consumer handle twice, and in 506 // a second transfer. This thus tests passing a consumer handle twice, and in
509 // particular tests (some of) |RemoteProducerDataPipeImpl|'s 507 // particular tests (some of) |RemoteProducerDataPipeImpl|'s
510 // |ConsumerEndSerialize()| (instead of |LocalDataPipeImpl|'s). 508 // |ConsumerEndSerialize()| (instead of |LocalDataPipeImpl|'s).
511 // 509 //
512 // Note about naming confusion: This class is named after the "local" class, 510 // Note about naming confusion: This class is named after the "local" class,
513 // i.e., |dp_| will have a |RemoteConsumerDataPipeImpl|. The remote side, of 511 // i.e., |dp_| will have a |RemoteConsumerDataPipeImpl|. The remote side, of
514 // course, will have a |RemoteProducerDataPipeImpl|. 512 // course, will have a |RemoteProducerDataPipeImpl|.
515 class RemoteConsumerDataPipeImplTestHelper2 513 class RemoteConsumerDataPipeImplTestHelper2
516 : public RemoteConsumerDataPipeImplTestHelper { 514 : public RemoteConsumerDataPipeImplTestHelper {
517 public: 515 public:
518 RemoteConsumerDataPipeImplTestHelper2() {} 516 RemoteConsumerDataPipeImplTestHelper2() {}
519 ~RemoteConsumerDataPipeImplTestHelper2() override {} 517 ~RemoteConsumerDataPipeImplTestHelper2() override {}
520 518
521 void DoTransfer() override { 519 void DoTransfer() override {
522 // This is the consumer dispatcher we'll send. 520 // This is the consumer dispatcher we'll send.
523 scoped_refptr<DataPipeConsumerDispatcher> to_send = 521 auto to_send = DataPipeConsumerDispatcher::Create();
524 DataPipeConsumerDispatcher::Create();
525 to_send->Init(dp()); 522 to_send->Init(dp());
526 scoped_refptr<Dispatcher> to_receive; 523 RefPtr<Dispatcher> to_receive;
527 SendDispatcher(0, to_send, &to_receive); 524 SendDispatcher(0, to_send, &to_receive);
528 // |to_send| should have been closed. This is |DCHECK()|ed when it is 525 // |to_send| should have been closed. This is |DCHECK()|ed when it is
529 // destroyed. 526 // destroyed.
530 EXPECT_TRUE(to_send->HasOneRef()); 527 to_send->AssertHasOneRef();
531 to_send = nullptr; 528 to_send = nullptr;
532 ASSERT_EQ(Dispatcher::Type::DATA_PIPE_CONSUMER, to_receive->GetType()); 529 ASSERT_EQ(Dispatcher::Type::DATA_PIPE_CONSUMER, to_receive->GetType());
533 to_send = static_cast<DataPipeConsumerDispatcher*>(to_receive.get()); 530 to_send = RefPtr<DataPipeConsumerDispatcher>(
531 static_cast<DataPipeConsumerDispatcher*>(to_receive.get()));
534 to_receive = nullptr; 532 to_receive = nullptr;
535 533
536 // Now send it back the other way. 534 // Now send it back the other way.
537 SendDispatcher(1, to_send, &to_receive); 535 SendDispatcher(1, to_send, &to_receive);
538 // |consumer_dispatcher_| should have been closed. This is |DCHECK()|ed when 536 // |consumer_dispatcher_| should have been closed. This is |DCHECK()|ed when
539 // it is destroyed. 537 // it is destroyed.
540 EXPECT_TRUE(to_send->HasOneRef()); 538 to_send->AssertHasOneRef();
541 to_send = nullptr; 539 to_send = nullptr;
542 540
543 ASSERT_EQ(Dispatcher::Type::DATA_PIPE_CONSUMER, to_receive->GetType()); 541 ASSERT_EQ(Dispatcher::Type::DATA_PIPE_CONSUMER, to_receive->GetType());
544 consumer_dispatcher_ = 542 consumer_dispatcher_ = RefPtr<DataPipeConsumerDispatcher>(
545 static_cast<DataPipeConsumerDispatcher*>(to_receive.get()); 543 static_cast<DataPipeConsumerDispatcher*>(to_receive.get()));
546 } 544 }
547 545
548 private: 546 private:
549 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoteConsumerDataPipeImplTestHelper2); 547 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoteConsumerDataPipeImplTestHelper2);
550 }; 548 };
551 549
552 // Test case instantiation ----------------------------------------------------- 550 // Test case instantiation -----------------------------------------------------
553 551
554 using HelperTypes = testing::Types<LocalDataPipeImplTestHelper, 552 using HelperTypes = testing::Types<LocalDataPipeImplTestHelper,
555 RemoteProducerDataPipeImplTestHelper, 553 RemoteProducerDataPipeImplTestHelper,
(...skipping 1939 matching lines...) Expand 10 before | Expand all | Expand 10 after
2495 test::Sleep(10u); 2493 test::Sleep(10u);
2496 2494
2497 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(num_bytes)); 2495 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(num_bytes));
2498 2496
2499 this->ConsumerClose(); 2497 this->ConsumerClose();
2500 } 2498 }
2501 2499
2502 } // namespace 2500 } // namespace
2503 } // namespace system 2501 } // namespace system
2504 } // namespace mojo 2502 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/data_pipe_consumer_dispatcher.cc ('k') | mojo/edk/system/data_pipe_producer_dispatcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698