ask_ut.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include "actorsystem.h"
  3. #include <library/cpp/actors/testlib/test_runtime.h>
  4. using namespace NActors;
  5. class TPingPong: public TActor<TPingPong> {
  6. public:
  7. TPingPong()
  8. : TActor(&TPingPong::Main)
  9. {
  10. }
  11. STATEFN(Main) {
  12. switch (ev->GetTypeRewrite()) {
  13. hFunc(TEvents::TEvPing, OnPing);
  14. hFunc(TEvents::TEvBlob, OnBlob);
  15. }
  16. }
  17. void OnPing(const TEvents::TEvPing::TPtr& ev) {
  18. Send(ev->Sender, new TEvents::TEvPong);
  19. }
  20. void OnBlob(const TEvents::TEvBlob::TPtr& ev) {
  21. Send(ev->Sender, ev->Release().Release());
  22. }
  23. };
  24. class TPing: public TActor<TPing> {
  25. public:
  26. TPing()
  27. : TActor(&TPing::Main)
  28. {
  29. }
  30. STATEFN(Main) {
  31. Y_UNUSED(ev);
  32. }
  33. };
  34. THolder<TTestActorRuntimeBase> CreateRuntime() {
  35. auto runtime = MakeHolder<TTestActorRuntimeBase>();
  36. runtime->SetScheduledEventFilter([](auto&&, auto&&, auto&&, auto&&) { return false; });
  37. runtime->Initialize();
  38. return runtime;
  39. }
  40. Y_UNIT_TEST_SUITE(AskActor) {
  41. Y_UNIT_TEST(Ok) {
  42. auto runtime = CreateRuntime();
  43. auto pingpong = runtime->Register(new TPingPong);
  44. {
  45. auto fut = runtime->GetAnyNodeActorSystem()->Ask<TEvents::TEvPong>(
  46. pingpong,
  47. THolder(new TEvents::TEvPing));
  48. runtime->DispatchEvents();
  49. fut.ExtractValueSync();
  50. }
  51. {
  52. auto fut = runtime->GetAnyNodeActorSystem()->Ask<TEvents::TEvBlob>(
  53. pingpong,
  54. THolder(new TEvents::TEvBlob("hello!")));
  55. runtime->DispatchEvents();
  56. auto ev = fut.ExtractValueSync();
  57. UNIT_ASSERT_VALUES_EQUAL(ev->Blob, "hello!");
  58. }
  59. {
  60. auto fut = runtime->GetAnyNodeActorSystem()->Ask<IEventBase>(
  61. pingpong,
  62. THolder(new TEvents::TEvPing));
  63. runtime->DispatchEvents();
  64. auto ev = fut.ExtractValueSync();
  65. UNIT_ASSERT_VALUES_EQUAL(ev->Type(), TEvents::TEvPong::EventType);
  66. }
  67. }
  68. Y_UNIT_TEST(Err) {
  69. auto runtime = CreateRuntime();
  70. auto pingpong = runtime->Register(new TPingPong);
  71. {
  72. auto fut = runtime->GetAnyNodeActorSystem()->Ask<TEvents::TEvBlob>(
  73. pingpong,
  74. THolder(new TEvents::TEvPing));
  75. runtime->DispatchEvents();
  76. UNIT_ASSERT_EXCEPTION_CONTAINS(
  77. fut.ExtractValueSync(),
  78. yexception,
  79. "received unexpected response HelloWorld: Pong");
  80. }
  81. }
  82. Y_UNIT_TEST(Timeout) {
  83. auto runtime = CreateRuntime();
  84. auto ping = runtime->Register(new TPing);
  85. {
  86. auto fut = runtime->GetAnyNodeActorSystem()->Ask<TEvents::TEvPong>(
  87. ping,
  88. THolder(new TEvents::TEvPing),
  89. TDuration::Seconds(1));
  90. auto start = runtime->GetCurrentTime();
  91. runtime->DispatchEvents({}, TDuration::Seconds(5));
  92. UNIT_ASSERT_EXCEPTION_CONTAINS(
  93. fut.ExtractValueSync(),
  94. yexception,
  95. "ask timeout");
  96. UNIT_ASSERT_VALUES_EQUAL(runtime->GetCurrentTime() - start, TDuration::Seconds(1));
  97. }
  98. {
  99. auto fut = runtime->GetAnyNodeActorSystem()->Ask<IEventBase>(
  100. ping,
  101. THolder(new TEvents::TEvPing),
  102. TDuration::Seconds(1));
  103. auto start = runtime->GetCurrentTime();
  104. runtime->DispatchEvents({}, TDuration::Seconds(5));
  105. UNIT_ASSERT_EXCEPTION_CONTAINS(
  106. fut.ExtractValueSync(),
  107. yexception,
  108. "ask timeout");
  109. UNIT_ASSERT_VALUES_EQUAL(runtime->GetCurrentTime() - start, TDuration::Seconds(1));
  110. }
  111. }
  112. }