aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/rain_check/core/spawn_ut.cpp
blob: ba5a5e41cf65f179ba8e1205ac82cd5143586a27 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <library/cpp/testing/unittest/registar.h>

#include <library/cpp/messagebus/rain_check/test/helper/misc.h>
#include <library/cpp/messagebus/rain_check/test/ut/test.h>

#include <library/cpp/messagebus/latch.h>

#include <util/system/event.h>

#include <array>

using namespace NRainCheck;
using namespace NActor;

Y_UNIT_TEST_SUITE(Spawn) {
    struct TTestTask: public ISimpleTask {
        TTestSync* const TestSync;

        TTestTask(TSimpleEnv*, TTestSync* testSync)
            : TestSync(testSync)
            , I(0)
        {
        }

        TSystemEvent Started;

        unsigned I;

        TContinueFunc Start() override {
            if (I < 4) {
                I += 1;
                return &TTestTask::Start;
            }
            TestSync->CheckAndIncrement(0);
            return &TTestTask::Continue;
        }

        TContinueFunc Continue() {
            TestSync->CheckAndIncrement(1);

            Started.Signal();
            return nullptr;
        }
    };

    Y_UNIT_TEST(Continuation) {
        TTestSync testSync;

        TSimpleEnv env;

        TIntrusivePtr<TSimpleTaskRunner> task = env.SpawnTask<TTestTask>(&testSync);

        testSync.WaitForAndIncrement(2);
    }

    struct TSubtask: public ISimpleTask {
        TTestEnv* const Env;
        TTestSync* const TestSync;

        TSubtask(TTestEnv* env, TTestSync* testSync)
            : Env(env)
            , TestSync(testSync)
        {
        }

        TContinueFunc Start() override {
            Sleep(TDuration::MilliSeconds(1));
            TestSync->CheckAndIncrement(1);
            return nullptr;
        }
    };

    struct TSpawnTask: public ISimpleTask {
        TTestEnv* const Env;
        TTestSync* const TestSync;

        TSpawnTask(TTestEnv* env, TTestSync* testSync)
            : Env(env)
            , TestSync(testSync)
        {
        }

        TSubtaskCompletion SubtaskCompletion;

        TContinueFunc Start() override {
            TestSync->CheckAndIncrement(0);
            SpawnSubtask<TSubtask>(Env, &SubtaskCompletion, TestSync);
            return &TSpawnTask::Continue;
        }

        TContinueFunc Continue() {
            TestSync->CheckAndIncrement(2);
            return nullptr;
        }
    };

    Y_UNIT_TEST(Subtask) {
        TTestSync testSync;

        TTestEnv env;

        TIntrusivePtr<TSimpleTaskRunner> task = env.SpawnTask<TSpawnTask>(&testSync);

        testSync.WaitForAndIncrement(3);
    }

    struct TSpawnLongTask: public ISimpleTask {
        TTestEnv* const Env;
        TTestSync* const TestSync;
        unsigned I;

        TSpawnLongTask(TTestEnv* env, TTestSync* testSync)
            : Env(env)
            , TestSync(testSync)
            , I(0)
        {
        }

        std::array<TSubtaskCompletion, 3> Subtasks;

        TContinueFunc Start() override {
            if (I == 1000) {
                TestSync->CheckAndIncrement(0);
                return nullptr;
            }

            for (auto& subtask : Subtasks) {
                SpawnSubtask<TNopSimpleTask>(Env, &subtask, "");
            }

            ++I;
            return &TSpawnLongTask::Start;
        }
    };

    Y_UNIT_TEST(SubtaskLong) {
        TTestSync testSync;

        TTestEnv env;

        TIntrusivePtr<TSimpleTaskRunner> task = env.SpawnTask<TSpawnLongTask>(&testSync);

        testSync.WaitForAndIncrement(1);
    }
}