Skip to content

Commit

Permalink
Back out "fbcode/gloo/test"
Browse files Browse the repository at this point in the history
Summary: Put back tests that were erroneously removed by codemod. This is because these files were added in OSS and no-one bothered to update the `TARGETS` file.

Reviewed By: fduwjj

Differential Revision:
D65238799

Privacy Context Container: L1267325

fbshipit-source-id: d196d141ab90df04a5673ceefdfdf2378399c08a
  • Loading branch information
c-p-i-o authored and facebook-github-bot committed Oct 31, 2024
1 parent 7069642 commit a8c8506
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 0 deletions.
102 changes: 102 additions & 0 deletions gloo/test/memory_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* Copyright (c) 2019-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#include "gloo/test/base_test.h"

#include <fstream>
#include <sstream>

#ifdef _WIN32
#include <Windows.h>
#include <psapi.h>
#endif

namespace gloo {
namespace test {
namespace {

#ifdef _WIN32
size_t readResidentSetSize() {
PROCESS_MEMORY_COUNTERS counters{};
GetProcessMemoryInfo(GetCurrentProcess(), &counters, sizeof(counters));
return counters.WorkingSetSize;
}
#else
size_t readResidentSetSize() {
std::stringstream path;
path << "/proc/" << getpid() << "/statm";
std::ifstream f(path.str());
size_t size;
size_t resident;
f >> size >> resident;
return (getpagesize() * resident);
}
#endif

const std::vector<Transport> kTransportsForMemoryTest{
Transport::TCP,
#if GLOO_HAVE_TRANSPORT_TCP_TLS
Transport::TCP_TLS,
#endif
};

// Test parameterization.
using Param = Transport;

// Test fixture.
class MemoryTest : public BaseTest,
public ::testing::WithParamInterface<Param> {};

TEST_P(MemoryTest, ManySlotsNoLeaks) {
const auto transport = GetParam();
spawn(transport, 2, [&](std::shared_ptr<Context> context) {
size_t tmp0;
size_t tmp1;
auto buf0 = context->createUnboundBuffer(&tmp0, sizeof(tmp0));
auto buf1 = context->createUnboundBuffer(&tmp1, sizeof(tmp1));
auto step = [&](size_t slot) {
const auto peer = 1 - context->rank;
if (context->rank == 0) {
buf0->send(peer, slot);
buf1->recv(peer, slot);
buf0->waitSend();
buf1->waitRecv();
} else {
buf0->recv(peer, slot);
buf1->send(peer, slot);
buf0->waitRecv();
buf1->waitSend();
}
};

// Prime processes with a few send/recv ping/pongs
size_t slot = 0;
for (auto i = 0; i < 10; i++) {
step(slot++);
}

// Read current memory usage and run for a while
auto baselineResidentSetSize = readResidentSetSize();
for (auto i = 0; i < 10000; i++) {
step(slot++);
}

// Ensure memory usage didn't increase
auto newResidentSetSize = readResidentSetSize();
ASSERT_EQ(baselineResidentSetSize, newResidentSetSize);
});
}

INSTANTIATE_TEST_CASE_P(
MemoryTestDefault,
MemoryTest,
::testing::ValuesIn(kTransportsForMemoryTest));

} // namespace
} // namespace test
} // namespace gloo
91 changes: 91 additions & 0 deletions gloo/test/tls_tcp_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Copyright (c) 2019-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#include "gloo/config.h"
#if GLOO_HAVE_TRANSPORT_TCP_TLS

#include <gmock/gmock.h>

#include "gloo/test/multiproc_test.h"
#include "gloo/test/openssl_utils.h"

namespace gloo {
namespace test {
namespace {

const char* kDefaultDevice = "localhost";

class TlsTcpTest : public BaseTest {};

TEST_F(TlsTcpTest, CreateDeviceWithAllEmptyFilePaths) {
bool exception_thrown = false;
try {
::gloo::rendezvous::HashStore store;
auto device = ::gloo::transport::tcp::tls::CreateDevice(
kDefaultDevice, "", "", "", "");
auto context = device->createContext(0, 1);
} catch (::gloo::EnforceNotMet e) {
exception_thrown = true;
ASSERT_THAT(
e.what(),
::testing::ContainsRegex(
"Private key and certificate location must be specified"));
}
ASSERT_TRUE(exception_thrown);
}

TEST_F(TlsTcpTest, CreateDeviceWithCAEmptyFilePaths) {
bool exception_thrown = false;
try {
::gloo::rendezvous::HashStore store;
auto device = ::gloo::transport::tcp::tls::CreateDevice(
kDefaultDevice, pkey_file, cert_file, "", "");
auto context = device->createContext(0, 1);
} catch (::gloo::EnforceNotMet e) {
exception_thrown = true;
ASSERT_THAT(
e.what(),
::testing::ContainsRegex("CAfile or CApath must be specified"));
}
ASSERT_TRUE(exception_thrown);
}

TEST_F(TlsTcpTest, CreateDeviceWithUnknownCA) {
auto device = ::gloo::transport::tcp::tls::CreateDevice(
kDefaultDevice, pkey_file, cert_file, cert_file, "");
auto context = device->createContext(0, 2);
auto& pair0 = context->createPair(0);
auto addrBytes0 = pair0->address().bytes();
auto& pair1 = context->createPair(1);
auto addrBytes1 = pair1->address().bytes();

bool exception_thrown = false;
spawnThreads(2, [&](int rank) {
try {
if (rank == 0) {
pair0->connect(addrBytes1);
} else {
pair1->connect(addrBytes0);
}
} catch (::gloo::IoException e) {
exception_thrown = true;
ASSERT_THAT(e.what(), ::testing::ContainsRegex("unknown ca"));
} catch (::gloo::EnforceNotMet e) {
exception_thrown = true;
ASSERT_THAT(
e.what(), ::testing::ContainsRegex("handshake was not succeeded"));
}
});

ASSERT_TRUE(exception_thrown);
}

} // namespace
} // namespace test
} // namespace gloo
#endif

0 comments on commit a8c8506

Please sign in to comment.