Add ICU message format support
[chromium-blink-merge.git] / mojo / runner / data_pipe_peek_unittest.cc
blob42c21774bf69621f5365ce47fe57eefa5b2a69a8
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.
5 #include "mojo/shell/data_pipe_peek.h"
7 #include "mojo/runner/context.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace mojo {
11 namespace runner {
12 namespace {
14 TEST(DataPipePeek, PeekNBytes) {
15 Context::EnsureEmbedderIsInitialized();
17 DataPipe data_pipe;
18 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get());
19 DataPipeProducerHandle producer(data_pipe.producer_handle.get());
21 // Inialize the pipe with 4 bytes.
23 const char* s4 = "1234";
24 uint32_t num_bytes4 = 4;
25 EXPECT_EQ(MOJO_RESULT_OK,
26 WriteDataRaw(producer, s4, &num_bytes4, MOJO_WRITE_DATA_FLAG_NONE));
27 EXPECT_EQ(4u, num_bytes4);
29 // We're not consuming data, so peeking for 4 bytes should always succeed.
31 std::string bytes;
32 MojoDeadline timeout = 0;
33 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout));
34 EXPECT_EQ(bytes, std::string(s4));
36 timeout = 1000; // 1ms
37 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout));
38 EXPECT_EQ(bytes, std::string(s4));
40 timeout = MOJO_DEADLINE_INDEFINITE;
41 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes4, timeout));
42 EXPECT_EQ(bytes, std::string(s4));
44 // Peeking for 5 bytes should fail, until another byte is written.
46 uint32_t bytes1 = 1;
47 uint32_t num_bytes5 = 5;
48 const char* s1 = "5";
49 const char* s5 = "12345";
51 timeout = 0;
52 EXPECT_FALSE(
53 shell::BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout));
55 timeout = 500; // Should cause peek to timeout after about 0.5ms.
56 EXPECT_FALSE(
57 shell::BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout));
59 EXPECT_EQ(MOJO_RESULT_OK,
60 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE));
61 EXPECT_EQ(1u, bytes1);
63 EXPECT_TRUE(shell::BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout));
64 EXPECT_EQ(bytes, std::string(s5));
66 // If the consumer side of the pipe is closed, peek should fail.
68 data_pipe.consumer_handle.reset();
69 timeout = 0;
70 EXPECT_FALSE(
71 shell::BlockingPeekNBytes(consumer, &bytes, num_bytes5, timeout));
74 TEST(DataPipePeek, PeekLine) {
75 Context::EnsureEmbedderIsInitialized();
77 DataPipe data_pipe;
78 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get());
79 DataPipeProducerHandle producer(data_pipe.producer_handle.get());
81 // Inialize the pipe with 4 bytes and no newline.
83 const char* s4 = "1234";
84 uint32_t num_bytes4 = 4;
85 EXPECT_EQ(MOJO_RESULT_OK,
86 WriteDataRaw(producer, s4, &num_bytes4, MOJO_WRITE_DATA_FLAG_NONE));
87 EXPECT_EQ(4u, num_bytes4);
89 // Peeking for a line should fail.
91 std::string str;
92 size_t max_str_length = 5;
93 MojoDeadline timeout = 0;
94 EXPECT_FALSE(
95 shell::BlockingPeekLine(consumer, &str, max_str_length, timeout));
97 // Writing a newline should cause PeekLine to succeed.
99 uint32_t bytes1 = 1;
100 const char* s1 = "\n";
101 EXPECT_EQ(MOJO_RESULT_OK,
102 WriteDataRaw(producer, s1, &bytes1, MOJO_WRITE_DATA_FLAG_NONE));
103 EXPECT_EQ(1u, bytes1);
105 EXPECT_TRUE(shell::BlockingPeekLine(consumer, &str, max_str_length, timeout));
106 EXPECT_EQ(str, std::string(s4) + "\n");
108 // If the max_line_length parameter is less than the length of the
109 // newline terminated string, then peek should fail.
111 max_str_length = 3;
112 EXPECT_FALSE(
113 shell::BlockingPeekLine(consumer, &str, max_str_length, timeout));
116 } // namespace
117 } // namespace runner
118 } // namespace mojo