Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1758)

Side by Side Diff: mojo/shell/data_pipe_peek_unittest.cc

Issue 1701933004: Remove the old package manager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@am2
Patch Set: . Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/shell/data_pipe_peek.cc ('k') | mojo/shell/fetcher.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/shell/data_pipe_peek.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include "mojo/edk/system/test_utils.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace mojo {
14 namespace runner {
15 namespace {
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
23 TEST(DataPipePeek, PeekNBytes) {
24 DataPipe data_pipe;
25 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get());
26 DataPipeProducerHandle producer(data_pipe.producer_handle.get());
27
28 // Inialize the pipe with 4 bytes.
29
30 const char* s4 = "1234";
31 uint32_t num_bytes4 = 4;
32 EXPECT_EQ(MOJO_RESULT_OK,
33 WriteDataRaw(producer, s4, &num_bytes4, MOJO_WRITE_DATA_FLAG_NONE));
34 EXPECT_EQ(4u, num_bytes4);
35
36 // We're not consuming data, so peeking for 4 bytes should always succeed.
37
38 std::string bytes;
39 MojoDeadline timeout = MOJO_DEADLINE_INDEFINITE;
40 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout));
41 EXPECT_EQ(bytes, std::string(s4));
42
43 timeout = 1000; // 1ms
44 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout));
45 EXPECT_EQ(bytes, std::string(s4));
46
47 timeout = MOJO_DEADLINE_INDEFINITE;
48 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout));
49 EXPECT_EQ(bytes, std::string(s4));
50
51 // Peeking for 5 bytes should fail, until another byte is written.
52
53 uint32_t bytes1 = 1;
54 uint32_t num_bytes5 = 5;
55 const char* s1 = "5";
56 const char* s5 = "12345";
57
58 timeout = 0;
59 EXPECT_FALSE(
60 shell::BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout));
61
62 timeout = 500; // Should cause peek to timeout after about 0.5ms.
63 EXPECT_FALSE(
64 shell::BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout));
65
66 EXPECT_EQ(MOJO_RESULT_OK,
67 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE));
68 EXPECT_EQ(1u, bytes1);
69
70 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout));
71 EXPECT_EQ(bytes, std::string(s5));
72
73 // If the consumer side of the pipe is closed, peek should fail.
74
75 data_pipe.consumer_handle.reset();
76 timeout = 0;
77 EXPECT_FALSE(
78 shell::BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout));
79 }
80
81 TEST(DataPipePeek, PeekLine) {
82 DataPipe data_pipe;
83 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get());
84 DataPipeProducerHandle producer(data_pipe.producer_handle.get());
85
86 // Inialize the pipe with 4 bytes and no newline.
87
88 const char* s4 = "1234";
89 uint32_t num_bytes4 = 4;
90 EXPECT_EQ(MOJO_RESULT_OK,
91 WriteDataRaw(producer, s4, &num_bytes4, MOJO_WRITE_DATA_FLAG_NONE));
92 EXPECT_EQ(4u, num_bytes4);
93
94 // Peeking for a line should fail.
95
96 std::string str;
97 size_t max_str_length = 5;
98 MojoDeadline timeout = 0;
99 EXPECT_FALSE(
100 shell::BlockingPeekLine(consumer, &str, max_str_length, timeout));
101
102 // Writing a newline should cause PeekLine to succeed.
103
104 uint32_t bytes1 = 1;
105 const char* s1 = "\n";
106 timeout = MOJO_DEADLINE_INDEFINITE;
107 EXPECT_EQ(MOJO_RESULT_OK,
108 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE));
109 EXPECT_EQ(1u, bytes1);
110
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);
120 EXPECT_EQ(str, std::string(s4) + "\n");
121
122 // If the max_line_length parameter is less than the length of the
123 // newline terminated string, then peek should fail.
124
125 max_str_length = 3;
126 timeout = 0;
127 EXPECT_FALSE(
128 shell::BlockingPeekLine(consumer, &str, max_str_length, timeout));
129 }
130
131 } // namespace
132 } // namespace runner
133 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/shell/data_pipe_peek.cc ('k') | mojo/shell/fetcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698