Skip to content

Commit f62993f

Browse files
committed
add test, small fixes
1 parent 9116f31 commit f62993f

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

lldb/source/Host/common/Socket.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ Status SharedSocket::CompleteSending(lldb::pid_t child_pid) {
9999
return Status::FromError(num_bytes.takeError());
100100
if (*num_bytes != sizeof(protocol_info))
101101
return Status::FromErrorStringWithFormatv(
102-
"Write(WSAPROTOCOL_INFO) failed: {0} bytes", *num_bytes);
102+
"Write(WSAPROTOCOL_INFO) failed: wrote {0}/{1} bytes", *num_bytes,
103+
sizeof(protocol_info));
103104
#endif
104105
return Status();
105106
}
@@ -117,7 +118,8 @@ Status SharedSocket::GetNativeSocket(shared_fd_t fd, NativeSocket &socket) {
117118
return Status::FromError(num_bytes.takeError());
118119
if (*num_bytes != sizeof(protocol_info)) {
119120
return Status::FromErrorStringWithFormatv(
120-
"socket_pipe.Read(WSAPROTOCOL_INFO) failed: {0} bytes", *num_bytes);
121+
"Read(WSAPROTOCOL_INFO) failed: read {0}/{1} bytes", *num_bytes,
122+
sizeof(protocol_info));
121123
}
122124
}
123125
socket = ::WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,

lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,9 +1162,9 @@ Status GDBRemoteCommunication::StartDebugserverProcess(
11621162
char buf[10];
11631163
if (llvm::Expected<size_t> num_bytes = socket_pipe.Read(
11641164
buf, std::size(buf), std::chrono::seconds(10))) {
1165-
port_str.append(buf, *num_bytes);
11661165
if (*num_bytes == 0)
11671166
break;
1167+
port_str.append(buf, *num_bytes);
11681168
} else {
11691169
error = Status::FromError(num_bytes.takeError());
11701170
}

lldb/unittests/Host/PipeTest.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
#include "TestingSupport/SubsystemRAII.h"
1111
#include "lldb/Host/FileSystem.h"
1212
#include "lldb/Host/HostInfo.h"
13+
#include "llvm/Testing/Support/Error.h"
1314
#include "gtest/gtest.h"
15+
#include <chrono>
1416
#include <fcntl.h>
1517
#include <numeric>
18+
#include <thread>
1619
#include <vector>
20+
#include <future>
1721

1822
using namespace lldb_private;
1923

@@ -144,3 +148,52 @@ TEST_F(PipeTest, WriteWithTimeout) {
144148
pipe.Write(write_ptr, write_chunk_size, std::chrono::milliseconds(10)),
145149
llvm::Succeeded());
146150
}
151+
152+
TEST_F(PipeTest, ReadWithTimeout) {
153+
Pipe pipe;
154+
ASSERT_THAT_ERROR(pipe.CreateNew(false).ToError(), llvm::Succeeded());
155+
156+
char buf[100];
157+
// The pipe is initially empty. A polling read returns immediately.
158+
ASSERT_THAT_EXPECTED(pipe.Read(buf, sizeof(buf), std::chrono::seconds(0)),
159+
llvm::Failed());
160+
161+
// With a timeout, we should wait for at least this amount of time (but not
162+
// too much).
163+
auto start = std::chrono::steady_clock::now();
164+
ASSERT_THAT_EXPECTED(
165+
pipe.Read(buf, sizeof(buf), std::chrono::milliseconds(200)),
166+
llvm::Failed());
167+
auto dur = std::chrono::steady_clock::now() - start;
168+
EXPECT_GT(dur, std::chrono::milliseconds(200));
169+
EXPECT_LT(dur, std::chrono::seconds(2));
170+
171+
// Write something into the pipe, and read it back. The blocking read call
172+
// should return even though it hasn't filled the buffer.
173+
llvm::StringRef hello_world("Hello world!");
174+
ASSERT_THAT_EXPECTED(pipe.Write(hello_world.data(), hello_world.size()),
175+
llvm::HasValue(hello_world.size()));
176+
ASSERT_THAT_EXPECTED(pipe.Read(buf, sizeof(buf)),
177+
llvm::HasValue(hello_world.size()));
178+
EXPECT_EQ(llvm::StringRef(buf, hello_world.size()), hello_world);
179+
180+
// Now write something and try to read it in chunks.
181+
memset(buf, 0, sizeof(buf));
182+
ASSERT_THAT_EXPECTED(pipe.Write(hello_world.data(), hello_world.size()),
183+
llvm::HasValue(hello_world.size()));
184+
ASSERT_THAT_EXPECTED(pipe.Read(buf, 4), llvm::HasValue(4));
185+
ASSERT_THAT_EXPECTED(pipe.Read(buf + 4, sizeof(buf) - 4),
186+
llvm::HasValue(hello_world.size() - 4));
187+
EXPECT_EQ(llvm::StringRef(buf, hello_world.size()), hello_world);
188+
189+
// A blocking read should wait until the data arrives.
190+
memset(buf, 0, sizeof(buf));
191+
std::future<llvm::Expected<size_t>> future_num_bytes = std::async(
192+
std::launch::async, [&] { return pipe.Read(buf, sizeof(buf)); });
193+
std::this_thread::sleep_for(std::chrono::seconds(1));
194+
ASSERT_THAT_EXPECTED(pipe.Write(hello_world.data(), hello_world.size()),
195+
llvm::HasValue(hello_world.size()));
196+
ASSERT_THAT_EXPECTED(future_num_bytes.get(),
197+
llvm::HasValue(hello_world.size()));
198+
EXPECT_EQ(llvm::StringRef(buf, hello_world.size()), hello_world);
199+
}

0 commit comments

Comments
 (0)