diff options
author | bulatman <bulatman@yandex-team.ru> | 2022-02-10 16:45:50 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:50 +0300 |
commit | 2f6ca198245aeffd5e2d82b65927c2465b68b4f5 (patch) | |
tree | 9142afc54d335ea52910662635b898e79e192e49 /library/cpp/testing/common | |
parent | 6560e4993b14d193f8c879e33a3de5e5eba6e21d (diff) | |
download | ydb-2f6ca198245aeffd5e2d82b65927c2465b68b4f5.tar.gz |
Restoring authorship annotation for <bulatman@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/testing/common')
-rw-r--r-- | library/cpp/testing/common/env.cpp | 36 | ||||
-rw-r--r-- | library/cpp/testing/common/env.h | 44 | ||||
-rw-r--r-- | library/cpp/testing/common/network.cpp | 406 | ||||
-rw-r--r-- | library/cpp/testing/common/network.h | 100 | ||||
-rw-r--r-- | library/cpp/testing/common/probe.cpp | 2 | ||||
-rw-r--r-- | library/cpp/testing/common/probe.h | 264 | ||||
-rw-r--r-- | library/cpp/testing/common/scope.cpp | 2 | ||||
-rw-r--r-- | library/cpp/testing/common/scope.h | 78 | ||||
-rw-r--r-- | library/cpp/testing/common/ut/env_ut.cpp | 156 | ||||
-rw-r--r-- | library/cpp/testing/common/ut/network_ut.cpp | 108 | ||||
-rw-r--r-- | library/cpp/testing/common/ut/scope_ut.cpp | 56 | ||||
-rw-r--r-- | library/cpp/testing/common/ut/ya.make | 36 | ||||
-rw-r--r-- | library/cpp/testing/common/ya.make | 36 |
13 files changed, 662 insertions, 662 deletions
diff --git a/library/cpp/testing/common/env.cpp b/library/cpp/testing/common/env.cpp index a6acf90e2c..fa3a47fe16 100644 --- a/library/cpp/testing/common/env.cpp +++ b/library/cpp/testing/common/env.cpp @@ -1,5 +1,5 @@ -#include "env.h" - +#include "env.h" + #include <build/scripts/c_templates/svnversion.h> #include <util/folder/dirut.h> @@ -32,14 +32,14 @@ TString BuildRoot() { } } -TString ArcadiaFromCurrentLocation(TStringBuf where, TStringBuf path) { - return (TFsPath(ArcadiaSourceRoot()) / TFsPath(where).Parent() / path).Fix(); -} - -TString BinaryPath(TStringBuf path) { - return (TFsPath(BuildRoot()) / path).Fix(); -} - +TString ArcadiaFromCurrentLocation(TStringBuf where, TStringBuf path) { + return (TFsPath(ArcadiaSourceRoot()) / TFsPath(where).Parent() / path).Fix(); +} + +TString BinaryPath(TStringBuf path) { + return (TFsPath(BuildRoot()) / path).Fix(); +} + TString GetArcadiaTestsData() { TString atdRoot = NPrivate::GetTestEnv().ArcadiaTestsDataDir; if (atdRoot) { @@ -47,21 +47,21 @@ TString GetArcadiaTestsData() { } TString path = NPrivate::GetCwd(); - const char pathsep = GetDirectorySeparator(); + const char pathsep = GetDirectorySeparator(); while (!path.empty()) { TString dataDir = path + "/arcadia_tests_data"; - if (IsDir(dataDir)) { + if (IsDir(dataDir)) { return dataDir; - } + } - size_t pos = path.find_last_of(pathsep); - if (pos == TString::npos) { + size_t pos = path.find_last_of(pathsep); + if (pos == TString::npos) { pos = 0; - } + } path.erase(pos); } - return {}; + return {}; } TString GetWorkPath() { @@ -143,7 +143,7 @@ void StopProcessCoreWatching(int pid) { } } -bool FromYaTest() { +bool FromYaTest() { return NPrivate::GetTestEnv().IsRunningFromTest; } diff --git a/library/cpp/testing/common/env.h b/library/cpp/testing/common/env.h index 6846ba3016..7b89aa1bed 100644 --- a/library/cpp/testing/common/env.h +++ b/library/cpp/testing/common/env.h @@ -4,39 +4,39 @@ #include <util/folder/path.h> #include <util/generic/string.h> -#include <util/generic/strbuf.h> -#include <util/system/src_location.h> +#include <util/generic/strbuf.h> +#include <util/system/src_location.h> -// @brief return full path to arcadia root -TString ArcadiaSourceRoot(); +// @brief return full path to arcadia root +TString ArcadiaSourceRoot(); -// @brief return full path for file or folder specified by known source location `where` and `path` which is relative to parent folder of `where` +// @brief return full path for file or folder specified by known source location `where` and `path` which is relative to parent folder of `where` // for the instance: there is 2 files in folder test example_ut.cpp and example.data, so full path to test/example.data can be obtained -// from example_ut.cpp as ArcadiaFromCurrentLocation(__SOURCE_FILE__, "example.data") -TString ArcadiaFromCurrentLocation(TStringBuf where, TStringBuf path); +// from example_ut.cpp as ArcadiaFromCurrentLocation(__SOURCE_FILE__, "example.data") +TString ArcadiaFromCurrentLocation(TStringBuf where, TStringBuf path); -// @brief return build folder path -TString BuildRoot(); +// @brief return build folder path +TString BuildRoot(); -// @brief return full path to built artefact, where path is relative from arcadia root -TString BinaryPath(TStringBuf path); +// @brief return full path to built artefact, where path is relative from arcadia root +TString BinaryPath(TStringBuf path); -// @brief return true if environment is testenv otherwise false -bool FromYaTest(); +// @brief return true if environment is testenv otherwise false +bool FromYaTest(); -// @brief returns TestsData dir (from env:ARCADIA_TESTS_DATA_DIR or path to existing folder `arcadia_tests_data` within parent folders) -TString GetArcadiaTestsData(); +// @brief returns TestsData dir (from env:ARCADIA_TESTS_DATA_DIR or path to existing folder `arcadia_tests_data` within parent folders) +TString GetArcadiaTestsData(); -// @brief return current working dir (from env:TEST_WORK_PATH or cwd) -TString GetWorkPath(); +// @brief return current working dir (from env:TEST_WORK_PATH or cwd) +TString GetWorkPath(); -// @brief return tests output path (workdir + testing_out_stuff) -TFsPath GetOutputPath(); +// @brief return tests output path (workdir + testing_out_stuff) +TFsPath GetOutputPath(); -// @brief return path from env:YA_TEST_RAM_DRIVE_PATH +// @brief return path from env:YA_TEST_RAM_DRIVE_PATH const TString& GetRamDrivePath(); -// @brief return path from env:YA_TEST_OUTPUT_RAM_DRIVE_PATH +// @brief return path from env:YA_TEST_OUTPUT_RAM_DRIVE_PATH const TString& GetOutputRamDrivePath(); // @brief return test parameter by name. If not exists, return an empty string @@ -54,7 +54,7 @@ void WatchProcessCore(int pid, const TFsPath& binaryPath, const TFsPath& cwd = T // @brief mark the process as successfully completed - a test machinery won't try to recover core dump file for the process void StopProcessCoreWatching(int pid); -#define SRC_(path) ArcadiaFromCurrentLocation(__SOURCE_FILE__, path) +#define SRC_(path) ArcadiaFromCurrentLocation(__SOURCE_FILE__, path) namespace NPrivate { class TTestEnv { diff --git a/library/cpp/testing/common/network.cpp b/library/cpp/testing/common/network.cpp index b06670b8d0..230c50ee6d 100644 --- a/library/cpp/testing/common/network.cpp +++ b/library/cpp/testing/common/network.cpp @@ -1,208 +1,208 @@ -#include "network.h" - -#include <util/folder/dirut.h> -#include <util/folder/path.h> -#include <util/generic/singleton.h> -#include <util/generic/utility.h> -#include <util/generic/vector.h> -#include <util/generic/ylimits.h> -#include <util/network/address.h> -#include <util/network/sock.h> -#include <util/random/random.h> -#include <util/stream/file.h> -#include <util/string/split.h> -#include <util/system/env.h> -#include <util/system/error.h> -#include <util/system/file_lock.h> -#include <util/system/fs.h> - -#ifdef _darwin_ -#include <sys/types.h> -#include <sys/sysctl.h> -#endif - -namespace { -#define Y_VERIFY_SYSERROR(expr) \ - do { \ - if (!(expr)) { \ - Y_FAIL(#expr ", errno=%d", LastSystemError()); \ - } \ - } while (false) - - class TPortGuard : public NTesting::IPort { - public: - TPortGuard(ui16 port, THolder<TFileLock> lock) - : Lock_(std::move(lock)) - , Port_(port) - { - } - +#include "network.h" + +#include <util/folder/dirut.h> +#include <util/folder/path.h> +#include <util/generic/singleton.h> +#include <util/generic/utility.h> +#include <util/generic/vector.h> +#include <util/generic/ylimits.h> +#include <util/network/address.h> +#include <util/network/sock.h> +#include <util/random/random.h> +#include <util/stream/file.h> +#include <util/string/split.h> +#include <util/system/env.h> +#include <util/system/error.h> +#include <util/system/file_lock.h> +#include <util/system/fs.h> + +#ifdef _darwin_ +#include <sys/types.h> +#include <sys/sysctl.h> +#endif + +namespace { +#define Y_VERIFY_SYSERROR(expr) \ + do { \ + if (!(expr)) { \ + Y_FAIL(#expr ", errno=%d", LastSystemError()); \ + } \ + } while (false) + + class TPortGuard : public NTesting::IPort { + public: + TPortGuard(ui16 port, THolder<TFileLock> lock) + : Lock_(std::move(lock)) + , Port_(port) + { + } + ~TPortGuard() override { - Y_VERIFY_SYSERROR(NFs::Remove(Lock_->GetName())); - } - - ui16 Get() override { - return Port_; - } - - private: - THolder<TFileLock> Lock_; - ui16 Port_; - }; - - std::pair<ui16, ui16> GetEphemeralRange() { - // IANA suggestion - std::pair<ui16, ui16> pair{(1 << 15) + (1 << 14), (1 << 16) - 1}; - #ifdef _linux_ - if (NFs::Exists("/proc/sys/net/ipv4/ip_local_port_range")) { - TIFStream fileStream("/proc/sys/net/ipv4/ip_local_port_range"); - fileStream >> pair.first >> pair.second; - } - #endif - #ifdef _darwin_ - ui32 first, last; - size_t size; - sysctlbyname("net.inet.ip.portrange.first", &first, &size, NULL, 0); - sysctlbyname("net.inet.ip.portrange.last", &last, &size, NULL, 0); - pair.first = first; - pair.second = last; - #endif - return pair; - } - - TVector<std::pair<ui16, ui16>> GetPortRanges() { - TString givenRange = GetEnv("VALID_PORT_RANGE"); - TVector<std::pair<ui16, ui16>> ranges; - if (givenRange.Contains(':')) { - auto res = StringSplitter(givenRange).Split(':').Limit(2).ToList<TString>(); - ranges.emplace_back(FromString<ui16>(res.front()), FromString<ui16>(res.back())); - } else { - const ui16 firstValid = 1025; - const ui16 lastValid = Max<ui16>(); - - auto [firstEphemeral, lastEphemeral] = GetEphemeralRange(); - const ui16 firstInvalid = Max(firstEphemeral, firstValid); - const ui16 lastInvalid = Min(lastEphemeral, lastValid); - - if (firstInvalid > firstValid) - ranges.emplace_back(firstValid, firstInvalid - 1); - if (lastInvalid < lastValid) - ranges.emplace_back(lastInvalid + 1, lastValid); - } - return ranges; - } - - class TPortManager { - static constexpr size_t Retries = 20; - public: - TPortManager() - : SyncDir_(GetEnv("PORT_SYNC_PATH")) - , Ranges_(GetPortRanges()) - , TotalCount_(0) - { - if (!SyncDir_.IsDefined()) { - SyncDir_ = TFsPath(GetSystemTempDir()) / "yandex_port_locks"; - } - Y_VERIFY(SyncDir_.IsDefined()); - NFs::MakeDirectoryRecursive(SyncDir_); - - for (auto [left, right] : Ranges_) { - TotalCount_ += right - left; - } - Y_VERIFY(0 != TotalCount_); - } - - NTesting::TPortHolder GetFreePort() const { - ui16 salt = RandomNumber<ui16>(); - for (ui16 attempt = 0; attempt < TotalCount_; ++attempt) { - ui16 probe = (salt + attempt) % TotalCount_; - - for (auto [left, right] : Ranges_) { - if (probe >= right - left) - probe -= right - left; - else { - probe += left; - break; - } - } - - auto port = TryAcquirePort(probe); - if (port) { - return NTesting::TPortHolder{std::move(port)}; - } - } - - Y_FAIL("Cannot get free port!"); - } - - TVector<NTesting::TPortHolder> GetFreePortsRange(size_t count) const { - Y_VERIFY(count > 0); - TVector<NTesting::TPortHolder> ports(Reserve(count)); - for (size_t i = 0; i < Retries; ++i) { - for (auto[left, right] : Ranges_) { - if (right - left < count) { - continue; - } - ui16 start = left + RandomNumber<ui16>((right - left) / 2); - if (right - start < count) { - continue; - } - for (ui16 probe = start; probe < right; ++probe) { - auto port = TryAcquirePort(probe); - if (port) { - ports.emplace_back(std::move(port)); - } else { - ports.clear(); - } - if (ports.size() == count) { - return ports; - } - } - // Can't find required number of ports without gap in the current range - ports.clear(); - } - } - Y_FAIL("Cannot get range of %zu ports!", count); - } - - private: - THolder<NTesting::IPort> TryAcquirePort(ui16 port) const { - auto lock = MakeHolder<TFileLock>(TString(SyncDir_ / ::ToString(port))); - if (!lock->TryAcquire()) { - return nullptr; - } - - TInet6StreamSocket sock; - Y_VERIFY_SYSERROR(INVALID_SOCKET != static_cast<SOCKET>(sock)); - - TSockAddrInet6 addr("::", port); - if (sock.Bind(&addr) != 0) { - lock->Release(); - Y_VERIFY(EADDRINUSE == LastSystemError(), "unexpected error: %d", LastSystemError()); - return nullptr; - } - return MakeHolder<TPortGuard>(port, std::move(lock)); - } - - private: - TFsPath SyncDir_; - TVector<std::pair<ui16, ui16>> Ranges_; - size_t TotalCount_; - }; -} - -namespace NTesting { - TPortHolder GetFreePort() { - return Singleton<TPortManager>()->GetFreePort(); - } - - namespace NLegacy { - TVector<TPortHolder> GetFreePortsRange(size_t count) { - return Singleton<TPortManager>()->GetFreePortsRange(count); - } - } + Y_VERIFY_SYSERROR(NFs::Remove(Lock_->GetName())); + } + + ui16 Get() override { + return Port_; + } + + private: + THolder<TFileLock> Lock_; + ui16 Port_; + }; + + std::pair<ui16, ui16> GetEphemeralRange() { + // IANA suggestion + std::pair<ui16, ui16> pair{(1 << 15) + (1 << 14), (1 << 16) - 1}; + #ifdef _linux_ + if (NFs::Exists("/proc/sys/net/ipv4/ip_local_port_range")) { + TIFStream fileStream("/proc/sys/net/ipv4/ip_local_port_range"); + fileStream >> pair.first >> pair.second; + } + #endif + #ifdef _darwin_ + ui32 first, last; + size_t size; + sysctlbyname("net.inet.ip.portrange.first", &first, &size, NULL, 0); + sysctlbyname("net.inet.ip.portrange.last", &last, &size, NULL, 0); + pair.first = first; + pair.second = last; + #endif + return pair; + } + + TVector<std::pair<ui16, ui16>> GetPortRanges() { + TString givenRange = GetEnv("VALID_PORT_RANGE"); + TVector<std::pair<ui16, ui16>> ranges; + if (givenRange.Contains(':')) { + auto res = StringSplitter(givenRange).Split(':').Limit(2).ToList<TString>(); + ranges.emplace_back(FromString<ui16>(res.front()), FromString<ui16>(res.back())); + } else { + const ui16 firstValid = 1025; + const ui16 lastValid = Max<ui16>(); + + auto [firstEphemeral, lastEphemeral] = GetEphemeralRange(); + const ui16 firstInvalid = Max(firstEphemeral, firstValid); + const ui16 lastInvalid = Min(lastEphemeral, lastValid); + + if (firstInvalid > firstValid) + ranges.emplace_back(firstValid, firstInvalid - 1); + if (lastInvalid < lastValid) + ranges.emplace_back(lastInvalid + 1, lastValid); + } + return ranges; + } + + class TPortManager { + static constexpr size_t Retries = 20; + public: + TPortManager() + : SyncDir_(GetEnv("PORT_SYNC_PATH")) + , Ranges_(GetPortRanges()) + , TotalCount_(0) + { + if (!SyncDir_.IsDefined()) { + SyncDir_ = TFsPath(GetSystemTempDir()) / "yandex_port_locks"; + } + Y_VERIFY(SyncDir_.IsDefined()); + NFs::MakeDirectoryRecursive(SyncDir_); + + for (auto [left, right] : Ranges_) { + TotalCount_ += right - left; + } + Y_VERIFY(0 != TotalCount_); + } + + NTesting::TPortHolder GetFreePort() const { + ui16 salt = RandomNumber<ui16>(); + for (ui16 attempt = 0; attempt < TotalCount_; ++attempt) { + ui16 probe = (salt + attempt) % TotalCount_; + + for (auto [left, right] : Ranges_) { + if (probe >= right - left) + probe -= right - left; + else { + probe += left; + break; + } + } + + auto port = TryAcquirePort(probe); + if (port) { + return NTesting::TPortHolder{std::move(port)}; + } + } + + Y_FAIL("Cannot get free port!"); + } + + TVector<NTesting::TPortHolder> GetFreePortsRange(size_t count) const { + Y_VERIFY(count > 0); + TVector<NTesting::TPortHolder> ports(Reserve(count)); + for (size_t i = 0; i < Retries; ++i) { + for (auto[left, right] : Ranges_) { + if (right - left < count) { + continue; + } + ui16 start = left + RandomNumber<ui16>((right - left) / 2); + if (right - start < count) { + continue; + } + for (ui16 probe = start; probe < right; ++probe) { + auto port = TryAcquirePort(probe); + if (port) { + ports.emplace_back(std::move(port)); + } else { + ports.clear(); + } + if (ports.size() == count) { + return ports; + } + } + // Can't find required number of ports without gap in the current range + ports.clear(); + } + } + Y_FAIL("Cannot get range of %zu ports!", count); + } + + private: + THolder<NTesting::IPort> TryAcquirePort(ui16 port) const { + auto lock = MakeHolder<TFileLock>(TString(SyncDir_ / ::ToString(port))); + if (!lock->TryAcquire()) { + return nullptr; + } + + TInet6StreamSocket sock; + Y_VERIFY_SYSERROR(INVALID_SOCKET != static_cast<SOCKET>(sock)); + + TSockAddrInet6 addr("::", port); + if (sock.Bind(&addr) != 0) { + lock->Release(); + Y_VERIFY(EADDRINUSE == LastSystemError(), "unexpected error: %d", LastSystemError()); + return nullptr; + } + return MakeHolder<TPortGuard>(port, std::move(lock)); + } + + private: + TFsPath SyncDir_; + TVector<std::pair<ui16, ui16>> Ranges_; + size_t TotalCount_; + }; +} + +namespace NTesting { + TPortHolder GetFreePort() { + return Singleton<TPortManager>()->GetFreePort(); + } + + namespace NLegacy { + TVector<TPortHolder> GetFreePortsRange(size_t count) { + return Singleton<TPortManager>()->GetFreePortsRange(count); + } + } IOutputStream& operator<<(IOutputStream& out, const TPortHolder& port) { return out << static_cast<ui16>(port); } -} +} diff --git a/library/cpp/testing/common/network.h b/library/cpp/testing/common/network.h index 66fb2b0c40..eb4d32f3a1 100644 --- a/library/cpp/testing/common/network.h +++ b/library/cpp/testing/common/network.h @@ -1,52 +1,52 @@ -#pragma once - -#include <util/generic/ptr.h> -#include <util/generic/vector.h> - -namespace NTesting { - - //@brief network port holder interface - class IPort { - public: - virtual ~IPort() {} - - virtual ui16 Get() = 0; - }; - - class TPortHolder : private THolder<IPort> { - using TBase = THolder<IPort>; - public: - using TBase::TBase; - using TBase::Release; - using TBase::Reset; - - operator ui16() const& { - return (*this)->Get(); - } - - operator ui16() const&& = delete; - }; - +#pragma once + +#include <util/generic/ptr.h> +#include <util/generic/vector.h> + +namespace NTesting { + + //@brief network port holder interface + class IPort { + public: + virtual ~IPort() {} + + virtual ui16 Get() = 0; + }; + + class TPortHolder : private THolder<IPort> { + using TBase = THolder<IPort>; + public: + using TBase::TBase; + using TBase::Release; + using TBase::Reset; + + operator ui16() const& { + return (*this)->Get(); + } + + operator ui16() const&& = delete; + }; + IOutputStream& operator<<(IOutputStream& out, const TPortHolder& port); - //@brief Get first free port. - [[nodiscard]] TPortHolder GetFreePort(); - - namespace NLegacy { - // Do not use this method, it needs only for TPortManager from unittests. - // Returns continuous sequence of the specified number of ports. - [[nodiscard]] TVector<TPortHolder> GetFreePortsRange(size_t count); - } - - //@brief helper class for inheritance - struct TFreePortOwner { - TFreePortOwner() : Port_(GetFreePort()) {} - - ui16 GetPort() const { - return Port_; - } - - private: - TPortHolder Port_; - }; -} + //@brief Get first free port. + [[nodiscard]] TPortHolder GetFreePort(); + + namespace NLegacy { + // Do not use this method, it needs only for TPortManager from unittests. + // Returns continuous sequence of the specified number of ports. + [[nodiscard]] TVector<TPortHolder> GetFreePortsRange(size_t count); + } + + //@brief helper class for inheritance + struct TFreePortOwner { + TFreePortOwner() : Port_(GetFreePort()) {} + + ui16 GetPort() const { + return Port_; + } + + private: + TPortHolder Port_; + }; +} diff --git a/library/cpp/testing/common/probe.cpp b/library/cpp/testing/common/probe.cpp index c0d7f46ba6..73f2fb6360 100644 --- a/library/cpp/testing/common/probe.cpp +++ b/library/cpp/testing/common/probe.cpp @@ -1 +1 @@ -#include "probe.h" +#include "probe.h" diff --git a/library/cpp/testing/common/probe.h b/library/cpp/testing/common/probe.h index ba5a83a83e..19910979b5 100644 --- a/library/cpp/testing/common/probe.h +++ b/library/cpp/testing/common/probe.h @@ -1,140 +1,140 @@ #pragma once -#include <util/system/yassert.h> +#include <util/system/yassert.h> namespace NTesting { - //////////////////////////////////////////////////////////////////////////////// - - // Below there is a serie of probe classes for testing construction/destruction copying/moving of class. - // for examples see tests in probe_ut.cpp - - struct TProbeState { - int Constructors = 0; - int Destructors = 0; - int ShadowDestructors = 0; - int CopyConstructors = 0; - int CopyAssignments = 0; - int MoveConstructors = 0; - int MoveAssignments = 0; - int Touches = 0; - - TProbeState() = default; - - void Reset() { - *this = TProbeState{}; - } - }; - - // Used for probing the number of copies that occur if a type must be coerced. - class TCoercibleToProbe { - public: - TProbeState* State; - TProbeState* ShadowState; - - public: - explicit TCoercibleToProbe(TProbeState* state) - : State(state) - , ShadowState(state) - {} - - private: - TCoercibleToProbe(const TCoercibleToProbe&); - TCoercibleToProbe(TCoercibleToProbe&&); - TCoercibleToProbe& operator=(const TCoercibleToProbe&); - TCoercibleToProbe& operator=(TCoercibleToProbe&&); - }; - - // Used for probing the number of copies in an argument. - class TProbe { - public: - TProbeState* State; - TProbeState* ShadowState; - - public: - static TProbe ExplicitlyCreateInvalidProbe() { - return TProbe(); - } - - explicit TProbe(TProbeState* state) - : State(state) - , ShadowState(state) - { - Y_ASSERT(State); - ++State->Constructors; - } - - ~TProbe() { - if (State) { - ++State->Destructors; - } - if (ShadowState) { - ++ShadowState->ShadowDestructors; - } - } - - TProbe(const TProbe& other) - : State(other.State) - , ShadowState(other.ShadowState) - { - Y_ASSERT(State); - ++State->CopyConstructors; - } - - TProbe(TProbe&& other) - : State(other.State) - , ShadowState(other.ShadowState) - { - Y_ASSERT(State); - other.State = nullptr; - ++State->MoveConstructors; - } - - TProbe(const TCoercibleToProbe& other) - : State(other.State) - , ShadowState(other.ShadowState) - { - Y_ASSERT(State); - ++State->CopyConstructors; + //////////////////////////////////////////////////////////////////////////////// + + // Below there is a serie of probe classes for testing construction/destruction copying/moving of class. + // for examples see tests in probe_ut.cpp + + struct TProbeState { + int Constructors = 0; + int Destructors = 0; + int ShadowDestructors = 0; + int CopyConstructors = 0; + int CopyAssignments = 0; + int MoveConstructors = 0; + int MoveAssignments = 0; + int Touches = 0; + + TProbeState() = default; + + void Reset() { + *this = TProbeState{}; + } + }; + + // Used for probing the number of copies that occur if a type must be coerced. + class TCoercibleToProbe { + public: + TProbeState* State; + TProbeState* ShadowState; + + public: + explicit TCoercibleToProbe(TProbeState* state) + : State(state) + , ShadowState(state) + {} + + private: + TCoercibleToProbe(const TCoercibleToProbe&); + TCoercibleToProbe(TCoercibleToProbe&&); + TCoercibleToProbe& operator=(const TCoercibleToProbe&); + TCoercibleToProbe& operator=(TCoercibleToProbe&&); + }; + + // Used for probing the number of copies in an argument. + class TProbe { + public: + TProbeState* State; + TProbeState* ShadowState; + + public: + static TProbe ExplicitlyCreateInvalidProbe() { + return TProbe(); + } + + explicit TProbe(TProbeState* state) + : State(state) + , ShadowState(state) + { + Y_ASSERT(State); + ++State->Constructors; + } + + ~TProbe() { + if (State) { + ++State->Destructors; + } + if (ShadowState) { + ++ShadowState->ShadowDestructors; + } + } + + TProbe(const TProbe& other) + : State(other.State) + , ShadowState(other.ShadowState) + { + Y_ASSERT(State); + ++State->CopyConstructors; + } + + TProbe(TProbe&& other) + : State(other.State) + , ShadowState(other.ShadowState) + { + Y_ASSERT(State); + other.State = nullptr; + ++State->MoveConstructors; + } + + TProbe(const TCoercibleToProbe& other) + : State(other.State) + , ShadowState(other.ShadowState) + { + Y_ASSERT(State); + ++State->CopyConstructors; + } + + TProbe(TCoercibleToProbe&& other) + : State(other.State) + , ShadowState(other.ShadowState) + { + Y_ASSERT(State); + other.State = nullptr; + ++State->MoveConstructors; + } + + TProbe& operator=(const TProbe& other) { + State = other.State; + ShadowState = other.ShadowState; + Y_ASSERT(State); + ++State->CopyAssignments; + return *this; + } + + TProbe& operator=(TProbe&& other) { + State = other.State; + ShadowState = other.ShadowState; + Y_ASSERT(State); + other.State = nullptr; + ++State->MoveAssignments; + return *this; } - - TProbe(TCoercibleToProbe&& other) - : State(other.State) - , ShadowState(other.ShadowState) - { - Y_ASSERT(State); - other.State = nullptr; - ++State->MoveConstructors; + + void Touch() const { + Y_ASSERT(State); + ++State->Touches; + } + + bool IsValid() const { + return nullptr != State; } - TProbe& operator=(const TProbe& other) { - State = other.State; - ShadowState = other.ShadowState; - Y_ASSERT(State); - ++State->CopyAssignments; - return *this; - } - - TProbe& operator=(TProbe&& other) { - State = other.State; - ShadowState = other.ShadowState; - Y_ASSERT(State); - other.State = nullptr; - ++State->MoveAssignments; - return *this; - } - - void Touch() const { - Y_ASSERT(State); - ++State->Touches; - } - - bool IsValid() const { - return nullptr != State; - } - - private: - TProbe() - : State(nullptr) - {} - }; + private: + TProbe() + : State(nullptr) + {} + }; } // namespace NTesting diff --git a/library/cpp/testing/common/scope.cpp b/library/cpp/testing/common/scope.cpp index 5efa7cb0d2..c70d695c1b 100644 --- a/library/cpp/testing/common/scope.cpp +++ b/library/cpp/testing/common/scope.cpp @@ -1 +1 @@ -#include "scope.h" +#include "scope.h" diff --git a/library/cpp/testing/common/scope.h b/library/cpp/testing/common/scope.h index af12bda3c4..a2ca0e77e4 100644 --- a/library/cpp/testing/common/scope.h +++ b/library/cpp/testing/common/scope.h @@ -1,39 +1,39 @@ -#pragma once - -#include <util/generic/string.h> -#include <util/generic/vector.h> -#include <util/system/env.h> - -#include <utility> - -namespace NTesting { - // @brief Assigns new values to the given environment variables and restores old values upon destruction. - // @note if there was no env variable with given name, it will be set to empty string upon destruction IGNIETFERRO-1486 - struct TScopedEnvironment { - TScopedEnvironment(const TString& name, const TString& value) - : PreviousState{1, {name, ::GetEnv(name)}} - { - ::SetEnv(name, value); - } - - TScopedEnvironment(const TVector<std::pair<TString, TString>>& vars) - : PreviousState(Reserve(vars.size())) - { - for (const auto& [k, v] : vars) { - PreviousState.emplace_back(k, ::GetEnv(k)); - ::SetEnv(k, v); - } - } - - ~TScopedEnvironment() { - for (const auto& [k, v] : PreviousState) { - ::SetEnv(k, v); - } - } - - TScopedEnvironment(const TScopedEnvironment&) = delete; - TScopedEnvironment& operator=(const TScopedEnvironment&) = delete; - private: - TVector<std::pair<TString, TString>> PreviousState; - }; -} +#pragma once + +#include <util/generic/string.h> +#include <util/generic/vector.h> +#include <util/system/env.h> + +#include <utility> + +namespace NTesting { + // @brief Assigns new values to the given environment variables and restores old values upon destruction. + // @note if there was no env variable with given name, it will be set to empty string upon destruction IGNIETFERRO-1486 + struct TScopedEnvironment { + TScopedEnvironment(const TString& name, const TString& value) + : PreviousState{1, {name, ::GetEnv(name)}} + { + ::SetEnv(name, value); + } + + TScopedEnvironment(const TVector<std::pair<TString, TString>>& vars) + : PreviousState(Reserve(vars.size())) + { + for (const auto& [k, v] : vars) { + PreviousState.emplace_back(k, ::GetEnv(k)); + ::SetEnv(k, v); + } + } + + ~TScopedEnvironment() { + for (const auto& [k, v] : PreviousState) { + ::SetEnv(k, v); + } + } + + TScopedEnvironment(const TScopedEnvironment&) = delete; + TScopedEnvironment& operator=(const TScopedEnvironment&) = delete; + private: + TVector<std::pair<TString, TString>> PreviousState; + }; +} diff --git a/library/cpp/testing/common/ut/env_ut.cpp b/library/cpp/testing/common/ut/env_ut.cpp index 889e8073e8..2aed1e4a25 100644 --- a/library/cpp/testing/common/ut/env_ut.cpp +++ b/library/cpp/testing/common/ut/env_ut.cpp @@ -1,104 +1,104 @@ -#include <library/cpp/testing/common/env.h> -#include <library/cpp/testing/common/scope.h> +#include <library/cpp/testing/common/env.h> +#include <library/cpp/testing/common/scope.h> #include <library/cpp/testing/gtest/gtest.h> - -#include <util/folder/dirut.h> + +#include <util/folder/dirut.h> #include <util/stream/file.h> -#include <util/system/env.h> -#include <util/system/execpath.h> +#include <util/system/env.h> +#include <util/system/execpath.h> #include <util/system/fs.h> - - -TEST(Runtime, ArcadiaSourceRoot) { + + +TEST(Runtime, ArcadiaSourceRoot) { NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename - { - auto tmpDir = ::GetSystemTempDir(); - NTesting::TScopedEnvironment guard("ARCADIA_SOURCE_ROOT", tmpDir); + { + auto tmpDir = ::GetSystemTempDir(); + NTesting::TScopedEnvironment guard("ARCADIA_SOURCE_ROOT", tmpDir); Singleton<NPrivate::TTestEnv>()->ReInitialize(); - EXPECT_EQ(tmpDir, ArcadiaSourceRoot()); - } - { - NTesting::TScopedEnvironment guard("ARCADIA_SOURCE_ROOT", ""); + EXPECT_EQ(tmpDir, ArcadiaSourceRoot()); + } + { + NTesting::TScopedEnvironment guard("ARCADIA_SOURCE_ROOT", ""); Singleton<NPrivate::TTestEnv>()->ReInitialize(); - EXPECT_FALSE(ArcadiaSourceRoot().empty()); - } -} - -TEST(Runtime, BuildRoot) { + EXPECT_FALSE(ArcadiaSourceRoot().empty()); + } +} + +TEST(Runtime, BuildRoot) { NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename - { - auto tmpDir = ::GetSystemTempDir(); - NTesting::TScopedEnvironment guard("ARCADIA_BUILD_ROOT", tmpDir); + { + auto tmpDir = ::GetSystemTempDir(); + NTesting::TScopedEnvironment guard("ARCADIA_BUILD_ROOT", tmpDir); Singleton<NPrivate::TTestEnv>()->ReInitialize(); - EXPECT_EQ(tmpDir, BuildRoot()); - } - { - NTesting::TScopedEnvironment guard("ARCADIA_BUILD_ROOT", ""); + EXPECT_EQ(tmpDir, BuildRoot()); + } + { + NTesting::TScopedEnvironment guard("ARCADIA_BUILD_ROOT", ""); Singleton<NPrivate::TTestEnv>()->ReInitialize(); - EXPECT_FALSE(BuildRoot().empty()); - } -} - -TEST(Runtime, BinaryPath) { + EXPECT_FALSE(BuildRoot().empty()); + } +} + +TEST(Runtime, BinaryPath) { NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename Singleton<NPrivate::TTestEnv>()->ReInitialize(); - EXPECT_TRUE(TFsPath(BinaryPath("library/cpp/testing/common/ut")).Exists()); -} - -TEST(Runtime, GetArcadiaTestsData) { + EXPECT_TRUE(TFsPath(BinaryPath("library/cpp/testing/common/ut")).Exists()); +} + +TEST(Runtime, GetArcadiaTestsData) { NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename - { - auto tmpDir = ::GetSystemTempDir(); - NTesting::TScopedEnvironment guard("ARCADIA_TESTS_DATA_DIR", tmpDir); + { + auto tmpDir = ::GetSystemTempDir(); + NTesting::TScopedEnvironment guard("ARCADIA_TESTS_DATA_DIR", tmpDir); Singleton<NPrivate::TTestEnv>()->ReInitialize(); - EXPECT_EQ(tmpDir, GetArcadiaTestsData()); - } - { - NTesting::TScopedEnvironment guard("ARCADIA_TESTS_DATA_DIR", ""); + EXPECT_EQ(tmpDir, GetArcadiaTestsData()); + } + { + NTesting::TScopedEnvironment guard("ARCADIA_TESTS_DATA_DIR", ""); Singleton<NPrivate::TTestEnv>()->ReInitialize(); - auto path = GetArcadiaTestsData(); - // it is not error if path is empty - const bool ok = (path.empty() || GetBaseName(path) == "arcadia_tests_data"); - EXPECT_TRUE(ok); - } -} - -TEST(Runtime, GetWorkPath) { + auto path = GetArcadiaTestsData(); + // it is not error if path is empty + const bool ok = (path.empty() || GetBaseName(path) == "arcadia_tests_data"); + EXPECT_TRUE(ok); + } +} + +TEST(Runtime, GetWorkPath) { NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename - { - auto tmpDir = ::GetSystemTempDir(); - NTesting::TScopedEnvironment guard("TEST_WORK_PATH", tmpDir); + { + auto tmpDir = ::GetSystemTempDir(); + NTesting::TScopedEnvironment guard("TEST_WORK_PATH", tmpDir); Singleton<NPrivate::TTestEnv>()->ReInitialize(); - EXPECT_EQ(tmpDir, GetWorkPath()); - } - { - NTesting::TScopedEnvironment guard("TEST_WORK_PATH", ""); + EXPECT_EQ(tmpDir, GetWorkPath()); + } + { + NTesting::TScopedEnvironment guard("TEST_WORK_PATH", ""); Singleton<NPrivate::TTestEnv>()->ReInitialize(); - EXPECT_TRUE(!GetWorkPath().empty()); - } -} - -TEST(Runtime, GetOutputPath) { + EXPECT_TRUE(!GetWorkPath().empty()); + } +} + +TEST(Runtime, GetOutputPath) { NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename Singleton<NPrivate::TTestEnv>()->ReInitialize(); - EXPECT_EQ(GetOutputPath().Basename(), "testing_out_stuff"); -} - -TEST(Runtime, GetRamDrivePath) { + EXPECT_EQ(GetOutputPath().Basename(), "testing_out_stuff"); +} + +TEST(Runtime, GetRamDrivePath) { NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename - auto tmpDir = ::GetSystemTempDir(); - NTesting::TScopedEnvironment guard("YA_TEST_RAM_DRIVE_PATH", tmpDir); + auto tmpDir = ::GetSystemTempDir(); + NTesting::TScopedEnvironment guard("YA_TEST_RAM_DRIVE_PATH", tmpDir); Singleton<NPrivate::TTestEnv>()->ReInitialize(); - EXPECT_EQ(tmpDir, GetRamDrivePath()); -} - -TEST(Runtime, GetOutputRamDrivePath) { + EXPECT_EQ(tmpDir, GetRamDrivePath()); +} + +TEST(Runtime, GetOutputRamDrivePath) { NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename - auto tmpDir = ::GetSystemTempDir(); - NTesting::TScopedEnvironment guard("YA_TEST_OUTPUT_RAM_DRIVE_PATH", tmpDir); + auto tmpDir = ::GetSystemTempDir(); + NTesting::TScopedEnvironment guard("YA_TEST_OUTPUT_RAM_DRIVE_PATH", tmpDir); Singleton<NPrivate::TTestEnv>()->ReInitialize(); - EXPECT_EQ(tmpDir, GetOutputRamDrivePath()); -} + EXPECT_EQ(tmpDir, GetOutputRamDrivePath()); +} #ifdef _linux_ TEST(Runtime, GdbPath) { diff --git a/library/cpp/testing/common/ut/network_ut.cpp b/library/cpp/testing/common/ut/network_ut.cpp index 11ba48fd5d..6a40775fd9 100644 --- a/library/cpp/testing/common/ut/network_ut.cpp +++ b/library/cpp/testing/common/ut/network_ut.cpp @@ -1,54 +1,54 @@ -#include <library/cpp/testing/common/network.h> -#include <library/cpp/testing/common/scope.h> - -#include <util/generic/hash_set.h> - -#include <util/folder/dirut.h> -#include <util/folder/path.h> -#include <util/folder/tempdir.h> -#include <util/network/sock.h> -#include <util/system/fs.h> - -#include <library/cpp/testing/gtest/gtest.h> - -static TTempDir TmpDir; - -TEST(NetworkTest, FreePort) { - NTesting::TScopedEnvironment envGuard("PORT_SYNC_PATH", TmpDir.Name()); - - TVector<NTesting::TPortHolder> ports(Reserve(100)); - - for (size_t i = 0; i < 100; ++i) { - ports.push_back(NTesting::GetFreePort()); - } - - THashSet<ui16> uniqPorts; - for (auto& port : ports) { - const TString guardPath = TmpDir.Path() / ToString(static_cast<ui16>(port)); - EXPECT_TRUE(NFs::Exists(guardPath)); - EXPECT_TRUE(uniqPorts.emplace(port).second); - - TInetStreamSocket sock; - TSockAddrInet addr(TIpHost{INADDR_ANY}, port); - ASSERT_EQ(0, SetSockOpt(sock, SOL_SOCKET, SO_REUSEADDR, 1)); - EXPECT_EQ(0, sock.Bind(&addr)); - } - ports.clear(); - for (ui16 port : uniqPorts) { - const TString guardPath = TmpDir.Path() / ToString(port); - EXPECT_FALSE(NFs::Exists(guardPath)); - } -} - - -TEST(FreePortTest, FreePortsRange) { - NTesting::TScopedEnvironment envGuard("PORT_SYNC_PATH", TmpDir.Name()); - - for (ui16 i = 2; i < 10; ++i) { - TVector<NTesting::TPortHolder> ports = NTesting::NLegacy::GetFreePortsRange(i); - ASSERT_EQ(i, ports.size()); - for (ui16 j = 1; j < i; ++j) { - EXPECT_EQ(static_cast<ui16>(ports[j]), static_cast<ui16>(ports[0]) + j); - } - } -} +#include <library/cpp/testing/common/network.h> +#include <library/cpp/testing/common/scope.h> + +#include <util/generic/hash_set.h> + +#include <util/folder/dirut.h> +#include <util/folder/path.h> +#include <util/folder/tempdir.h> +#include <util/network/sock.h> +#include <util/system/fs.h> + +#include <library/cpp/testing/gtest/gtest.h> + +static TTempDir TmpDir; + +TEST(NetworkTest, FreePort) { + NTesting::TScopedEnvironment envGuard("PORT_SYNC_PATH", TmpDir.Name()); + + TVector<NTesting::TPortHolder> ports(Reserve(100)); + + for (size_t i = 0; i < 100; ++i) { + ports.push_back(NTesting::GetFreePort()); + } + + THashSet<ui16> uniqPorts; + for (auto& port : ports) { + const TString guardPath = TmpDir.Path() / ToString(static_cast<ui16>(port)); + EXPECT_TRUE(NFs::Exists(guardPath)); + EXPECT_TRUE(uniqPorts.emplace(port).second); + + TInetStreamSocket sock; + TSockAddrInet addr(TIpHost{INADDR_ANY}, port); + ASSERT_EQ(0, SetSockOpt(sock, SOL_SOCKET, SO_REUSEADDR, 1)); + EXPECT_EQ(0, sock.Bind(&addr)); + } + ports.clear(); + for (ui16 port : uniqPorts) { + const TString guardPath = TmpDir.Path() / ToString(port); + EXPECT_FALSE(NFs::Exists(guardPath)); + } +} + + +TEST(FreePortTest, FreePortsRange) { + NTesting::TScopedEnvironment envGuard("PORT_SYNC_PATH", TmpDir.Name()); + + for (ui16 i = 2; i < 10; ++i) { + TVector<NTesting::TPortHolder> ports = NTesting::NLegacy::GetFreePortsRange(i); + ASSERT_EQ(i, ports.size()); + for (ui16 j = 1; j < i; ++j) { + EXPECT_EQ(static_cast<ui16>(ports[j]), static_cast<ui16>(ports[0]) + j); + } + } +} diff --git a/library/cpp/testing/common/ut/scope_ut.cpp b/library/cpp/testing/common/ut/scope_ut.cpp index 4bbbd6b41c..4fb82c2466 100644 --- a/library/cpp/testing/common/ut/scope_ut.cpp +++ b/library/cpp/testing/common/ut/scope_ut.cpp @@ -1,28 +1,28 @@ -#include <library/cpp/testing/common/scope.h> - -#include <util/system/env.h> - -#include <library/cpp/testing/gtest/gtest.h> - -TEST(TScopedEnvironment, SingleValue) { - auto before = GetEnv("ARCADIA_SOURCE_ROOT"); - { - NTesting::TScopedEnvironment guard("ARCADIA_SOURCE_ROOT", "source"); - EXPECT_EQ("source", GetEnv("ARCADIA_SOURCE_ROOT")); - } - EXPECT_EQ(before, GetEnv("ARCADIA_SOURCE_ROOT")); -} - -TEST(TScopedEnvironment, MultiValue) { - TVector<TString> before{GetEnv("ARCADIA_SOURCE_ROOT"), GetEnv("ARCADIA_BUILD_ROOT")}; - { - NTesting::TScopedEnvironment guard{{ - {"ARCADIA_SOURCE_ROOT", "source"}, - {"ARCADIA_BUILD_ROOT", "build"}, - }}; - EXPECT_EQ("source", GetEnv("ARCADIA_SOURCE_ROOT")); - EXPECT_EQ("build", GetEnv("ARCADIA_BUILD_ROOT")); - } - TVector<TString> after{GetEnv("ARCADIA_SOURCE_ROOT"), GetEnv("ARCADIA_BUILD_ROOT")}; - EXPECT_EQ(before, after); -} +#include <library/cpp/testing/common/scope.h> + +#include <util/system/env.h> + +#include <library/cpp/testing/gtest/gtest.h> + +TEST(TScopedEnvironment, SingleValue) { + auto before = GetEnv("ARCADIA_SOURCE_ROOT"); + { + NTesting::TScopedEnvironment guard("ARCADIA_SOURCE_ROOT", "source"); + EXPECT_EQ("source", GetEnv("ARCADIA_SOURCE_ROOT")); + } + EXPECT_EQ(before, GetEnv("ARCADIA_SOURCE_ROOT")); +} + +TEST(TScopedEnvironment, MultiValue) { + TVector<TString> before{GetEnv("ARCADIA_SOURCE_ROOT"), GetEnv("ARCADIA_BUILD_ROOT")}; + { + NTesting::TScopedEnvironment guard{{ + {"ARCADIA_SOURCE_ROOT", "source"}, + {"ARCADIA_BUILD_ROOT", "build"}, + }}; + EXPECT_EQ("source", GetEnv("ARCADIA_SOURCE_ROOT")); + EXPECT_EQ("build", GetEnv("ARCADIA_BUILD_ROOT")); + } + TVector<TString> after{GetEnv("ARCADIA_SOURCE_ROOT"), GetEnv("ARCADIA_BUILD_ROOT")}; + EXPECT_EQ(before, after); +} diff --git a/library/cpp/testing/common/ut/ya.make b/library/cpp/testing/common/ut/ya.make index fbe866c479..053aa38079 100644 --- a/library/cpp/testing/common/ut/ya.make +++ b/library/cpp/testing/common/ut/ya.make @@ -1,19 +1,19 @@ GTEST() -OWNER( - amatanhead - bulatman - thegeorg - g:cpp-contrib -) - -SRCS( - env_ut.cpp - network_ut.cpp - scope_ut.cpp -) - -PEERDIR( - library/cpp/testing/common -) - -END() +OWNER( + amatanhead + bulatman + thegeorg + g:cpp-contrib +) + +SRCS( + env_ut.cpp + network_ut.cpp + scope_ut.cpp +) + +PEERDIR( + library/cpp/testing/common +) + +END() diff --git a/library/cpp/testing/common/ya.make b/library/cpp/testing/common/ya.make index 1c3b68328e..2f4b0ce26e 100644 --- a/library/cpp/testing/common/ya.make +++ b/library/cpp/testing/common/ya.make @@ -1,23 +1,23 @@ -LIBRARY() - -OWNER( - amatanhead - bulatman - thegeorg - g:cpp-contrib -) - -SRCS( - env.cpp - network.cpp +LIBRARY() + +OWNER( + amatanhead + bulatman + thegeorg + g:cpp-contrib +) + +SRCS( + env.cpp + network.cpp probe.cpp - scope.cpp -) - + scope.cpp +) + PEERDIR( library/cpp/json ) -END() - -RECURSE_FOR_TESTS(ut) +END() + +RECURSE_FOR_TESTS(ut) |