~aleteoryx/muditaos

muditaos/module-services/service-appmgr/tests/test-ActionsRegistry.cpp -rw-r--r-- 6.0 KiB
a405cad6Aleteoryx trim readme 7 days ago
                                                                                
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include <catch2/catch.hpp>

#include <service-appmgr/model/ActionsRegistry.hpp>

using namespace app::manager;

TEST_CASE("Given actions registry when initialise it incorrectly then verify")
{
    REQUIRE_THROWS(ActionsRegistry{nullptr});
}

TEST_CASE("Given empty actions registry when verify its state then no pending actions")
{
    auto nextActionCallback = [](ActionEntry & /*entry*/) { return ActionProcessStatus::Accepted; };
    ActionsRegistry registry{std::move(nextActionCallback)};

    REQUIRE(!registry.hasPendingAction());
}

TEST_CASE("Given actions registry when enqueue then check pending action")
{
    auto nextActionCallback = [](ActionEntry & /*entry*/) { return ActionProcessStatus::Accepted; };
    ActionsRegistry registry{std::move(nextActionCallback)};

    registry.enqueue(ActionEntry{actions::Launch});
    REQUIRE(registry.hasPendingAction());
    REQUIRE(registry.getPendingAction()->actionId == actions::Launch);
}

TEST_CASE("Given actions registry when enqueue then check if action was processed")
{
    bool handled            = false;
    auto nextActionCallback = [&handled](ActionEntry & /*entry*/) {
        handled = true;
        return ActionProcessStatus::Accepted;
    };
    ActionsRegistry registry{std::move(nextActionCallback)};

    registry.enqueue(ActionEntry{actions::Launch});
    REQUIRE(handled);
}

TEST_CASE("Given actions registry when finished queued action then no pending actions")
{
    auto nextActionCallback = [](ActionEntry & /*entry*/) { return ActionProcessStatus::Accepted; };
    ActionsRegistry registry{std::move(nextActionCallback)};

    registry.enqueue(ActionEntry{actions::Launch});
    registry.finished();
    REQUIRE(!registry.hasPendingAction());
}

TEST_CASE("Given actions registry when enqueue multiple actions sequentially then count handled actions")
{
    int counter             = 0;
    auto nextActionCallback = [&counter](ActionEntry & /*entry*/) {
        ++counter;
        return ActionProcessStatus::Accepted;
    };
    ActionsRegistry registry{std::move(nextActionCallback)};

    registry.enqueue(ActionEntry{actions::Launch});
    registry.finished();

    registry.enqueue(ActionEntry{actions::Launch});
    registry.finished();

    registry.enqueue(ActionEntry{actions::Launch});
    registry.finished();

    REQUIRE(counter == 3);
}

TEST_CASE("Given actions registry when enqueue multiple actions at once then count handled actions")
{
    int counter             = 0;
    auto nextActionCallback = [&counter](ActionEntry & /*entry*/) {
        ++counter;
        return ActionProcessStatus::Accepted;
    };
    ActionsRegistry registry{std::move(nextActionCallback)};

    registry.enqueue(ActionEntry{actions::Launch});
    REQUIRE(counter == 1);
    REQUIRE(registry.hasPendingAction());

    registry.enqueue(ActionEntry{actions::Launch});
    registry.enqueue(ActionEntry{actions::Launch});

    registry.finished(); // Finished processing the 1st action
    REQUIRE(counter == 2);
    REQUIRE(registry.hasPendingAction());

    registry.finished(); // Finished processing the 2nd action
    REQUIRE(counter == 3);
    REQUIRE(registry.hasPendingAction());

    registry.finished(); // Finished processing the 3rd action
    REQUIRE(!registry.hasPendingAction());
}

TEST_CASE("Given actions registry when enqueue multiple actions at once then wait for them to expire.")
{
    auto nextActionCallback = [](ActionEntry & /*entry*/) { return ActionProcessStatus::Accepted; };
    ActionsRegistry registry{std::move(nextActionCallback)};

    registry.enqueue(ActionEntry{actions::Launch});
    REQUIRE(registry.hasPendingAction());

    registry.enqueue(ActionEntry{actions::Launch, {}, std::chrono::steady_clock::now() - std::chrono::seconds{29}});
    registry.enqueue(ActionEntry{actions::Launch, {}, std::chrono::steady_clock::now() - std::chrono::seconds{30}});

    registry.finished(); // Finished processing the 1st action
    REQUIRE(registry.hasPendingAction());

    registry.finished();                   // Finished processing the 2nd action
    REQUIRE(!registry.hasPendingAction()); // 3rd action has expired.
}

TEST_CASE("Given actions registry when enqueue multiple actions at once then process the specific ones only.")
{
    int counter             = 0;
    auto nextActionCallback = [&counter](ActionEntry &entry) {
        if (entry.actionId == actions::UserAction) {
            ++counter;
            return ActionProcessStatus::Accepted;
        }
        return ActionProcessStatus::Skipped;
    };
    ActionsRegistry registry{std::move(nextActionCallback)};

    registry.enqueue(ActionEntry{actions::Launch});
    REQUIRE(!registry.hasPendingAction());

    registry.enqueue(ActionEntry{actions::UserAction});
    registry.enqueue(ActionEntry{actions::Launch});
    REQUIRE(counter == 1);

    registry.finished();
    REQUIRE(counter == 1);
    REQUIRE(!registry.hasPendingAction());
}

TEST_CASE("Process the specific actions only and skip others.")
{
    int acceptedCounter     = 0;
    int skippedCounter      = 0;
    int droppedCounter      = 0;
    auto nextActionCallback = [&acceptedCounter, &skippedCounter, &droppedCounter](ActionEntry &entry) {
        if (entry.actionId == actions::UserAction) {
            ++acceptedCounter;
            return ActionProcessStatus::Accepted;
        }
        if (entry.actionId == actions::Home) {
            ++skippedCounter;
            return ActionProcessStatus::Skipped;
        }
        ++droppedCounter;
        return ActionProcessStatus::Dropped;
    };
    ActionsRegistry registry{std::move(nextActionCallback)};

    registry.enqueue(ActionEntry{actions::Launch});
    REQUIRE(droppedCounter == 1);
    REQUIRE(!registry.hasPendingAction());

    registry.enqueue(ActionEntry{actions::Home});
    REQUIRE(skippedCounter == 1);
    REQUIRE(!registry.hasPendingAction());

    registry.enqueue(ActionEntry{actions::UserAction});
    registry.finished();
    REQUIRE(acceptedCounter == 1);
}