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

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

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

Powered by Google App Engine
This is Rietveld 408576698