yt_unittest_lib.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #pragma once
  2. #include <yt/cpp/mapreduce/interface/logging/logger.h>
  3. #include <yt/cpp/mapreduce/interface/client.h>
  4. #include <yt/cpp/mapreduce/interface/config.h>
  5. #include <library/cpp/yson/node/node_io.h>
  6. #include <util/generic/bt_exception.h>
  7. #include <util/datetime/base.h>
  8. ////////////////////////////////////////////////////////////////////////////////
  9. template <>
  10. void Out<NYT::TNode>(IOutputStream& s, const NYT::TNode& node);
  11. template <>
  12. void Out<TGUID>(IOutputStream& s, const TGUID& guid);
  13. ////////////////////////////////////////////////////////////////////////////////
  14. namespace NYT::NTesting {
  15. ////////////////////////////////////////////////////////////////////////////////
  16. IClientPtr CreateTestClient(TString proxy = "", TCreateClientOptions options = {});
  17. // Create map node by unique path in Cypress and return that path.
  18. TYPath CreateTestDirectory(const IClientBasePtr& client);
  19. TString GenerateRandomData(size_t size, ui64 seed = 42);
  20. TVector<TNode> ReadTable(const IClientBasePtr& client, const TString& tablePath);
  21. void WriteTable(const IClientBasePtr& client, const TString& tablePath, const std::vector<TNode>& rowList);
  22. template <class TMessage>
  23. TVector<TMessage> ReadProtoTable(const IClientBasePtr& client, const TString& tablePath);
  24. template <class TMessage>
  25. void WriteProtoTable(const IClientBasePtr& client, const TString& tablePath, const std::vector<TMessage>& rowList);
  26. ////////////////////////////////////////////////////////////////////////////////
  27. // TODO: should be removed, usages should be replaced with TConfigSaverGuard
  28. class TZeroWaitLockPollIntervalGuard
  29. {
  30. public:
  31. TZeroWaitLockPollIntervalGuard();
  32. ~TZeroWaitLockPollIntervalGuard();
  33. private:
  34. TDuration OldWaitLockPollInterval_;
  35. };
  36. ////////////////////////////////////////////////////////////////////////////////
  37. class TConfigSaverGuard
  38. {
  39. public:
  40. TConfigSaverGuard();
  41. ~TConfigSaverGuard();
  42. private:
  43. TConfig Config_;
  44. };
  45. ////////////////////////////////////////////////////////////////////////////////
  46. class TDebugMetricDiff
  47. {
  48. public:
  49. TDebugMetricDiff(TString name);
  50. ui64 GetTotal() const;
  51. private:
  52. TString Name_;
  53. ui64 InitialValue_;
  54. };
  55. ////////////////////////////////////////////////////////////////////////////////
  56. struct TOwningYaMRRow
  57. {
  58. TString Key;
  59. TString SubKey;
  60. TString Value;
  61. TOwningYaMRRow(const TYaMRRow& row = {});
  62. TOwningYaMRRow(TString key, TString subKey, TString value);
  63. operator TYaMRRow() const;
  64. };
  65. bool operator == (const TOwningYaMRRow& row1, const TOwningYaMRRow& row2);
  66. ////////////////////////////////////////////////////////////////////////////////
  67. class TTestFixture
  68. {
  69. public:
  70. explicit TTestFixture(const TCreateClientOptions& options = {});
  71. ~TTestFixture();
  72. // Return precreated client.
  73. IClientPtr GetClient() const;
  74. // Return newly created client. Useful for cases:
  75. // - when we want to have multiple clients objects;
  76. // - when we want to control to control destruction of client object;
  77. IClientPtr CreateClient(const TCreateClientOptions& options = {}) const;
  78. IClientPtr CreateClientForUser(const TString& user, TCreateClientOptions options = {});
  79. TYPath GetWorkingDir() const;
  80. static TString GetYtProxy();
  81. private:
  82. TConfigSaverGuard ConfigGuard_;
  83. IClientPtr Client_;
  84. TYPath WorkingDir_;
  85. };
  86. ////////////////////////////////////////////////////////////////////////////////
  87. class TTabletFixture
  88. : public TTestFixture
  89. {
  90. public:
  91. TTabletFixture();
  92. private:
  93. void WaitForTabletCell();
  94. };
  95. ////////////////////////////////////////////////////////////////////////////////
  96. // Compares only columns and only "name" and "type" fields of columns.
  97. bool AreSchemasEqual(const TTableSchema& lhs, const TTableSchema& rhs);
  98. class TWaitFailedException
  99. : public TWithBackTrace<yexception>
  100. { };
  101. void WaitForPredicate(const std::function<bool()>& predicate, TDuration timeout = TDuration::Seconds(60));
  102. ////////////////////////////////////////////////////////////////////////////////
  103. // Redirects all the LOG_* calls with the corresponding level to `stream`.
  104. // Moreover, the LOG_* calls are delegated to `oldLogger`.
  105. class TStreamTeeLogger
  106. : public ILogger
  107. {
  108. public:
  109. TStreamTeeLogger(ELevel cutLevel, IOutputStream* stream, ILoggerPtr oldLogger);
  110. void Log(ELevel level, const ::TSourceLocation& sourceLocation, const char* format, va_list args) override;
  111. private:
  112. ILoggerPtr OldLogger_;
  113. IOutputStream* Stream_;
  114. ELevel Level_;
  115. };
  116. ////////////////////////////////////////////////////////////////////////////////
  117. template <typename T>
  118. TString ToYson(const T& x)
  119. {
  120. TNode result;
  121. TNodeBuilder builder(&result);
  122. Serialize(x, &builder);
  123. return NodeToYsonString(result);
  124. }
  125. ////////////////////////////////////////////////////////////////////////////////
  126. } // namespace NYT::NTesting
  127. ////////////////////////////////////////////////////////////////////////////////
  128. template <>
  129. void Out<NYT::NTesting::TOwningYaMRRow>(IOutputStream& out, const NYT::NTesting::TOwningYaMRRow& row);
  130. ////////////////////////////////////////////////////////////////////////////////
  131. // for UNITTEST()
  132. #define ASSERT_SERIALIZABLES_EQUAL(a, b) \
  133. UNIT_ASSERT_EQUAL_C(a, b, NYT::NTesting::ToYson(a) << " != " << NYT::NTesting::ToYson(b))
  134. #define ASSERT_SERIALIZABLES_UNEQUAL(a, b) \
  135. UNIT_ASSERT_UNEQUAL_C(a, b, NYT::NTesting::ToYson(a) << " == " << NYT::NTesting::ToYson(b))
  136. // for GTEST()
  137. #define ASSERT_SERIALIZABLES_EQ(a, b) \
  138. ASSERT_EQ(a, b) << NYT::NTesting::ToYson(a) << " != " << NYT::NTesting::ToYson(b)
  139. #define ASSERT_SERIALIZABLES_NE(a, b) \
  140. ASSERT_NE(a, b) << NYT::NTesting::ToYson(a) << " == " << NYT::NTesting::ToYson(b)
  141. #define YT_UNITTEST_LIB_H_
  142. #include "yt_unittest_lib-inl.h"
  143. #undef YT_UNITTEST_LIB_H_