summaryrefslogtreecommitdiffstats
blob: 167fe4581bbd854c46a1b7a8ebfa18f67a9236bf (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
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <boost/bind.hpp>
#include <boost/thread.hpp>

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

#include <Swiften/Base/sleep.h>
#include <Swiften/EventLoop/SimpleEventLoop.h>

using namespace Swift;

class SimpleEventLoopTest : public CppUnit::TestFixture {
        CPPUNIT_TEST_SUITE(SimpleEventLoopTest);
        // FIXME: Temporarily disabling run, because it generates a "vector
        // iterator not incrementable" on XP
        //CPPUNIT_TEST(testRun);
        CPPUNIT_TEST(testPostFromMainThread);
        CPPUNIT_TEST_SUITE_END();

    public:
        void setUp() {
            counter_ = 0;
        }

        void testRun() {
            SimpleEventLoop testling;
            boost::thread thread(boost::bind(&SimpleEventLoopTest::runIncrementingThread, this, &testling));
            testling.run();

            CPPUNIT_ASSERT_EQUAL(10, counter_);
        }

        void testPostFromMainThread() {
            SimpleEventLoop testling;
            testling.postEvent(boost::bind(&SimpleEventLoopTest::incrementCounterAndStop, this, &testling));
            testling.run();

            CPPUNIT_ASSERT_EQUAL(1, counter_);
        }

    private:
        void runIncrementingThread(SimpleEventLoop* loop) {
            for (unsigned int i = 0; i < 10; ++i) {
                Swift::sleep(1);
                loop->postEvent(boost::bind(&SimpleEventLoopTest::incrementCounter, this));
            }
            loop->stop();
        }

        void incrementCounter() {
            counter_++;
        }

        void incrementCounterAndStop(SimpleEventLoop* loop) {
            counter_++;
            loop->stop();
        }

        int counter_;
};

CPPUNIT_TEST_SUITE_REGISTRATION(SimpleEventLoopTest);