Skip to content

Commit

Permalink
Updated the test. Increased the buffer size.
Browse files Browse the repository at this point in the history
  • Loading branch information
slydiman committed Aug 5, 2024
1 parent b0a38a1 commit b06518a
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions lldb/unittests/Host/PipeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/HostInfo.h"
#include "gtest/gtest.h"

#include <fcntl.h>
#include <numeric>
#include <vector>

Expand Down Expand Up @@ -58,58 +58,69 @@ TEST_F(PipeTest, OpenAsReader) {
TEST_F(PipeTest, WriteWithTimeout) {
Pipe pipe;
ASSERT_THAT_ERROR(pipe.CreateNew(false).ToError(), llvm::Succeeded());
// Note write_chunk_size must be less than the pipe buffer.

// The pipe buffer is 1024 for PipeWindows and at least 512 on Darwin.
const size_t buf_size = 8192;
// In Linux versions before 2.6.11, the capacity of a pipe was the same as the
// system page size (e.g., 4096 bytes on i386).
// Since Linux 2.6.11, the pipe capacity is 16 pages (i.e., 65,536 bytes in a
// system with a page size of 4096 bytes).
// Since Linux 2.6.35, the default pipe capacity is 16 pages, but the capacity
// can be queried and set using the fcntl(2) F_GETPIPE_SZ and F_SETPIPE_SZ
// operations:

#if !defined(_WIN32) && defined(F_SETPIPE_SZ)
::fcntl(pipe.GetWriteFileDescriptor(), F_SETPIPE_SZ, 4096);
#endif

const size_t buf_size = 66000;

// Note write_chunk_size must be less than the pipe buffer.
const size_t write_chunk_size = 234;

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

char *write_ptr = (char *)&write_buf.front();
char *read_ptr = (char *)&read_buf.front();
char *write_ptr = reinterpret_cast<char *>(write_buf.data());
char *read_ptr = reinterpret_cast<char *>(read_buf.data());
size_t write_bytes = 0;
size_t read_bytes = 0;
size_t num_bytes = 0;

// Write to the pipe until it is full.
while (write_bytes < buf_size) {
while (write_bytes + write_chunk_size <= buf_size) {
Status error =
pipe.WriteWithTimeout(write_ptr + write_bytes, write_chunk_size,
std::chrono::milliseconds(10), num_bytes);
if (error.Fail())
break; // The write buffer is full
break; // The write buffer is full.
write_bytes += num_bytes;
}
ASSERT_TRUE(write_bytes + write_chunk_size <= buf_size);
ASSERT_LE(write_bytes + write_chunk_size, buf_size)
<< "Pipe buffer larger than expected";

// Attempt a write with a long timeout.
auto start_time = std::chrono::steady_clock::now();
ASSERT_THAT_ERROR(
pipe.WriteWithTimeout(write_ptr + write_bytes, write_chunk_size,
std::chrono::milliseconds(2000), num_bytes)
.ToError(),
llvm::Failed());
auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start_time)
.count();
ASSERT_GE(dur, 2000);
ASSERT_THAT_ERROR(pipe.WriteWithTimeout(write_ptr + write_bytes,
write_chunk_size,
std::chrono::seconds(2), num_bytes)
.ToError(),
llvm::Failed());
auto dur = std::chrono::steady_clock::now() - start_time;
ASSERT_GE(dur, std::chrono::seconds(2));

// Attempt a write with a short timeout
// Attempt a write with a short timeout.
start_time = std::chrono::steady_clock::now();
ASSERT_THAT_ERROR(
pipe.WriteWithTimeout(write_ptr + write_bytes, write_chunk_size,
std::chrono::milliseconds(200), num_bytes)
.ToError(),
llvm::Failed());
dur = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start_time)
.count();
ASSERT_GE(dur, 200);
ASSERT_LT(dur, 300);
dur = std::chrono::steady_clock::now() - start_time;
ASSERT_GE(dur, std::chrono::milliseconds(200));
ASSERT_LT(dur, std::chrono::seconds(2));

// Drain the pipe
// Drain the pipe.
while (read_bytes < write_bytes) {
ASSERT_THAT_ERROR(
pipe.ReadWithTimeout(read_ptr + read_bytes, write_bytes - read_bytes,
Expand Down

0 comments on commit b06518a

Please sign in to comment.