OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/common/data_pipe_utils.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 namespace mojo { |
| 10 namespace common { |
| 11 namespace { |
| 12 |
| 13 /* |
| 14 TEST(DataPipePeek, PeekNBytes) { |
| 15 DataPipe data_pipe; |
| 16 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get()); |
| 17 DataPipeProducerHandle producer(data_pipe.producer_handle.get()); |
| 18 |
| 19 // Inialize the pipe with 4 bytes. |
| 20 |
| 21 const char* s4 = "1234"; |
| 22 uint32_t num_bytes4 = 4; |
| 23 EXPECT_EQ(MOJO_RESULT_OK, |
| 24 WriteDataRaw(producer, s4, &num_bytes4, MOJO_WRITE_DATA_FLAG_NONE)); |
| 25 EXPECT_EQ(4u, num_bytes4); |
| 26 |
| 27 // We're not consuming data, so peeking for 4 bytes should always succeed. |
| 28 |
| 29 std::string bytes; |
| 30 MojoDeadline timeout = 0; |
| 31 EXPECT_TRUE(BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout)); |
| 32 EXPECT_EQ(bytes, std::string(s4)); |
| 33 |
| 34 timeout = 1000; // 1ms |
| 35 EXPECT_TRUE(BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout)); |
| 36 EXPECT_EQ(bytes, std::string(s4)); |
| 37 |
| 38 timeout = MOJO_DEADLINE_INDEFINITE; |
| 39 EXPECT_TRUE(BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout)); |
| 40 EXPECT_EQ(bytes, std::string(s4)); |
| 41 |
| 42 // Peeking for 5 bytes should fail, until another byte is written. |
| 43 |
| 44 uint32_t bytes1 = 1; |
| 45 uint32_t num_bytes5 = 5; |
| 46 const char* s1 = "5"; |
| 47 const char* s5 = "12345"; |
| 48 |
| 49 timeout = 0; |
| 50 EXPECT_FALSE(BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); |
| 51 |
| 52 timeout = 500; // Should cause peek to timeout after about 0.5ms. |
| 53 EXPECT_FALSE(BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); |
| 54 |
| 55 EXPECT_EQ(MOJO_RESULT_OK, |
| 56 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE)); |
| 57 EXPECT_EQ(1u, bytes1); |
| 58 |
| 59 EXPECT_TRUE(BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); |
| 60 EXPECT_EQ(bytes, std::string(s5)); |
| 61 |
| 62 // If the consumer side of the pipe is closed, peek should fail. |
| 63 |
| 64 data_pipe.consumer_handle.reset(); |
| 65 timeout = 0; |
| 66 EXPECT_FALSE(BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout)); |
| 67 } |
| 68 |
| 69 TEST(DataPipePeek, PeekLine) { |
| 70 DataPipe data_pipe; |
| 71 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get()); |
| 72 DataPipeProducerHandle producer(data_pipe.producer_handle.get()); |
| 73 |
| 74 // Inialize the pipe with 4 bytes and no newline. |
| 75 |
| 76 const char* s4 = "1234"; |
| 77 uint32_t num_bytes4 = 4; |
| 78 EXPECT_EQ(MOJO_RESULT_OK, |
| 79 WriteDataRaw(producer, s4, &num_bytes4, MOJO_WRITE_DATA_FLAG_NONE)); |
| 80 EXPECT_EQ(4u, num_bytes4); |
| 81 |
| 82 // Peeking for a line should fail. |
| 83 |
| 84 std::string str; |
| 85 size_t max_str_length = 5; |
| 86 MojoDeadline timeout = 0; |
| 87 EXPECT_FALSE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); |
| 88 |
| 89 // Writing a newline should cause PeekLine to succeed. |
| 90 |
| 91 uint32_t bytes1 = 1; |
| 92 const char* s1 = "\n"; |
| 93 EXPECT_EQ(MOJO_RESULT_OK, |
| 94 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE)); |
| 95 EXPECT_EQ(1u, bytes1); |
| 96 |
| 97 EXPECT_TRUE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); |
| 98 EXPECT_EQ(str, std::string(s4) + "\n"); |
| 99 |
| 100 // If the max_line_length parameter is less than the length of the |
| 101 // newline terminated string, then peek should fail. |
| 102 |
| 103 max_str_length = 3; |
| 104 EXPECT_FALSE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); |
| 105 } |
| 106 */ |
| 107 } // namespace |
| 108 } // namespace common |
| 109 } // namespace mojo |
OLD | NEW |