[lldb] Make CommunicationTest compatible with windows

Our (TCP) socket support is in a much better state than pipes. Use that
for testing the Communication class.

Move the CreateTCPConnectedSockets function
(SocketTestUtilities.{h,cpp}) to a place where it can be used from
Communication tests.
This commit is contained in:
Pavel Labath 2022-08-24 15:59:46 +02:00
parent 41667a8b9b
commit 59656c0492
10 changed files with 50 additions and 41 deletions

View File

@ -21,6 +21,7 @@ add_lldb_unittest(LLDBCoreTests
lldbPluginSymbolFileSymtab
lldbSymbol
lldbUtilityHelpers
lldbHostHelpers
LLVMTestingSupport
LINK_COMPONENTS
Support

View File

@ -12,6 +12,8 @@
#include "lldb/Host/Pipe.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
#include "TestingSupport/Host/SocketTestUtilities.h"
#include "TestingSupport/SubsystemRAII.h"
#include <thread>
@ -21,30 +23,31 @@
using namespace lldb_private;
#ifndef _WIN32
static void CommunicationReadTest(bool use_read_thread) {
Pipe pipe;
ASSERT_THAT_ERROR(pipe.CreateNew(/*child_process_inherit=*/false).ToError(),
llvm::Succeeded());
class CommunicationTest : public testing::Test {
private:
SubsystemRAII<Socket> m_subsystems;
};
Status error;
size_t bytes_written;
ASSERT_THAT_ERROR(pipe.Write("test", 4, bytes_written).ToError(),
llvm::Succeeded());
ASSERT_EQ(bytes_written, 4U);
static void CommunicationReadTest(bool use_read_thread) {
std::unique_ptr<TCPSocket> a, b;
ASSERT_TRUE(CreateTCPConnectedSockets("localhost", &a, &b));
size_t num_bytes = 4;
ASSERT_THAT_ERROR(a->Write("test", num_bytes).ToError(), llvm::Succeeded());
ASSERT_EQ(num_bytes, 4U);
Communication comm("test");
comm.SetConnection(std::make_unique<ConnectionFileDescriptor>(
pipe.ReleaseReadFileDescriptor(), /*owns_fd=*/true));
comm.SetConnection(std::make_unique<ConnectionFileDescriptor>(b.release()));
comm.SetCloseOnEOF(true);
if (use_read_thread)
if (use_read_thread) {
ASSERT_TRUE(comm.StartReadThread());
}
// This read should wait for the data to become available and return it.
lldb::ConnectionStatus status = lldb::eConnectionStatusSuccess;
char buf[16];
error.Clear();
Status error;
EXPECT_EQ(
comm.Read(buf, sizeof(buf), std::chrono::seconds(5), status, &error), 4U);
EXPECT_EQ(status, lldb::eConnectionStatusSuccess);
@ -68,7 +71,7 @@ static void CommunicationReadTest(bool use_read_thread) {
EXPECT_THAT_ERROR(error.ToError(), llvm::Failed());
// This read should return EOF.
pipe.CloseWriteFileDescriptor();
ASSERT_THAT_ERROR(a->Close().ToError(), llvm::Succeeded());
error.Clear();
EXPECT_EQ(
comm.Read(buf, sizeof(buf), std::chrono::seconds(5), status, &error), 0U);
@ -80,37 +83,33 @@ static void CommunicationReadTest(bool use_read_thread) {
EXPECT_TRUE(comm.JoinReadThread());
}
TEST(CommunicationTest, Read) {
TEST_F(CommunicationTest, Read) {
CommunicationReadTest(/*use_thread=*/false);
}
TEST(CommunicationTest, ReadThread) {
TEST_F(CommunicationTest, ReadThread) {
CommunicationReadTest(/*use_thread=*/true);
}
TEST(CommunicationTest, SynchronizeWhileClosing) {
// Set up a communication object reading from a pipe.
Pipe pipe;
ASSERT_THAT_ERROR(pipe.CreateNew(/*child_process_inherit=*/false).ToError(),
llvm::Succeeded());
TEST_F(CommunicationTest, SynchronizeWhileClosing) {
std::unique_ptr<TCPSocket> a, b;
ASSERT_TRUE(CreateTCPConnectedSockets("localhost", &a, &b));
Communication comm("test");
comm.SetConnection(std::make_unique<ConnectionFileDescriptor>(
pipe.ReleaseReadFileDescriptor(), /*owns_fd=*/true));
comm.SetConnection(std::make_unique<ConnectionFileDescriptor>(b.release()));
comm.SetCloseOnEOF(true);
ASSERT_TRUE(comm.StartReadThread());
// Ensure that we can safely synchronize with the read thread while it is
// closing the read end (in response to us closing the write end).
pipe.CloseWriteFileDescriptor();
ASSERT_THAT_ERROR(a->Close().ToError(), llvm::Succeeded());
comm.SynchronizeWithReadThread();
ASSERT_TRUE(comm.StopReadThread());
}
#endif
#if LLDB_ENABLE_POSIX
TEST(CommunicationTest, WriteAll) {
TEST_F(CommunicationTest, WriteAll) {
Pipe pipe;
ASSERT_THAT_ERROR(pipe.CreateNew(/*child_process_inherit=*/false).ToError(),
llvm::Succeeded());

View File

@ -20,7 +20,7 @@ using Entry = FormatEntity::Entry;
TEST(FormatEntityTest, DefinitionConstructionNameAndType) {
Definition d("foo", FormatEntity::Entry::Type::Invalid);
EXPECT_EQ(d.name, "foo");
EXPECT_STREQ(d.name, "foo");
EXPECT_EQ(d.string, nullptr);
EXPECT_EQ(d.type, FormatEntity::Entry::Type::Invalid);
EXPECT_EQ(d.data, 0UL);
@ -32,8 +32,8 @@ TEST(FormatEntityTest, DefinitionConstructionNameAndType) {
TEST(FormatEntityTest, DefinitionConstructionNameAndString) {
Definition d("foo", "string");
EXPECT_EQ(d.name, "foo");
EXPECT_EQ(d.string, "string");
EXPECT_STREQ(d.name, "foo");
EXPECT_STREQ(d.string, "string");
EXPECT_EQ(d.type, FormatEntity::Entry::Type::EscapeCode);
EXPECT_EQ(d.data, 0UL);
EXPECT_EQ(d.num_children, 0UL);
@ -44,7 +44,7 @@ TEST(FormatEntityTest, DefinitionConstructionNameAndString) {
TEST(FormatEntityTest, DefinitionConstructionNameTypeData) {
Definition d("foo", FormatEntity::Entry::Type::Invalid, 33);
EXPECT_EQ(d.name, "foo");
EXPECT_STREQ(d.name, "foo");
EXPECT_EQ(d.string, nullptr);
EXPECT_EQ(d.type, FormatEntity::Entry::Type::Invalid);
EXPECT_EQ(d.data, 33UL);
@ -56,14 +56,14 @@ TEST(FormatEntityTest, DefinitionConstructionNameTypeData) {
TEST(FormatEntityTest, DefinitionConstructionNameTypeChildren) {
Definition d("foo", FormatEntity::Entry::Type::Invalid, 33);
Definition parent("parent", FormatEntity::Entry::Type::Invalid, 1, &d);
EXPECT_EQ(parent.name, "parent");
EXPECT_EQ(parent.string, nullptr);
EXPECT_STREQ(parent.name, "parent");
EXPECT_STREQ(parent.string, nullptr);
EXPECT_EQ(parent.type, FormatEntity::Entry::Type::Invalid);
EXPECT_EQ(parent.num_children, 1UL);
EXPECT_EQ(parent.children, &d);
EXPECT_FALSE(parent.keep_separator);
EXPECT_EQ(parent.children[0].name, "foo");
EXPECT_STREQ(parent.children[0].name, "foo");
EXPECT_EQ(parent.children[0].string, nullptr);
EXPECT_EQ(parent.children[0].type, FormatEntity::Entry::Type::Invalid);
EXPECT_EQ(parent.children[0].data, 33UL);

View File

@ -11,7 +11,6 @@ set (FILES
ProcessLaunchInfoTest.cpp
SocketAddressTest.cpp
SocketTest.cpp
SocketTestUtilities.cpp
ThreadLauncherTest.cpp
XMLTest.cpp
)
@ -34,5 +33,6 @@ add_lldb_unittest(HostTests
LINK_LIBS
lldbHost
lldbUtilityHelpers
lldbHostHelpers
LLVMTestingSupport
)

View File

@ -6,9 +6,8 @@
//
//===----------------------------------------------------------------------===//
#include "SocketTestUtilities.h"
#include "TestingSupport/Host/SocketTestUtilities.h"
#include "gtest/gtest.h"
#include "TestingSupport/SubsystemRAII.h"
#include "lldb/Host/posix/ConnectionFileDescriptorPosix.h"
#include "lldb/Utility/UriParser.h"

View File

@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
#include "SocketTestUtilities.h"
#include "TestingSupport/Host/SocketTestUtilities.h"
#include "TestingSupport/SubsystemRAII.h"
#include "lldb/Host/Config.h"
#include "lldb/Utility/UriParser.h"

View File

@ -12,4 +12,5 @@ add_lldb_library(lldbUtilityHelpers
ObjectYAML
)
add_subdirectory(Host)
add_subdirectory(Symbol)

View File

@ -0,0 +1,9 @@
set_property(DIRECTORY PROPERTY EXCLUDE_FROM_ALL ON)
add_lldb_library(lldbHostHelpers
SocketTestUtilities.cpp
LINK_LIBS
lldbHost
LLVMTestingSupport
llvm_gtest
)

View File

@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
#include "SocketTestUtilities.h"
#include "TestingSupport/Host/SocketTestUtilities.h"
#include "lldb/Host/Config.h"
#include "lldb/Utility/StreamString.h"

View File

@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
#ifndef LLDB_UNITTESTS_HOST_SOCKETTESTUTILITIES_H
#define LLDB_UNITTESTS_HOST_SOCKETTESTUTILITIES_H
#ifndef LLDB_UNITTESTS_TESTINGSUPPORT_HOST_SOCKETTESTUTILITIES_H
#define LLDB_UNITTESTS_TESTINGSUPPORT_HOST_SOCKETTESTUTILITIES_H
#include <cstdio>
#include <functional>