summaryrefslogtreecommitdiff
path: root/indra/llcommon/tests/lleventfilter_test.cpp
blob: d7b80e2545f4135fa6f0039bdca66cc11bc03c8a (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/**
 * @file   lleventfilter_test.cpp
 * @author Nat Goodspeed
 * @date   2009-03-06
 * @brief  Test for lleventfilter.
 *
 * $LicenseInfo:firstyear=2009&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

// Precompiled header
#include "linden_common.h"
// associated header
#include "lleventfilter.h"
// STL headers
// std headers
// external library headers
// other Linden headers
#include "../test/lltut.h"
#include "stringize.h"
#include "llsdutil.h"
#include "listener.h"
#include "tests/wrapllerrs.h"

#include <typeinfo>

/*****************************************************************************
*   Test classes
*****************************************************************************/
// Strictly speaking, we're testing LLEventTimeoutBase rather than the
// production LLEventTimeout (using LLTimer) because we don't want every test
// run to pause for some number of seconds until we reach a real timeout. But
// as we've carefully put all functionality except actual LLTimer calls into
// LLEventTimeoutBase, that should suffice. We're not not not trying to test
// LLTimer here.
class TestEventTimeout: public LLEventTimeoutBase
{
public:
    TestEventTimeout():
        mElapsed(true)
    {}
    TestEventTimeout(LLEventPump& source):
        LLEventTimeoutBase(source),
        mElapsed(true)
    {}

    // test hook
    void forceTimeout(bool timeout=true) { mElapsed = timeout; }

protected:
    virtual void setCountdown(F32 seconds) { mElapsed = false; }
    virtual bool countdownElapsed() const { return mElapsed; }

private:
    bool mElapsed;
};

// Similar remarks about LLEventThrottle: we're actually testing the logic in
// LLEventThrottleBase, dummying out the LLTimer and LLEventTimeout used by
// the production LLEventThrottle class.
class TestEventThrottle: public LLEventThrottleBase
{
public:
    TestEventThrottle(F32 interval):
        LLEventThrottleBase(interval),
        mAlarmRemaining(-1.f),
        mTimerRemaining(-1.f)
    {}
    TestEventThrottle(LLEventPump& source, F32 interval):
        LLEventThrottleBase(source, interval),
        mAlarmRemaining(-1.f),
        mTimerRemaining(-1.f)
    {}

    /*----- implementation of LLEventThrottleBase timing functionality -----*/
    virtual void alarmActionAfter(F32 interval, const LLEventTimeoutBase::Action& action) /*override*/
    {
        mAlarmRemaining = interval;
        mAlarmAction = action;
    }

    virtual bool alarmRunning() const /*override*/
    {
        // decrementing to exactly 0 should mean the alarm fires
        return mAlarmRemaining > 0.f;
    }

    virtual void alarmCancel() /*override*/
    {
        mAlarmRemaining = -1.f;
    }

    virtual void timerSet(F32 interval) /*override*/
    {
        mTimerRemaining = interval;
    }

    virtual F32  timerGetRemaining() const /*override*/
    {
        // LLTimer.getRemainingTimeF32() never returns negative; 0.0 means expired
        return (mTimerRemaining > 0.0f)? mTimerRemaining : 0.0f;
    }

    /*------------------- methods for manipulating time --------------------*/
    void alarmAdvance(F32 delta)
    {
        bool wasRunning = alarmRunning();
        mAlarmRemaining -= delta;
        if (wasRunning && ! alarmRunning())
        {
            mAlarmAction();
        }
    }

    void timerAdvance(F32 delta)
    {
        // This simple implementation, like alarmAdvance(), completely ignores
        // HOW negative mTimerRemaining might go. All that matters is whether
        // it's negative. We trust that no test method in this source will
        // drive it beyond the capacity of an F32. Seems like a safe assumption.
        mTimerRemaining -= delta;
    }

    void advance(F32 delta)
    {
        // Advance the timer first because it has no side effects.
        // alarmAdvance() might call flush(), which will need to see the
        // change in the timer.
        timerAdvance(delta);
        alarmAdvance(delta);
    }

    F32 mAlarmRemaining, mTimerRemaining;
    LLEventTimeoutBase::Action mAlarmAction;
};

/*****************************************************************************
*   TUT
*****************************************************************************/
namespace tut
{
    struct filter_data
    {
        // The resemblance between this test data and that in llevents_tut.cpp
        // is not coincidental.
        filter_data():
            pumps(LLEventPumps::instance()),
            mainloop(pumps.obtain("mainloop")),
            listener0("first"),
            listener1("second")
        {}
        LLEventPumps& pumps;
        LLEventPump& mainloop;
        Listener listener0;
        Listener listener1;

        void check_listener(const std::string& desc, const Listener& listener, const LLSD& got)
        {
            ensure_equals(STRINGIZE(listener << ' ' << desc),
                          listener.getLastEvent(), got);
        }
    };
    typedef test_group<filter_data> filter_group;
    typedef filter_group::object filter_object;
    filter_group filtergrp("lleventfilter");

    template<> template<>
    void filter_object::test<1>()
    {
        set_test_name("LLEventMatching");
        LLEventPump& driver(pumps.obtain("driver"));
        listener0.reset(0);
        // Listener isn't derived from LLEventTrackable specifically to test
        // various connection-management mechanisms. But that means we have a
        // couple of transient Listener objects, one of which is listening to
        // a persistent LLEventPump. Capture those connections in local
        // LLTempBoundListener instances so they'll disconnect
        // on destruction.
        LLTempBoundListener temp1(
            listener0.listenTo(driver));
        // Construct a pattern LLSD: desired Event must have a key "foo"
        // containing string "bar"
        LLSD pattern;
        pattern.insert("foo", "bar");
        LLEventMatching filter(driver, pattern);
        listener1.reset(0);
        LLTempBoundListener temp2(
            listener1.listenTo(filter));
        driver.post(1);
        check_listener("direct", listener0, LLSD(1));
        check_listener("filtered", listener1, LLSD(0));
        // Okay, construct an LLSD map matching the pattern
        LLSD data;
        data["foo"] = "bar";
        data["random"] = 17;
        driver.post(data);
        check_listener("direct", listener0, data);
        check_listener("filtered", listener1, data);
    }

    template<> template<>
    void filter_object::test<2>()
    {
        set_test_name("LLEventTimeout::actionAfter()");
        LLEventPump& driver(pumps.obtain("driver"));
        TestEventTimeout filter(driver);
        listener0.reset(0);
        LLTempBoundListener temp1(
            listener0.listenTo(filter));
        // Use listener1.call() as the Action for actionAfter(), since it
        // already provides a way to sense the call
        listener1.reset(0);
        // driver --> filter --> listener0
        filter.actionAfter(20,
                           boost::bind(&Listener::call, boost::ref(listener1), LLSD("timeout")));
        // Okay, (fake) timer is ticking. 'filter' can only sense the timer
        // when we pump mainloop. Do that right now to take the logic path
        // before either the anticipated event arrives or the timer expires.
        mainloop.post(17);
        check_listener("no timeout 1", listener1, LLSD(0));
        // Expected event arrives...
        driver.post(1);
        check_listener("event passed thru", listener0, LLSD(1));
        // Should have canceled the timer. Verify that by asserting that the
        // time has expired, then pumping mainloop again.
        filter.forceTimeout();
        mainloop.post(17);
        check_listener("no timeout 2", listener1, LLSD(0));
        // Verify chained actionAfter() calls, that is, that a second
        // actionAfter() resets the timer established by the first
        // actionAfter().
        filter.actionAfter(20,
                           boost::bind(&Listener::call, boost::ref(listener1), LLSD("timeout")));
        // Since our TestEventTimeout class isn't actually manipulating time
        // (quantities of seconds), only a bool "elapsed" flag, sense that by
        // forcing the flag between actionAfter() calls.
        filter.forceTimeout();
        // Pumping mainloop here would result in a timeout (as we'll verify
        // below). This state simulates a ticking timer that has not yet timed
        // out. But now, before a mainloop event lets 'filter' recognize
        // timeout on the previous actionAfter() call, pretend we're pushing
        // that timeout farther into the future.
        filter.actionAfter(20,
                           boost::bind(&Listener::call, boost::ref(listener1), LLSD("timeout")));
        // Look ma, no timeout!
        mainloop.post(17);
        check_listener("no timeout 3", listener1, LLSD(0));
        // Now let the updated actionAfter() timer expire.
        filter.forceTimeout();
        // Notice the timeout.
        mainloop.post(17);
        check_listener("timeout", listener1, LLSD("timeout"));
        // Timing out cancels the timer. Verify that.
        listener1.reset(0);
        filter.forceTimeout();
        mainloop.post(17);
        check_listener("no timeout 4", listener1, LLSD(0));
        // Reset the timer and then cancel() it.
        filter.actionAfter(20,
                           boost::bind(&Listener::call, boost::ref(listener1), LLSD("timeout")));
        // neither expired nor satisified
        mainloop.post(17);
        check_listener("no timeout 5", listener1, LLSD(0));
        // cancel
        filter.cancel();
        // timeout!
        filter.forceTimeout();
        mainloop.post(17);
        check_listener("no timeout 6", listener1, LLSD(0));
    }

    template<> template<>
    void filter_object::test<3>()
    {
        set_test_name("LLEventTimeout::eventAfter()");
        LLEventPump& driver(pumps.obtain("driver"));
        TestEventTimeout filter(driver);
        listener0.reset(0);
        LLTempBoundListener temp1(
            listener0.listenTo(filter));
        filter.eventAfter(20, LLSD("timeout"));
        // Okay, (fake) timer is ticking. 'filter' can only sense the timer
        // when we pump mainloop. Do that right now to take the logic path
        // before either the anticipated event arrives or the timer expires.
        mainloop.post(17);
        check_listener("no timeout 1", listener0, LLSD(0));
        // Expected event arrives...
        driver.post(1);
        check_listener("event passed thru", listener0, LLSD(1));
        // Should have canceled the timer. Verify that by asserting that the
        // time has expired, then pumping mainloop again.
        filter.forceTimeout();
        mainloop.post(17);
        check_listener("no timeout 2", listener0, LLSD(1));
        // Set timer again.
        filter.eventAfter(20, LLSD("timeout"));
        // Now let the timer expire.
        filter.forceTimeout();
        // Notice the timeout.
        mainloop.post(17);
        check_listener("timeout", listener0, LLSD("timeout"));
        // Timing out cancels the timer. Verify that.
        listener0.reset(0);
        filter.forceTimeout();
        mainloop.post(17);
        check_listener("no timeout 3", listener0, LLSD(0));
    }

    template<> template<>
    void filter_object::test<4>()
    {
        set_test_name("LLEventTimeout::errorAfter()");
        WrapLLErrs capture;
        LLEventPump& driver(pumps.obtain("driver"));
        TestEventTimeout filter(driver);
        listener0.reset(0);
        LLTempBoundListener temp1(
            listener0.listenTo(filter));
        filter.errorAfter(20, "timeout");
        // Okay, (fake) timer is ticking. 'filter' can only sense the timer
        // when we pump mainloop. Do that right now to take the logic path
        // before either the anticipated event arrives or the timer expires.
        mainloop.post(17);
        check_listener("no timeout 1", listener0, LLSD(0));
        // Expected event arrives...
        driver.post(1);
        check_listener("event passed thru", listener0, LLSD(1));
        // Should have canceled the timer. Verify that by asserting that the
        // time has expired, then pumping mainloop again.
        filter.forceTimeout();
        mainloop.post(17);
        check_listener("no timeout 2", listener0, LLSD(1));
        // Set timer again.
        filter.errorAfter(20, "timeout");
        // Now let the timer expire.
        filter.forceTimeout();
        // Notice the timeout.
        std::string threw = capture.catch_llerrs([this](){
                mainloop.post(17);
            });
        ensure_contains("errorAfter() timeout exception", threw, "timeout");
        // Timing out cancels the timer. Verify that.
        listener0.reset(0);
        filter.forceTimeout();
        mainloop.post(17);
        check_listener("no timeout 3", listener0, LLSD(0));
    }

    template<> template<>
    void filter_object::test<5>()
    {
        set_test_name("LLEventThrottle");
        TestEventThrottle throttle(3);
        Concat cat;
        throttle.listen("concat", boost::ref(cat));

        // (sequence taken from LLEventThrottleBase Doxygen comments)
        //  1: post(): event immediately passed to listeners, next no sooner than 4
        throttle.advance(1);
        throttle.post("1");
        ensure_equals("1", cat.result, "1"); // delivered immediately
        //  2: post(): deferred: waiting for 3 seconds to elapse
        throttle.advance(1);
        throttle.post("2");
        ensure_equals("2", cat.result, "1"); // "2" not yet delivered
        //  3: post(): deferred
        throttle.advance(1);
        throttle.post("3");
        ensure_equals("3", cat.result, "1"); // "3" not yet delivered
        //  4: no post() call, but event delivered to listeners; next no sooner than 7
        throttle.advance(1);
        ensure_equals("4", cat.result, "13"); // "3" delivered
        //  6: post(): deferred
        throttle.advance(2);
        throttle.post("6");
        ensure_equals("6", cat.result, "13"); // "6" not yet delivered
        //  7: no post() call, but event delivered; next no sooner than 10
        throttle.advance(1);
        ensure_equals("7", cat.result, "136"); // "6" delivered
        // 12: post(): immediately passed to listeners, next no sooner than 15
        throttle.advance(5);
        throttle.post(";12");
        ensure_equals("12", cat.result, "136;12"); // "12" delivered
        // 17: post(): immediately passed to listeners, next no sooner than 20
        throttle.advance(5);
        throttle.post(";17");
        ensure_equals("17", cat.result, "136;12;17"); // "17" delivered
    }

    template<class PUMP>
    void test()
    {
        PUMP pump(typeid(PUMP).name());
        LLSD data{LLSD::emptyArray()};
        bool consumed{true};
        // listener that appends to 'data'
        // but that also returns the current value of 'consumed'
        // Instantiate this separately because we're going to listen()
        // multiple times with the same lambda: LLEventMailDrop only replays
        // queued events on a new listen() call.
        auto lambda =
            [&data, &consumed](const LLSD& event)->bool
            {
                data.append(event);
                return consumed;
            };
        {
            LLTempBoundListener conn = pump.listen("lambda", lambda);
            pump.post("first");
        }
        // first post() should certainly be received by listener
        ensure_equals("first", data, llsd::array("first"));
        // the question is, since consumed was true, did it queue the value?
        data = LLSD::emptyArray();
        {
            // if it queued the value, it would be delivered on subsequent
            // listen() call
            LLTempBoundListener conn = pump.listen("lambda", lambda);
        }
        ensure_equals("empty1", data, LLSD::emptyArray());
        data = LLSD::emptyArray();
        // now let's NOT consume the posted data
        consumed = false;
        {
            LLTempBoundListener conn = pump.listen("lambda", lambda);
            pump.post("second");
            pump.post("third");
        }
        // the two events still arrive
        ensure_equals("second,third1", data, llsd::array("second", "third"));
        data = LLSD::emptyArray();
        {
            // when we reconnect, these should be delivered again
            // but this time they should be consumed
            consumed = true;
            LLTempBoundListener conn = pump.listen("lambda", lambda);
        }
        // unconsumed events were delivered again
        ensure_equals("second,third2", data, llsd::array("second", "third"));
        data = LLSD::emptyArray();
        {
            // when we reconnect this time, no more unconsumed events
            LLTempBoundListener conn = pump.listen("lambda", lambda);
        }
        ensure_equals("empty2", data, LLSD::emptyArray());
    }

    template<> template<>
    void filter_object::test<6>()
    {
        set_test_name("LLEventMailDrop");
        tut::test<LLEventMailDrop>();
    }

    template<> template<>
    void filter_object::test<7>()
    {
        set_test_name("LLEventLogProxyFor<LLEventMailDrop>");
        tut::test< LLEventLogProxyFor<LLEventMailDrop> >();
    }
} // namespace tut

/*****************************************************************************
*   Link dependencies
*****************************************************************************/
#include "llsdutil.cpp"