test_pool.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <yql/essentials/public/purecalc/common/interface.h>
  3. #include <yql/essentials/public/purecalc/io_specs/protobuf/spec.h>
  4. #include <yql/essentials/public/purecalc/ut/protos/test_structs.pb.h>
  5. #include <library/cpp/protobuf/util/pb_io.h>
  6. #include <util/string/cast.h>
  7. using namespace NYql::NPureCalc;
  8. namespace {
  9. class TStringMessageStreamImpl: public IStream<NPureCalcProto::TStringMessage*> {
  10. private:
  11. ui32 I_ = 0;
  12. NPureCalcProto::TStringMessage Message_{};
  13. public:
  14. NPureCalcProto::TStringMessage* Fetch() override {
  15. if (I_ >= 3) {
  16. return nullptr;
  17. } else {
  18. Message_.SetX(ToString(I_));
  19. ++I_;
  20. return &Message_;
  21. }
  22. }
  23. };
  24. class TStringMessageConsumerImpl: public IConsumer<NPureCalcProto::TStringMessage*> {
  25. private:
  26. TVector<TString>* Buf_;
  27. public:
  28. TStringMessageConsumerImpl(TVector<TString>* buf)
  29. : Buf_(buf)
  30. {
  31. }
  32. public:
  33. void OnObject(NPureCalcProto::TStringMessage* t) override {
  34. Buf_->push_back(t->GetX());
  35. }
  36. void OnFinish() override {
  37. }
  38. };
  39. }
  40. Y_UNIT_TEST_SUITE(TestWorkerPool) {
  41. static TString sql = "SELECT 'abc'u || X AS X FROM Input";
  42. static TVector<TString> expected{"abc0", "abc1", "abc2"};
  43. void TestPullStreamImpl(bool useWorkerPool) {
  44. auto factory = MakeProgramFactory(TProgramFactoryOptions().SetUseWorkerPool(useWorkerPool));
  45. auto program = factory->MakePullStreamProgram(
  46. TProtobufInputSpec<NPureCalcProto::TStringMessage>(),
  47. TProtobufOutputSpec<NPureCalcProto::TStringMessage>(),
  48. sql,
  49. ETranslationMode::SQL
  50. );
  51. auto check = [](IStream<NPureCalcProto::TStringMessage*>* output) {
  52. TVector<TString> actual;
  53. while (auto *x = output->Fetch()) {
  54. actual.push_back(x->GetX());
  55. }
  56. UNIT_ASSERT_VALUES_EQUAL(expected, actual);
  57. };
  58. // Sequential use
  59. for (size_t i = 0; i < 2; ++i) {
  60. auto output = program->Apply(MakeHolder<TStringMessageStreamImpl>());
  61. check(output.Get());
  62. }
  63. // Parallel use
  64. {
  65. auto output1 = program->Apply(MakeHolder<TStringMessageStreamImpl>());
  66. auto output2 = program->Apply(MakeHolder<TStringMessageStreamImpl>());
  67. check(output1.Get());
  68. check(output2.Get());
  69. }
  70. }
  71. Y_UNIT_TEST(TestPullStreamUseWorkerPool) {
  72. TestPullStreamImpl(true);
  73. }
  74. Y_UNIT_TEST(TestPullStreamNoWorkerPool) {
  75. TestPullStreamImpl(false);
  76. }
  77. void TestPullListImpl(bool useWorkerPool) {
  78. auto factory = MakeProgramFactory(TProgramFactoryOptions().SetUseWorkerPool(useWorkerPool));
  79. auto program = factory->MakePullListProgram(
  80. TProtobufInputSpec<NPureCalcProto::TStringMessage>(),
  81. TProtobufOutputSpec<NPureCalcProto::TStringMessage>(),
  82. sql,
  83. ETranslationMode::SQL
  84. );
  85. auto check = [](IStream<NPureCalcProto::TStringMessage*>* output) {
  86. TVector<TString> actual;
  87. while (auto *x = output->Fetch()) {
  88. actual.push_back(x->GetX());
  89. }
  90. UNIT_ASSERT_VALUES_EQUAL(expected, actual);
  91. };
  92. // Sequential use
  93. for (size_t i = 0; i < 2; ++i) {
  94. auto output = program->Apply(MakeHolder<TStringMessageStreamImpl>());
  95. check(output.Get());
  96. }
  97. // Parallel use
  98. {
  99. auto output1 = program->Apply(MakeHolder<TStringMessageStreamImpl>());
  100. auto output2 = program->Apply(MakeHolder<TStringMessageStreamImpl>());
  101. check(output1.Get());
  102. check(output2.Get());
  103. }
  104. }
  105. Y_UNIT_TEST(TestPullListUseWorkerPool) {
  106. TestPullListImpl(true);
  107. }
  108. Y_UNIT_TEST(TestPullListNoWorkerPool) {
  109. TestPullListImpl(false);
  110. }
  111. void TestPushStreamImpl(bool useWorkerPool) {
  112. auto factory = MakeProgramFactory(TProgramFactoryOptions().SetUseWorkerPool(useWorkerPool));
  113. auto program = factory->MakePushStreamProgram(
  114. TProtobufInputSpec<NPureCalcProto::TStringMessage>(),
  115. TProtobufOutputSpec<NPureCalcProto::TStringMessage>(),
  116. sql,
  117. ETranslationMode::SQL
  118. );
  119. auto check = [](IConsumer<NPureCalcProto::TStringMessage*>* input, const TVector<TString>& result) {
  120. NPureCalcProto::TStringMessage message;
  121. for (auto s: {"0", "1", "2"}) {
  122. message.SetX(s);
  123. input->OnObject(&message);
  124. }
  125. input->OnFinish();
  126. UNIT_ASSERT_VALUES_EQUAL(expected, result);
  127. };
  128. // Sequential use
  129. for (size_t i = 0; i < 2; ++i) {
  130. TVector<TString> actual;
  131. auto input = program->Apply(MakeHolder<TStringMessageConsumerImpl>(&actual));
  132. check(input.Get(), actual);
  133. }
  134. // Parallel use
  135. {
  136. TVector<TString> actual1;
  137. auto input1 = program->Apply(MakeHolder<TStringMessageConsumerImpl>(&actual1));
  138. TVector<TString> actual2;
  139. auto input2 = program->Apply(MakeHolder<TStringMessageConsumerImpl>(&actual2));
  140. check(input1.Get(), actual1);
  141. check(input2.Get(), actual2);
  142. }
  143. }
  144. Y_UNIT_TEST(TestPushStreamUseWorkerPool) {
  145. TestPushStreamImpl(true);
  146. }
  147. Y_UNIT_TEST(TestPushStreamNoWorkerPool) {
  148. TestPushStreamImpl(false);
  149. }
  150. }