OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "mojo/edk/system/raw_channel.h" | 5 #include "mojo/edk/system/raw_channel.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 namespace system { | 34 namespace system { |
35 namespace { | 35 namespace { |
36 | 36 |
37 scoped_ptr<MessageInTransit> MakeTestMessage(uint32_t num_bytes) { | 37 scoped_ptr<MessageInTransit> MakeTestMessage(uint32_t num_bytes) { |
38 std::vector<unsigned char> bytes(num_bytes, 0); | 38 std::vector<unsigned char> bytes(num_bytes, 0); |
39 for (size_t i = 0; i < num_bytes; i++) | 39 for (size_t i = 0; i < num_bytes; i++) |
40 bytes[i] = static_cast<unsigned char>(i + num_bytes); | 40 bytes[i] = static_cast<unsigned char>(i + num_bytes); |
41 return make_scoped_ptr( | 41 return make_scoped_ptr( |
42 new MessageInTransit(MessageInTransit::kTypeMessagePipeEndpoint, | 42 new MessageInTransit(MessageInTransit::kTypeMessagePipeEndpoint, |
43 MessageInTransit::kSubtypeMessagePipeEndpointData, | 43 MessageInTransit::kSubtypeMessagePipeEndpointData, |
44 num_bytes, | 44 num_bytes, bytes.empty() ? nullptr : &bytes[0])); |
45 bytes.empty() ? nullptr : &bytes[0])); | |
46 } | 45 } |
47 | 46 |
48 bool CheckMessageData(const void* bytes, uint32_t num_bytes) { | 47 bool CheckMessageData(const void* bytes, uint32_t num_bytes) { |
49 const unsigned char* b = static_cast<const unsigned char*>(bytes); | 48 const unsigned char* b = static_cast<const unsigned char*>(bytes); |
50 for (uint32_t i = 0; i < num_bytes; i++) { | 49 for (uint32_t i = 0; i < num_bytes; i++) { |
51 if (b[i] != static_cast<unsigned char>(i + num_bytes)) | 50 if (b[i] != static_cast<unsigned char>(i + num_bytes)) |
52 return false; | 51 return false; |
53 } | 52 } |
54 return true; | 53 return true; |
55 } | 54 } |
56 | 55 |
57 void InitOnIOThread(RawChannel* raw_channel, RawChannel::Delegate* delegate) { | 56 void InitOnIOThread(RawChannel* raw_channel, RawChannel::Delegate* delegate) { |
58 CHECK(raw_channel->Init(delegate)); | 57 CHECK(raw_channel->Init(delegate)); |
59 } | 58 } |
60 | 59 |
61 bool WriteTestMessageToHandle(const embedder::PlatformHandle& handle, | 60 bool WriteTestMessageToHandle(const embedder::PlatformHandle& handle, |
62 uint32_t num_bytes) { | 61 uint32_t num_bytes) { |
63 scoped_ptr<MessageInTransit> message(MakeTestMessage(num_bytes)); | 62 scoped_ptr<MessageInTransit> message(MakeTestMessage(num_bytes)); |
64 | 63 |
65 size_t write_size = 0; | 64 size_t write_size = 0; |
66 mojo::test::BlockingWrite( | 65 mojo::test::BlockingWrite(handle, message->main_buffer(), |
67 handle, message->main_buffer(), message->main_buffer_size(), &write_size); | 66 message->main_buffer_size(), &write_size); |
68 return write_size == message->main_buffer_size(); | 67 return write_size == message->main_buffer_size(); |
69 } | 68 } |
70 | 69 |
71 // ----------------------------------------------------------------------------- | 70 // ----------------------------------------------------------------------------- |
72 | 71 |
73 class RawChannelTest : public testing::Test { | 72 class RawChannelTest : public testing::Test { |
74 public: | 73 public: |
75 RawChannelTest() : io_thread_(base::TestIOThread::kManualStart) {} | 74 RawChannelTest() : io_thread_(base::TestIOThread::kManualStart) {} |
76 ~RawChannelTest() override {} | 75 ~RawChannelTest() override {} |
77 | 76 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 public: | 127 public: |
129 explicit TestMessageReaderAndChecker(embedder::PlatformHandle handle) | 128 explicit TestMessageReaderAndChecker(embedder::PlatformHandle handle) |
130 : handle_(handle) {} | 129 : handle_(handle) {} |
131 ~TestMessageReaderAndChecker() { CHECK(bytes_.empty()); } | 130 ~TestMessageReaderAndChecker() { CHECK(bytes_.empty()); } |
132 | 131 |
133 bool ReadAndCheckNextMessage(uint32_t expected_size) { | 132 bool ReadAndCheckNextMessage(uint32_t expected_size) { |
134 unsigned char buffer[4096]; | 133 unsigned char buffer[4096]; |
135 | 134 |
136 for (size_t i = 0; i < kMessageReaderMaxPollIterations;) { | 135 for (size_t i = 0; i < kMessageReaderMaxPollIterations;) { |
137 size_t read_size = 0; | 136 size_t read_size = 0; |
138 CHECK(mojo::test::NonBlockingRead( | 137 CHECK(mojo::test::NonBlockingRead(handle_, buffer, sizeof(buffer), |
139 handle_, buffer, sizeof(buffer), &read_size)); | 138 &read_size)); |
140 | 139 |
141 // Append newly-read data to |bytes_|. | 140 // Append newly-read data to |bytes_|. |
142 bytes_.insert(bytes_.end(), buffer, buffer + read_size); | 141 bytes_.insert(bytes_.end(), buffer, buffer + read_size); |
143 | 142 |
144 // If we have the header.... | 143 // If we have the header.... |
145 size_t message_size; | 144 size_t message_size; |
146 if (MessageInTransit::GetNextMessageSize( | 145 if (MessageInTransit::GetNextMessageSize( |
147 bytes_.empty() ? nullptr : &bytes_[0], | 146 bytes_.empty() ? nullptr : &bytes_[0], bytes_.size(), |
148 bytes_.size(), | |
149 &message_size)) { | 147 &message_size)) { |
150 // If we've read the whole message.... | 148 // If we've read the whole message.... |
151 if (bytes_.size() >= message_size) { | 149 if (bytes_.size() >= message_size) { |
152 bool rv = true; | 150 bool rv = true; |
153 MessageInTransit::View message_view(message_size, &bytes_[0]); | 151 MessageInTransit::View message_view(message_size, &bytes_[0]); |
154 CHECK_EQ(message_view.main_buffer_size(), message_size); | 152 CHECK_EQ(message_view.main_buffer_size(), message_size); |
155 | 153 |
156 if (message_view.num_bytes() != expected_size) { | 154 if (message_view.num_bytes() != expected_size) { |
157 LOG(ERROR) << "Wrong size: " << message_size << " instead of " | 155 LOG(ERROR) << "Wrong size: " << message_size << " instead of " |
158 << expected_size << " bytes."; | 156 << expected_size << " bytes."; |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 DISALLOW_COPY_AND_ASSIGN(ReadCountdownRawChannelDelegate); | 369 DISALLOW_COPY_AND_ASSIGN(ReadCountdownRawChannelDelegate); |
372 }; | 370 }; |
373 | 371 |
374 TEST_F(RawChannelTest, WriteMessageAndOnReadMessage) { | 372 TEST_F(RawChannelTest, WriteMessageAndOnReadMessage) { |
375 static const size_t kNumWriterThreads = 10; | 373 static const size_t kNumWriterThreads = 10; |
376 static const size_t kNumWriteMessagesPerThread = 4000; | 374 static const size_t kNumWriteMessagesPerThread = 4000; |
377 | 375 |
378 WriteOnlyRawChannelDelegate writer_delegate; | 376 WriteOnlyRawChannelDelegate writer_delegate; |
379 scoped_ptr<RawChannel> writer_rc(RawChannel::Create(handles[0].Pass())); | 377 scoped_ptr<RawChannel> writer_rc(RawChannel::Create(handles[0].Pass())); |
380 io_thread()->PostTaskAndWait(FROM_HERE, | 378 io_thread()->PostTaskAndWait(FROM_HERE, |
381 base::Bind(&InitOnIOThread, | 379 base::Bind(&InitOnIOThread, writer_rc.get(), |
382 writer_rc.get(), | |
383 base::Unretained(&writer_delegate))); | 380 base::Unretained(&writer_delegate))); |
384 | 381 |
385 ReadCountdownRawChannelDelegate reader_delegate(kNumWriterThreads * | 382 ReadCountdownRawChannelDelegate reader_delegate(kNumWriterThreads * |
386 kNumWriteMessagesPerThread); | 383 kNumWriteMessagesPerThread); |
387 scoped_ptr<RawChannel> reader_rc(RawChannel::Create(handles[1].Pass())); | 384 scoped_ptr<RawChannel> reader_rc(RawChannel::Create(handles[1].Pass())); |
388 io_thread()->PostTaskAndWait(FROM_HERE, | 385 io_thread()->PostTaskAndWait(FROM_HERE, |
389 base::Bind(&InitOnIOThread, | 386 base::Bind(&InitOnIOThread, reader_rc.get(), |
390 reader_rc.get(), | |
391 base::Unretained(&reader_delegate))); | 387 base::Unretained(&reader_delegate))); |
392 | 388 |
393 { | 389 { |
394 ScopedVector<RawChannelWriterThread> writer_threads; | 390 ScopedVector<RawChannelWriterThread> writer_threads; |
395 for (size_t i = 0; i < kNumWriterThreads; i++) { | 391 for (size_t i = 0; i < kNumWriterThreads; i++) { |
396 writer_threads.push_back(new RawChannelWriterThread( | 392 writer_threads.push_back(new RawChannelWriterThread( |
397 writer_rc.get(), kNumWriteMessagesPerThread)); | 393 writer_rc.get(), kNumWriteMessagesPerThread)); |
398 } | 394 } |
399 for (size_t i = 0; i < writer_threads.size(); i++) | 395 for (size_t i = 0; i < writer_threads.size(); i++) |
400 writer_threads[i]->Start(); | 396 writer_threads[i]->Start(); |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 | 675 |
680 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); | 676 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); |
681 | 677 |
682 // Wait for the delegate, which will shut the |RawChannel| down. | 678 // Wait for the delegate, which will shut the |RawChannel| down. |
683 delegate.Wait(); | 679 delegate.Wait(); |
684 } | 680 } |
685 | 681 |
686 } // namespace | 682 } // namespace |
687 } // namespace system | 683 } // namespace system |
688 } // namespace mojo | 684 } // namespace mojo |
OLD | NEW |