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/shell/data_pipe_peek.h" | 5 #include "mojo/shell/data_pipe_peek.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
| 10 #include "mojo/edk/system/test_utils.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 namespace mojo { | 13 namespace mojo { |
13 namespace runner { | 14 namespace runner { |
14 namespace { | 15 namespace { |
15 | 16 |
| 17 // In various places, we have to poll (since, e.g., we can't yet wait for a |
| 18 // certain amount of data to be available). This is the maximum number of |
| 19 // iterations (separated by a short sleep). |
| 20 // TODO(vtl): Get rid of this. |
| 21 const size_t kMaxPoll = 100; |
| 22 |
16 TEST(DataPipePeek, PeekNBytes) { | 23 TEST(DataPipePeek, PeekNBytes) { |
17 DataPipe data_pipe; | 24 DataPipe data_pipe; |
18 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get()); | 25 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get()); |
19 DataPipeProducerHandle producer(data_pipe.producer_handle.get()); | 26 DataPipeProducerHandle producer(data_pipe.producer_handle.get()); |
20 | 27 |
21 // Inialize the pipe with 4 bytes. | 28 // Inialize the pipe with 4 bytes. |
22 | 29 |
23 const char* s4 = "1234"; | 30 const char* s4 = "1234"; |
24 uint32_t num_bytes4 = 4; | 31 uint32_t num_bytes4 = 4; |
25 EXPECT_EQ(MOJO_RESULT_OK, | 32 EXPECT_EQ(MOJO_RESULT_OK, |
26 WriteDataRaw(producer, s4, &num_bytes4, MOJO_WRITE_DATA_FLAG_NONE)); | 33 WriteDataRaw(producer, s4, &num_bytes4, MOJO_WRITE_DATA_FLAG_NONE)); |
27 EXPECT_EQ(4u, num_bytes4); | 34 EXPECT_EQ(4u, num_bytes4); |
28 | 35 |
29 // We're not consuming data, so peeking for 4 bytes should always succeed. | 36 // We're not consuming data, so peeking for 4 bytes should always succeed. |
30 | 37 |
31 std::string bytes; | 38 std::string bytes; |
32 MojoDeadline timeout = 0; | 39 MojoDeadline timeout = MOJO_DEADLINE_INDEFINITE; |
33 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout)); | 40 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout)); |
34 EXPECT_EQ(bytes, std::string(s4)); | 41 EXPECT_EQ(bytes, std::string(s4)); |
35 | 42 |
36 timeout = 1000; // 1ms | 43 timeout = 1000; // 1ms |
37 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout)); | 44 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout)); |
38 EXPECT_EQ(bytes, std::string(s4)); | 45 EXPECT_EQ(bytes, std::string(s4)); |
39 | 46 |
40 timeout = MOJO_DEADLINE_INDEFINITE; | 47 timeout = MOJO_DEADLINE_INDEFINITE; |
41 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout)); | 48 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout)); |
42 EXPECT_EQ(bytes, std::string(s4)); | 49 EXPECT_EQ(bytes, std::string(s4)); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 std::string str; | 96 std::string str; |
90 size_t max_str_length = 5; | 97 size_t max_str_length = 5; |
91 MojoDeadline timeout = 0; | 98 MojoDeadline timeout = 0; |
92 EXPECT_FALSE( | 99 EXPECT_FALSE( |
93 shell::BlockingPeekLine(consumer, &str, max_str_length, timeout)); | 100 shell::BlockingPeekLine(consumer, &str, max_str_length, timeout)); |
94 | 101 |
95 // Writing a newline should cause PeekLine to succeed. | 102 // Writing a newline should cause PeekLine to succeed. |
96 | 103 |
97 uint32_t bytes1 = 1; | 104 uint32_t bytes1 = 1; |
98 const char* s1 = "\n"; | 105 const char* s1 = "\n"; |
| 106 timeout = MOJO_DEADLINE_INDEFINITE; |
99 EXPECT_EQ(MOJO_RESULT_OK, | 107 EXPECT_EQ(MOJO_RESULT_OK, |
100 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE)); | 108 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE)); |
101 EXPECT_EQ(1u, bytes1); | 109 EXPECT_EQ(1u, bytes1); |
102 | 110 |
103 EXPECT_TRUE(shell::BlockingPeekLine(consumer, &str, max_str_length, timeout)); | 111 bool succeeded = false; |
| 112 for (size_t i = 0; i < kMaxPoll; i++) { |
| 113 if (shell::BlockingPeekLine(consumer, &str, max_str_length, timeout)) { |
| 114 succeeded = true; |
| 115 break; |
| 116 } |
| 117 edk::test::Sleep(edk::test::EpsilonDeadline()); |
| 118 } |
| 119 EXPECT_TRUE(succeeded); |
104 EXPECT_EQ(str, std::string(s4) + "\n"); | 120 EXPECT_EQ(str, std::string(s4) + "\n"); |
105 | 121 |
106 // If the max_line_length parameter is less than the length of the | 122 // If the max_line_length parameter is less than the length of the |
107 // newline terminated string, then peek should fail. | 123 // newline terminated string, then peek should fail. |
108 | 124 |
109 max_str_length = 3; | 125 max_str_length = 3; |
| 126 timeout = 0; |
110 EXPECT_FALSE( | 127 EXPECT_FALSE( |
111 shell::BlockingPeekLine(consumer, &str, max_str_length, timeout)); | 128 shell::BlockingPeekLine(consumer, &str, max_str_length, timeout)); |
112 } | 129 } |
113 | 130 |
114 } // namespace | 131 } // namespace |
115 } // namespace runner | 132 } // namespace runner |
116 } // namespace mojo | 133 } // namespace mojo |
OLD | NEW |