end_of_stream_ut.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <yt/cpp/mapreduce/io/node_table_reader.h>
  2. #include <library/cpp/testing/gtest/gtest.h>
  3. using namespace NYT;
  4. ////////////////////////////////////////////////////////////////////////////////
  5. class TStringRawTableReader
  6. : public TRawTableReader
  7. {
  8. public:
  9. TStringRawTableReader(const TString& string)
  10. : String_(string)
  11. , Stream_(String_)
  12. { }
  13. bool Retry(const TMaybe<ui32>&, const TMaybe<ui64>&, const std::exception_ptr&) override
  14. {
  15. return false;
  16. }
  17. void ResetRetries() override
  18. { }
  19. bool HasRangeIndices() const override
  20. {
  21. return false;
  22. }
  23. private:
  24. size_t DoRead(void* buf, size_t len) override
  25. {
  26. return Stream_.Read(buf, len);
  27. }
  28. private:
  29. const TString String_;
  30. TStringStream Stream_;
  31. };
  32. ////////////////////////////////////////////////////////////////////////////////
  33. class TEndOfStreamTest
  34. : public ::testing::TestWithParam<bool>
  35. { };
  36. TEST_P(TEndOfStreamTest, Eos)
  37. {
  38. bool addEos = GetParam();
  39. auto proxy = ::MakeIntrusive<TStringRawTableReader>(TString::Join(
  40. "{a=13;b = \"string\"}; {c = {d=12}};",
  41. "<key_switch=%true>#; {e = 42};",
  42. addEos ? "<end_of_stream=%true>#" : ""
  43. ));
  44. TNodeTableReader reader(proxy);
  45. TVector<TNode> expectedRows = {TNode()("a", 13)("b", "string"), TNode()("c", TNode()("d", 12))};
  46. for (const auto& expectedRow : expectedRows) {
  47. EXPECT_TRUE(reader.IsValid());
  48. EXPECT_TRUE(!reader.IsEndOfStream());
  49. EXPECT_TRUE(!reader.IsRawReaderExhausted());
  50. EXPECT_EQ(reader.GetRow(), expectedRow);
  51. reader.Next();
  52. }
  53. EXPECT_TRUE(!reader.IsValid());
  54. EXPECT_TRUE(!reader.IsEndOfStream());
  55. EXPECT_TRUE(!reader.IsRawReaderExhausted());
  56. reader.NextKey();
  57. reader.Next();
  58. expectedRows = {TNode()("e", 42)};
  59. for (const auto& expectedRow : expectedRows) {
  60. EXPECT_TRUE(reader.IsValid());
  61. EXPECT_TRUE(!reader.IsEndOfStream());
  62. EXPECT_TRUE(!reader.IsRawReaderExhausted());
  63. EXPECT_EQ(reader.GetRow(), expectedRow);
  64. reader.Next();
  65. }
  66. EXPECT_TRUE(!reader.IsValid());
  67. if (addEos) {
  68. EXPECT_TRUE(reader.IsEndOfStream());
  69. } else {
  70. EXPECT_TRUE(!reader.IsEndOfStream());
  71. }
  72. EXPECT_TRUE(reader.IsRawReaderExhausted());
  73. }
  74. INSTANTIATE_TEST_SUITE_P(WithEos, TEndOfStreamTest, ::testing::Values(true));
  75. INSTANTIATE_TEST_SUITE_P(WithoutEos, TEndOfStreamTest, ::testing::Values(false));
  76. ////////////////////////////////////////////////////////////////////////////////