Skip to content

Commit b06518a

Browse files
committed
Updated the test. Increased the buffer size.
1 parent b0a38a1 commit b06518a

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

lldb/unittests/Host/PipeTest.cpp

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "lldb/Host/FileSystem.h"
1212
#include "lldb/Host/HostInfo.h"
1313
#include "gtest/gtest.h"
14-
14+
#include <fcntl.h>
1515
#include <numeric>
1616
#include <vector>
1717

@@ -58,58 +58,69 @@ TEST_F(PipeTest, OpenAsReader) {
5858
TEST_F(PipeTest, WriteWithTimeout) {
5959
Pipe pipe;
6060
ASSERT_THAT_ERROR(pipe.CreateNew(false).ToError(), llvm::Succeeded());
61-
// Note write_chunk_size must be less than the pipe buffer.
61+
6262
// The pipe buffer is 1024 for PipeWindows and at least 512 on Darwin.
63-
const size_t buf_size = 8192;
63+
// In Linux versions before 2.6.11, the capacity of a pipe was the same as the
64+
// system page size (e.g., 4096 bytes on i386).
65+
// Since Linux 2.6.11, the pipe capacity is 16 pages (i.e., 65,536 bytes in a
66+
// system with a page size of 4096 bytes).
67+
// Since Linux 2.6.35, the default pipe capacity is 16 pages, but the capacity
68+
// can be queried and set using the fcntl(2) F_GETPIPE_SZ and F_SETPIPE_SZ
69+
// operations:
70+
71+
#if !defined(_WIN32) && defined(F_SETPIPE_SZ)
72+
::fcntl(pipe.GetWriteFileDescriptor(), F_SETPIPE_SZ, 4096);
73+
#endif
74+
75+
const size_t buf_size = 66000;
76+
77+
// Note write_chunk_size must be less than the pipe buffer.
6478
const size_t write_chunk_size = 234;
6579

6680
std::vector<int32_t> write_buf(buf_size / sizeof(int32_t));
6781
std::iota(write_buf.begin(), write_buf.end(), 0);
6882
std::vector<int32_t> read_buf(write_buf.size() + 100, -1);
6983

70-
char *write_ptr = (char *)&write_buf.front();
71-
char *read_ptr = (char *)&read_buf.front();
84+
char *write_ptr = reinterpret_cast<char *>(write_buf.data());
85+
char *read_ptr = reinterpret_cast<char *>(read_buf.data());
7286
size_t write_bytes = 0;
7387
size_t read_bytes = 0;
7488
size_t num_bytes = 0;
7589

7690
// Write to the pipe until it is full.
77-
while (write_bytes < buf_size) {
91+
while (write_bytes + write_chunk_size <= buf_size) {
7892
Status error =
7993
pipe.WriteWithTimeout(write_ptr + write_bytes, write_chunk_size,
8094
std::chrono::milliseconds(10), num_bytes);
8195
if (error.Fail())
82-
break; // The write buffer is full
96+
break; // The write buffer is full.
8397
write_bytes += num_bytes;
8498
}
85-
ASSERT_TRUE(write_bytes + write_chunk_size <= buf_size);
99+
ASSERT_LE(write_bytes + write_chunk_size, buf_size)
100+
<< "Pipe buffer larger than expected";
86101

87102
// Attempt a write with a long timeout.
88103
auto start_time = std::chrono::steady_clock::now();
89-
ASSERT_THAT_ERROR(
90-
pipe.WriteWithTimeout(write_ptr + write_bytes, write_chunk_size,
91-
std::chrono::milliseconds(2000), num_bytes)
92-
.ToError(),
93-
llvm::Failed());
94-
auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(
95-
std::chrono::steady_clock::now() - start_time)
96-
.count();
97-
ASSERT_GE(dur, 2000);
104+
ASSERT_THAT_ERROR(pipe.WriteWithTimeout(write_ptr + write_bytes,
105+
write_chunk_size,
106+
std::chrono::seconds(2), num_bytes)
107+
.ToError(),
108+
llvm::Failed());
109+
auto dur = std::chrono::steady_clock::now() - start_time;
110+
ASSERT_GE(dur, std::chrono::seconds(2));
98111

99-
// Attempt a write with a short timeout
112+
// Attempt a write with a short timeout.
100113
start_time = std::chrono::steady_clock::now();
101114
ASSERT_THAT_ERROR(
102115
pipe.WriteWithTimeout(write_ptr + write_bytes, write_chunk_size,
103116
std::chrono::milliseconds(200), num_bytes)
104117
.ToError(),
105118
llvm::Failed());
106-
dur = std::chrono::duration_cast<std::chrono::milliseconds>(
107-
std::chrono::steady_clock::now() - start_time)
108-
.count();
109-
ASSERT_GE(dur, 200);
110-
ASSERT_LT(dur, 300);
119+
dur = std::chrono::steady_clock::now() - start_time;
120+
ASSERT_GE(dur, std::chrono::milliseconds(200));
121+
ASSERT_LT(dur, std::chrono::seconds(2));
111122

112-
// Drain the pipe
123+
// Drain the pipe.
113124
while (read_bytes < write_bytes) {
114125
ASSERT_THAT_ERROR(
115126
pipe.ReadWithTimeout(read_ptr + read_bytes, write_bytes - read_bytes,

0 commit comments

Comments
 (0)