| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // TODO(vtl): Factor out the remaining POSIX-specific bits of this test (once we | 5 // TODO(vtl): Factor out the remaining POSIX-specific bits of this test (once we |
| 6 // have a non-POSIX implementation). | 6 // have a non-POSIX implementation). |
| 7 | 7 |
| 8 #include "mojo/system/raw_channel.h" | 8 #include "mojo/system/raw_channel.h" |
| 9 | 9 |
| 10 #include <fcntl.h> | 10 #include <fcntl.h> |
| 11 #include <stdint.h> | 11 #include <stdint.h> |
| 12 #include <unistd.h> | 12 #include <unistd.h> |
| 13 | 13 |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 17 #include "base/bind.h" | 17 #include "base/bind.h" |
| 18 #include "base/callback.h" | |
| 19 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
| 20 #include "base/location.h" | 19 #include "base/location.h" |
| 21 #include "base/logging.h" | 20 #include "base/logging.h" |
| 22 #include "base/memory/scoped_ptr.h" | 21 #include "base/memory/scoped_ptr.h" |
| 23 #include "base/memory/scoped_vector.h" | 22 #include "base/memory/scoped_vector.h" |
| 24 #include "base/message_loop/message_loop.h" | 23 #include "base/message_loop/message_loop.h" |
| 25 #include "base/posix/eintr_wrapper.h" | 24 #include "base/posix/eintr_wrapper.h" |
| 26 #include "base/rand_util.h" | 25 #include "base/rand_util.h" |
| 27 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
| 28 #include "base/synchronization/waitable_event.h" | 27 #include "base/synchronization/waitable_event.h" |
| 29 #include "base/threading/platform_thread.h" // For |Sleep()|. | 28 #include "base/threading/platform_thread.h" // For |Sleep()|. |
| 30 #include "base/threading/simple_thread.h" | 29 #include "base/threading/simple_thread.h" |
| 31 #include "base/threading/thread.h" | |
| 32 #include "base/time/time.h" | 30 #include "base/time/time.h" |
| 33 #include "mojo/system/message_in_transit.h" | 31 #include "mojo/system/message_in_transit.h" |
| 34 #include "mojo/system/platform_channel_pair.h" | 32 #include "mojo/system/platform_channel_pair.h" |
| 35 #include "mojo/system/platform_handle.h" | 33 #include "mojo/system/platform_handle.h" |
| 36 #include "mojo/system/scoped_platform_handle.h" | 34 #include "mojo/system/scoped_platform_handle.h" |
| 37 #include "mojo/system/test_utils.h" | 35 #include "mojo/system/test_utils.h" |
| 38 #include "testing/gtest/include/gtest/gtest.h" | |
| 39 | 36 |
| 40 namespace mojo { | 37 namespace mojo { |
| 41 namespace system { | 38 namespace system { |
| 42 namespace { | 39 namespace { |
| 43 | 40 |
| 44 MessageInTransit* MakeTestMessage(uint32_t num_bytes) { | 41 MessageInTransit* MakeTestMessage(uint32_t num_bytes) { |
| 45 std::vector<unsigned char> bytes(num_bytes, 0); | 42 std::vector<unsigned char> bytes(num_bytes, 0); |
| 46 for (size_t i = 0; i < num_bytes; i++) | 43 for (size_t i = 0; i < num_bytes; i++) |
| 47 bytes[i] = static_cast<unsigned char>(i + num_bytes); | 44 bytes[i] = static_cast<unsigned char>(i + num_bytes); |
| 48 return MessageInTransit::Create( | 45 return MessageInTransit::Create( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 59 } | 56 } |
| 60 return true; | 57 return true; |
| 61 } | 58 } |
| 62 | 59 |
| 63 void InitOnIOThread(RawChannel* raw_channel) { | 60 void InitOnIOThread(RawChannel* raw_channel) { |
| 64 CHECK(raw_channel->Init()); | 61 CHECK(raw_channel->Init()); |
| 65 } | 62 } |
| 66 | 63 |
| 67 // ----------------------------------------------------------------------------- | 64 // ----------------------------------------------------------------------------- |
| 68 | 65 |
| 69 class RawChannelPosixTest : public testing::Test { | 66 class RawChannelPosixTest : public test::TestWithIOThreadBase { |
| 70 public: | 67 public: |
| 71 RawChannelPosixTest() : io_thread_("io_thread") { | 68 RawChannelPosixTest() {} |
| 72 } | 69 virtual ~RawChannelPosixTest() {} |
| 73 | |
| 74 virtual ~RawChannelPosixTest() { | |
| 75 } | |
| 76 | 70 |
| 77 virtual void SetUp() OVERRIDE { | 71 virtual void SetUp() OVERRIDE { |
| 78 io_thread_.StartWithOptions( | 72 test::TestWithIOThreadBase::SetUp(); |
| 79 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | |
| 80 | 73 |
| 81 PlatformChannelPair channel_pair; | 74 PlatformChannelPair channel_pair; |
| 82 handles[0] = channel_pair.PassServerHandle(); | 75 handles[0] = channel_pair.PassServerHandle(); |
| 83 handles[1] = channel_pair.PassClientHandle(); | 76 handles[1] = channel_pair.PassClientHandle(); |
| 84 } | 77 } |
| 85 | 78 |
| 86 virtual void TearDown() OVERRIDE { | 79 virtual void TearDown() OVERRIDE { |
| 87 handles[0].reset(); | 80 handles[0].reset(); |
| 88 handles[1].reset(); | 81 handles[1].reset(); |
| 89 | 82 |
| 90 io_thread_.Stop(); | 83 test::TestWithIOThreadBase::TearDown(); |
| 91 } | 84 } |
| 92 | 85 |
| 93 protected: | 86 protected: |
| 94 base::MessageLoop* io_thread_message_loop() { | |
| 95 return io_thread_.message_loop(); | |
| 96 } | |
| 97 | |
| 98 scoped_refptr<base::TaskRunner> io_thread_task_runner() { | |
| 99 return io_thread_message_loop()->message_loop_proxy(); | |
| 100 } | |
| 101 | |
| 102 ScopedPlatformHandle handles[2]; | 87 ScopedPlatformHandle handles[2]; |
| 103 | 88 |
| 104 private: | 89 private: |
| 105 base::Thread io_thread_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(RawChannelPosixTest); | 90 DISALLOW_COPY_AND_ASSIGN(RawChannelPosixTest); |
| 108 }; | 91 }; |
| 109 | 92 |
| 110 // RawChannelPosixTest.WriteMessage -------------------------------------------- | 93 // RawChannelPosixTest.WriteMessage -------------------------------------------- |
| 111 | 94 |
| 112 class WriteOnlyRawChannelDelegate : public RawChannel::Delegate { | 95 class WriteOnlyRawChannelDelegate : public RawChannel::Delegate { |
| 113 public: | 96 public: |
| 114 WriteOnlyRawChannelDelegate() {} | 97 WriteOnlyRawChannelDelegate() {} |
| 115 virtual ~WriteOnlyRawChannelDelegate() {} | 98 virtual ~WriteOnlyRawChannelDelegate() {} |
| 116 | 99 |
| (...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 | 495 |
| 513 // TODO(vtl): In theory, it's conceivable that closing the other end might | 496 // TODO(vtl): In theory, it's conceivable that closing the other end might |
| 514 // lead to read failing. In practice, it doesn't seem to. | 497 // lead to read failing. In practice, it doesn't seem to. |
| 515 EXPECT_EQ(RawChannel::Delegate::FATAL_ERROR_FAILED_WRITE, | 498 EXPECT_EQ(RawChannel::Delegate::FATAL_ERROR_FAILED_WRITE, |
| 516 delegate.WaitForFatalError()); | 499 delegate.WaitForFatalError()); |
| 517 | 500 |
| 518 test::PostTaskAndWait(io_thread_task_runner(), | 501 test::PostTaskAndWait(io_thread_task_runner(), |
| 519 FROM_HERE, | 502 FROM_HERE, |
| 520 base::Bind(&RawChannel::Shutdown, | 503 base::Bind(&RawChannel::Shutdown, |
| 521 base::Unretained(rc.get()))); | 504 base::Unretained(rc.get()))); |
| 522 | |
| 523 } | 505 } |
| 524 | 506 |
| 525 // RawChannelPosixTest.WriteMessageAfterShutdown ------------------------------- | 507 // RawChannelPosixTest.WriteMessageAfterShutdown ------------------------------- |
| 526 | 508 |
| 527 // Makes sure that calling |WriteMessage()| after |Shutdown()| behaves | 509 // Makes sure that calling |WriteMessage()| after |Shutdown()| behaves |
| 528 // correctly. | 510 // correctly. |
| 529 TEST_F(RawChannelPosixTest, WriteMessageAfterShutdown) { | 511 TEST_F(RawChannelPosixTest, WriteMessageAfterShutdown) { |
| 530 WriteOnlyRawChannelDelegate delegate; | 512 WriteOnlyRawChannelDelegate delegate; |
| 531 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass(), | 513 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass(), |
| 532 &delegate, | 514 &delegate, |
| 533 io_thread_message_loop())); | 515 io_thread_message_loop())); |
| 534 | 516 |
| 535 test::PostTaskAndWait(io_thread_task_runner(), | 517 test::PostTaskAndWait(io_thread_task_runner(), |
| 536 FROM_HERE, | 518 FROM_HERE, |
| 537 base::Bind(&InitOnIOThread, rc.get())); | 519 base::Bind(&InitOnIOThread, rc.get())); |
| 538 test::PostTaskAndWait(io_thread_task_runner(), | 520 test::PostTaskAndWait(io_thread_task_runner(), |
| 539 FROM_HERE, | 521 FROM_HERE, |
| 540 base::Bind(&RawChannel::Shutdown, | 522 base::Bind(&RawChannel::Shutdown, |
| 541 base::Unretained(rc.get()))); | 523 base::Unretained(rc.get()))); |
| 542 | 524 |
| 543 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); | 525 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); |
| 544 } | 526 } |
| 545 | 527 |
| 546 } // namespace | 528 } // namespace |
| 547 } // namespace system | 529 } // namespace system |
| 548 } // namespace mojo | 530 } // namespace mojo |
| OLD | NEW |