summaryrefslogtreecommitdiff
path: root/cesar/maximus/scheduler/utest/src/TestScheduler.cpp
blob: 0d32ade0e9edd4d20331d483a21290de11854b84 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* Maximus project {{{
 *
 * Copyright (C) 2012 MStar Semiconductor
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    maximus/scheduler/utest/src/TestScheduler.cpp
 * \ingroup maximus_scheduler_utest
 *
 */
#include <cppunit/extensions/TestFactoryRegistry.h>
#include "maximus/scheduler/inc/Scheduler.h"
#include "maximus/sci/inc/SciServer.h"
#include "maximus/processors/inc/PhyProcessor.h"
#include "maximus/processors/inc/ClockProcessor.h"
#include "maximus/utils/inc/Error.h"
#include "inc/TestScheduler.h"
#include "inc/fake_Maximus.h"
#include "inc/EventExpected.h"

CPPUNIT_TEST_SUITE_REGISTRATION (TestScheduler);
list_event_t received;

void
TestScheduler::setUp ()
{
    fake_maximus = new Maximus ();
    fake_server = new SciServer (sta_list);
    fake_phy_proc = new PhyProcessor (*fake_server,
                                      *fake_maximus);
    fake_clock_proc = new ClockProcessor (*fake_server,
                                          *fake_maximus);
    sched = NULL;
}

void
TestScheduler::tearDown ()
{
    delete fake_clock_proc;
    delete fake_phy_proc;
    delete fake_server;
    delete fake_maximus;
}

inline void
TestScheduler::init_procs (Scheduler *p)
{
    received.clear ();
    fake_phy_proc->register_scheduler (sched);
    fake_clock_proc->register_scheduler (sched);
}

inline void
TestScheduler::add_wanted_list (list_event_t &list)
{
    list_event_t::iterator it;

    for (it = list.begin (); it != list.end (); ++it)
    {
        if (it->get_evt_type () == NETWORK_CLOCK_TYPE_PHY)
            sched->add_phy_event (
                it->get_tick (),
                it->get_sta_id (),
                it->get_clock_id (),
                it->get_msg ());
        else
            sched->add_event (
                it->get_tick (),
                it->get_sta_id (),
                it->get_clock_id (),
                it->get_evt_type ());
    }
}

inline void
TestScheduler::check_schedule (list_event_t &list)
{
    list_event_t::iterator it;
    list_event_t::iterator it_rcv;
    bool is_found;
    uint id;
    uint nb_event_expected;
    Network_Clock_Tick prev_time;

    prev_time = sched->get_current_tick ();
    nb_event_expected = list.size ();

    for (uint i = list.size (); i != 0; --i)
    {
        sched->process_next_event ();
        CPPUNIT_ASSERT (prev_time <= sched->get_current_tick ());
    }

    CPPUNIT_ASSERT (nb_event_expected == received.size ());

    id = 0;
    for (it = list.begin (); it != list.end (); ++it)
    {
        ++id;
        is_found = false;

        for (it_rcv = received.begin ();
             it_rcv != received.end ();
             ++it_rcv)
        {
            if (*it == *it_rcv)
            {
                is_found = true;
                break;
            }
        }

        if (is_found)
            received.erase (it_rcv);
        else
        {
            std::ostringstream oss;
            oss << "Event requested not received (" << id << ")";
            CPPUNIT_FAIL (oss.str ());
        }
    }

    sched->process_next_event ();
    CPPUNIT_ASSERT (0 == received.size ());
}

void
TestScheduler::test_add_events_ok ()
{
    std::cout << __func__ << std::endl;
    sched = new Scheduler (*fake_phy_proc, *fake_clock_proc);
    list_event_t wanted;
    init_procs (sched);


    /* With this list of events, we will test to received:
     *  - events sorted by tick,
     *  - on same tick, there is not sort mandatory,
     *  - all events must be received as there key are different.
     */
    wanted.push_back (
        EventExpected (
            0, 25, 30, NETWORK_CLOCK_TYPE_STATION, NULL));
    wanted.push_back (
        EventExpected (
            2, 25, 30, NETWORK_CLOCK_TYPE_STATION, NULL));
    wanted.push_back (
        EventExpected (
            5, 25, 30, NETWORK_CLOCK_TYPE_STATION, NULL));
    wanted.push_back (
        EventExpected (
            5, 25, 31, NETWORK_CLOCK_TYPE_PHY, (SciMsg *)0xDEADBEEF));
    wanted.push_back (
        EventExpected (
            5, 29, 31, NETWORK_CLOCK_TYPE_STATION, NULL));
    wanted.push_back (
        EventExpected (
            6, 29, 31, NETWORK_CLOCK_TYPE_STATION, NULL));
    wanted.push_back (
        EventExpected (
            30, 30, 31, NETWORK_CLOCK_TYPE_PHY, (SciMsg *)0xDEADBEEF));
    wanted.push_back (
        EventExpected (
            10, 40, 31, NETWORK_CLOCK_TYPE_PHY, (SciMsg *)0xDEADBEEF));

    add_wanted_list (wanted);

    CPPUNIT_ASSERT (sched->get_current_tick () == 0);
    check_schedule (wanted);

    delete sched;
}

void
TestScheduler::test_max_tick ()
{
    std::cout << __func__ << std::endl;
    sched = new Scheduler (*fake_phy_proc, *fake_clock_proc);
    list_event_t wanted;
    init_procs (sched);

    sched->set_max_tick (200);

    wanted.push_back (
        EventExpected (
            600, 0, 0, NETWORK_CLOCK_TYPE_STATION, NULL));
    add_wanted_list (wanted);
    sched->process_next_event ();
    CPPUNIT_ASSERT (200 == sched->get_current_tick ());
    CPPUNIT_ASSERT (received.empty ());

    sched->set_max_tick (0);
    sched->process_next_event ();
    CPPUNIT_ASSERT (600 == sched->get_current_tick ());
    CPPUNIT_ASSERT (!received.empty ());
    CPPUNIT_ASSERT (*received.begin () == *wanted.begin ());

    delete sched;
}

void
TestScheduler::test_remove_event ()
{
    std::cout << __func__ << std::endl;
    list_event_t::iterator it;
    sched = new Scheduler (*fake_phy_proc, *fake_clock_proc);
    init_procs (sched);
    list_event_t unwanted, wanted;

    unwanted.push_back (
        EventExpected (
            13, 14, 15, NETWORK_CLOCK_TYPE_STATION, NULL));
    wanted.push_back (
        EventExpected (
            35, 15, 31, NETWORK_CLOCK_TYPE_PHY, (SciMsg *)0xBEEF));
    unwanted.push_back (
        EventExpected (
            35, 26, 40, NETWORK_CLOCK_TYPE_STATION, NULL));
    wanted.push_back (
        EventExpected (
            45, 26, 40, NETWORK_CLOCK_TYPE_STATION, NULL));

    add_wanted_list (wanted);
    add_wanted_list (unwanted);

    for (it = unwanted.begin (); it != unwanted.end (); ++it)
    {
        sched->remove_event (
            it->get_tick (),
            it->get_sta_id (),
            it->get_clock_id ());
    }
    check_schedule (wanted);
    delete sched;
}

void
TestScheduler::test_remove_event_past ()
{
    std::cout << __func__ << std::endl;
    list_event_t::iterator it;
    sched = new Scheduler (*fake_phy_proc, *fake_clock_proc);
    init_procs (sched);
    list_event_t wanted_1, wanted_2;

    wanted_1.push_back (
        EventExpected (
            100, 14, 15, NETWORK_CLOCK_TYPE_STATION, NULL));
    wanted_2.push_back (
        EventExpected (
            200, 15, 31, NETWORK_CLOCK_TYPE_PHY, (SciMsg *)0xBEEF));

    add_wanted_list (wanted_1);
    add_wanted_list (wanted_2);

    sched->set_max_tick (150);
    check_schedule (wanted_1);

    /* should we have an error on this request ? */
    sched->remove_event (40, 11, 12);

    sched->set_max_tick (0);
    check_schedule (wanted_2);
    delete sched;
}

void
TestScheduler::test_remove_sta_events ()
{
    std::cout << __func__ << std::endl;
    list_event_t::iterator it;
    sched = new Scheduler (*fake_phy_proc, *fake_clock_proc);
    init_procs (sched);
    Sci_Msg_Station_Id staid = 18;

    list_event_t unwanted, wanted;

    unwanted.push_back (
        EventExpected (
            13, staid, 15, NETWORK_CLOCK_TYPE_STATION, NULL));
    wanted.push_back (
        EventExpected (
            35, 15, 31, NETWORK_CLOCK_TYPE_PHY, (SciMsg *)0xBEEF));
    unwanted.push_back (
        EventExpected (
            35, staid, 40, NETWORK_CLOCK_TYPE_STATION, NULL));
    wanted.push_back (
        EventExpected (
            45, 26, 40, NETWORK_CLOCK_TYPE_STATION, NULL));

    add_wanted_list (wanted);
    add_wanted_list (unwanted);

    sched->remove_sta_events (staid);
    check_schedule (wanted);
    delete sched;
}