aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/core/ask_ut.cpp
blob: e72ebdba9b66c1f49dbc65a65ba360f934021364 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <library/cpp/testing/unittest/registar.h>

#include "actorsystem.h"

#include <library/cpp/actors/testlib/test_runtime.h>

using namespace NActors;

class TPingPong: public TActor<TPingPong> {
public:
    TPingPong()
        : TActor(&TPingPong::Main)
    {
    }

    STATEFN(Main) {
        switch (ev->GetTypeRewrite()) {
            hFunc(TEvents::TEvPing, OnPing);
            hFunc(TEvents::TEvBlob, OnBlob);
        }
    }

    void OnPing(const TEvents::TEvPing::TPtr& ev) {
        Send(ev->Sender, new TEvents::TEvPong);
    }

    void OnBlob(const TEvents::TEvBlob::TPtr& ev) {
        Send(ev->Sender, ev->Release().Release());
    }
};

class TPing: public TActor<TPing> {
public:
    TPing()
        : TActor(&TPing::Main)
    {
    }

    STATEFN(Main) {
        Y_UNUSED(ev);
    }
};

THolder<TTestActorRuntimeBase> CreateRuntime() {
    auto runtime = MakeHolder<TTestActorRuntimeBase>();
    runtime->SetScheduledEventFilter([](auto&&, auto&&, auto&&, auto&&) { return false; });
    runtime->Initialize();
    return runtime;
}

Y_UNIT_TEST_SUITE(AskActor) {
    Y_UNIT_TEST(Ok) {
        auto runtime = CreateRuntime();
        auto pingpong = runtime->Register(new TPingPong);

        {
            auto fut = runtime->GetAnyNodeActorSystem()->Ask<TEvents::TEvPong>(
                pingpong,
                THolder(new TEvents::TEvPing));
            runtime->DispatchEvents();
            fut.ExtractValueSync();
        }

        {
            auto fut = runtime->GetAnyNodeActorSystem()->Ask<TEvents::TEvBlob>(
                pingpong,
                THolder(new TEvents::TEvBlob("hello!")));
            runtime->DispatchEvents();
            auto ev = fut.ExtractValueSync();
            UNIT_ASSERT_VALUES_EQUAL(ev->Blob, "hello!");
        }

        {
            auto fut = runtime->GetAnyNodeActorSystem()->Ask<IEventBase>(
                pingpong,
                THolder(new TEvents::TEvPing));
            runtime->DispatchEvents();
            auto ev = fut.ExtractValueSync();
            UNIT_ASSERT_VALUES_EQUAL(ev->Type(), TEvents::TEvPong::EventType);
        }
    }

    Y_UNIT_TEST(Err) {
        auto runtime = CreateRuntime();
        auto pingpong = runtime->Register(new TPingPong);

        {
            auto fut = runtime->GetAnyNodeActorSystem()->Ask<TEvents::TEvBlob>(
                pingpong,
                THolder(new TEvents::TEvPing));
            runtime->DispatchEvents();
            UNIT_ASSERT_EXCEPTION_CONTAINS(
                fut.ExtractValueSync(),
                yexception,
                "received unexpected response HelloWorld: Pong");
        }
    }

    Y_UNIT_TEST(Timeout) {
        auto runtime = CreateRuntime();
        auto ping = runtime->Register(new TPing);

        {
            auto fut = runtime->GetAnyNodeActorSystem()->Ask<TEvents::TEvPong>(
                ping,
                THolder(new TEvents::TEvPing),
                TDuration::Seconds(1));
            auto start = runtime->GetCurrentTime();
            runtime->DispatchEvents({}, TDuration::Seconds(5));
            UNIT_ASSERT_EXCEPTION_CONTAINS(
                fut.ExtractValueSync(),
                yexception,
                "ask timeout");
            UNIT_ASSERT_VALUES_EQUAL(runtime->GetCurrentTime() - start, TDuration::Seconds(1));
        }

        {
            auto fut = runtime->GetAnyNodeActorSystem()->Ask<IEventBase>(
                ping,
                THolder(new TEvents::TEvPing),
                TDuration::Seconds(1));
            auto start = runtime->GetCurrentTime();
            runtime->DispatchEvents({}, TDuration::Seconds(5));
            UNIT_ASSERT_EXCEPTION_CONTAINS(
                fut.ExtractValueSync(),
                yexception,
                "ask timeout");
            UNIT_ASSERT_VALUES_EQUAL(runtime->GetCurrentTime() - start, TDuration::Seconds(1));
        }
    }
}