aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/rain_check/core/simple_ut.cpp
blob: d0afaf19faa03193ad126160e61e2aec81241cee (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
#include <library/cpp/testing/unittest/registar.h>

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

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

#include <util/system/event.h>

using namespace NRainCheck;

Y_UNIT_TEST_SUITE(RainCheckSimple) {
    struct TTaskWithCompletionCallback: public ISimpleTask {
        TTestEnv* const Env;
        TTestSync* const TestSync;

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

        TSubtaskCompletion SleepCompletion;

        TContinueFunc Start() override {
            TestSync->CheckAndIncrement(0);

            Env->SleepService.Sleep(&SleepCompletion, TDuration::MilliSeconds(1));
            SleepCompletion.SetCompletionCallback(&TTaskWithCompletionCallback::SleepCompletionCallback);

            return &TTaskWithCompletionCallback::Last;
        }

        void SleepCompletionCallback(TSubtaskCompletion* completion) {
            Y_ABORT_UNLESS(completion == &SleepCompletion);
            TestSync->CheckAndIncrement(1);

            Env->SleepService.Sleep(&SleepCompletion, TDuration::MilliSeconds(1));
            SleepCompletion.SetCompletionCallback(&TTaskWithCompletionCallback::NextSleepCompletionCallback);
        }

        void NextSleepCompletionCallback(TSubtaskCompletion*) {
            TestSync->CheckAndIncrement(2);
        }

        TContinueFunc Last() {
            TestSync->CheckAndIncrement(3);
            return nullptr;
        }
    };

    Y_UNIT_TEST(CompletionCallback) {
        TTestEnv env;
        TTestSync testSync;

        env.SpawnTask<TTaskWithCompletionCallback>(&testSync);

        testSync.WaitForAndIncrement(4);
    }
}