| 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 #include <stdio.h> | 8 #include <stdio.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <utility> | 11 #include <utility> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "mojo/edk/embedder/platform_channel_pair.h" | 15 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 16 #include "mojo/edk/platform/platform_handle.h" | 16 #include "mojo/edk/platform/platform_handle.h" |
| 17 #include "mojo/edk/platform/scoped_platform_handle.h" | 17 #include "mojo/edk/platform/scoped_platform_handle.h" |
| 18 #include "mojo/edk/platform/thread_utils.h" |
| 18 #include "mojo/edk/system/message_in_transit.h" | 19 #include "mojo/edk/system/message_in_transit.h" |
| 19 #include "mojo/edk/system/test/random.h" | 20 #include "mojo/edk/system/test/random.h" |
| 20 #include "mojo/edk/system/test/scoped_test_dir.h" | 21 #include "mojo/edk/system/test/scoped_test_dir.h" |
| 21 #include "mojo/edk/system/test/simple_test_thread.h" | 22 #include "mojo/edk/system/test/simple_test_thread.h" |
| 22 #include "mojo/edk/system/test/sleep.h" | |
| 23 #include "mojo/edk/system/test/test_io_thread.h" | 23 #include "mojo/edk/system/test/test_io_thread.h" |
| 24 #include "mojo/edk/system/test/timeouts.h" |
| 24 #include "mojo/edk/system/transport_data.h" | 25 #include "mojo/edk/system/transport_data.h" |
| 25 #include "mojo/edk/test/test_utils.h" | 26 #include "mojo/edk/test/test_utils.h" |
| 26 #include "mojo/edk/util/make_unique.h" | 27 #include "mojo/edk/util/make_unique.h" |
| 27 #include "mojo/edk/util/mutex.h" | 28 #include "mojo/edk/util/mutex.h" |
| 28 #include "mojo/edk/util/scoped_file.h" | 29 #include "mojo/edk/util/scoped_file.h" |
| 29 #include "mojo/edk/util/waitable_event.h" | 30 #include "mojo/edk/util/waitable_event.h" |
| 30 #include "mojo/public/cpp/system/macros.h" | 31 #include "mojo/public/cpp/system/macros.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 33 |
| 33 using mojo::platform::PlatformHandle; | 34 using mojo::platform::PlatformHandle; |
| 34 using mojo::platform::ScopedPlatformHandle; | 35 using mojo::platform::ScopedPlatformHandle; |
| 36 using mojo::platform::ThreadSleep; |
| 35 using mojo::util::AutoResetWaitableEvent; | 37 using mojo::util::AutoResetWaitableEvent; |
| 36 using mojo::util::MakeUnique; | 38 using mojo::util::MakeUnique; |
| 37 using mojo::util::Mutex; | 39 using mojo::util::Mutex; |
| 38 using mojo::util::MutexLocker; | 40 using mojo::util::MutexLocker; |
| 39 | 41 |
| 40 namespace mojo { | 42 namespace mojo { |
| 41 namespace system { | 43 namespace system { |
| 42 namespace { | 44 namespace { |
| 43 | 45 |
| 44 std::unique_ptr<MessageInTransit> MakeTestMessage(uint32_t num_bytes) { | 46 std::unique_ptr<MessageInTransit> MakeTestMessage(uint32_t num_bytes) { |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 | 168 |
| 167 // Erase message data. | 169 // Erase message data. |
| 168 bytes_.erase(bytes_.begin(), | 170 bytes_.erase(bytes_.begin(), |
| 169 bytes_.begin() + message_view.main_buffer_size()); | 171 bytes_.begin() + message_view.main_buffer_size()); |
| 170 return rv; | 172 return rv; |
| 171 } | 173 } |
| 172 } | 174 } |
| 173 | 175 |
| 174 if (static_cast<size_t>(read_size) < sizeof(buffer)) { | 176 if (static_cast<size_t>(read_size) < sizeof(buffer)) { |
| 175 i++; | 177 i++; |
| 176 test::SleepMilliseconds(kMessageReaderSleepMs); | 178 ThreadSleep(test::DeadlineFromMilliseconds(kMessageReaderSleepMs)); |
| 177 } | 179 } |
| 178 } | 180 } |
| 179 | 181 |
| 180 LOG(ERROR) << "Too many iterations."; | 182 LOG(ERROR) << "Too many iterations."; |
| 181 return false; | 183 return false; |
| 182 } | 184 } |
| 183 | 185 |
| 184 private: | 186 private: |
| 185 const PlatformHandle handle_; | 187 const PlatformHandle handle_; |
| 186 | 188 |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 for (size_t i = 0; i < kNumWriterThreads; i++) { | 395 for (size_t i = 0; i < kNumWriterThreads; i++) { |
| 394 writer_threads.push_back(MakeUnique<RawChannelWriterThread>( | 396 writer_threads.push_back(MakeUnique<RawChannelWriterThread>( |
| 395 writer_rc.get(), kNumWriteMessagesPerThread)); | 397 writer_rc.get(), kNumWriteMessagesPerThread)); |
| 396 } | 398 } |
| 397 for (size_t i = 0; i < writer_threads.size(); i++) | 399 for (size_t i = 0; i < writer_threads.size(); i++) |
| 398 writer_threads[i]->Start(); | 400 writer_threads[i]->Start(); |
| 399 } // Joins all the writer threads. | 401 } // Joins all the writer threads. |
| 400 | 402 |
| 401 // Sleep a bit, to let any extraneous reads be processed. (There shouldn't be | 403 // Sleep a bit, to let any extraneous reads be processed. (There shouldn't be |
| 402 // any, but we want to know about them.) | 404 // any, but we want to know about them.) |
| 403 test::SleepMilliseconds(100u); | 405 ThreadSleep(test::DeadlineFromMilliseconds(100u)); |
| 404 | 406 |
| 405 // Wait for reading to finish. | 407 // Wait for reading to finish. |
| 406 reader_delegate.Wait(); | 408 reader_delegate.Wait(); |
| 407 | 409 |
| 408 io_thread()->PostTaskAndWait([&reader_rc]() { reader_rc->Shutdown(); }); | 410 io_thread()->PostTaskAndWait([&reader_rc]() { reader_rc->Shutdown(); }); |
| 409 | 411 |
| 410 io_thread()->PostTaskAndWait([&writer_rc]() { writer_rc->Shutdown(); }); | 412 io_thread()->PostTaskAndWait([&writer_rc]() { writer_rc->Shutdown(); }); |
| 411 } | 413 } |
| 412 | 414 |
| 413 // RawChannelTest.OnError ------------------------------------------------------ | 415 // RawChannelTest.OnError ------------------------------------------------------ |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 // We should get a write error. | 483 // We should get a write error. |
| 482 delegate.WaitForWriteError(); | 484 delegate.WaitForWriteError(); |
| 483 | 485 |
| 484 // We should also get a read error. | 486 // We should also get a read error. |
| 485 delegate.WaitForReadError(); | 487 delegate.WaitForReadError(); |
| 486 | 488 |
| 487 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(2))); | 489 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(2))); |
| 488 | 490 |
| 489 // Sleep a bit, to make sure we don't get another |OnError()| | 491 // Sleep a bit, to make sure we don't get another |OnError()| |
| 490 // notification. (If we actually get another one, |OnError()| crashes.) | 492 // notification. (If we actually get another one, |OnError()| crashes.) |
| 491 test::SleepMilliseconds(20u); | 493 ThreadSleep(test::DeadlineFromMilliseconds(20u)); |
| 492 | 494 |
| 493 io_thread()->PostTaskAndWait([&rc]() { rc->Shutdown(); }); | 495 io_thread()->PostTaskAndWait([&rc]() { rc->Shutdown(); }); |
| 494 } | 496 } |
| 495 | 497 |
| 496 // RawChannelTest.ReadUnaffectedByWriteError ----------------------------------- | 498 // RawChannelTest.ReadUnaffectedByWriteError ----------------------------------- |
| 497 | 499 |
| 498 TEST_F(RawChannelTest, ReadUnaffectedByWriteError) { | 500 TEST_F(RawChannelTest, ReadUnaffectedByWriteError) { |
| 499 const size_t kMessageCount = 5; | 501 const size_t kMessageCount = 5; |
| 500 | 502 |
| 501 // Write a few messages into the other end. | 503 // Write a few messages into the other end. |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 838 | 840 |
| 839 read_delegate.Wait(); | 841 read_delegate.Wait(); |
| 840 | 842 |
| 841 io_thread()->PostTaskAndWait([&rc_read]() { rc_read->Shutdown(); }); | 843 io_thread()->PostTaskAndWait([&rc_read]() { rc_read->Shutdown(); }); |
| 842 io_thread()->PostTaskAndWait([&rc_write]() { rc_write->Shutdown(); }); | 844 io_thread()->PostTaskAndWait([&rc_write]() { rc_write->Shutdown(); }); |
| 843 } | 845 } |
| 844 | 846 |
| 845 } // namespace | 847 } // namespace |
| 846 } // namespace system | 848 } // namespace system |
| 847 } // namespace mojo | 849 } // namespace mojo |
| OLD | NEW |