~samuel-thibault/compiz/clone

« back to all changes in this revision

Viewing changes to src/timer/tests/while-calling/src/test-timer-set-times-while-calling.cpp

  • Committer: Daniel van Vugt
  • Date: 2012-01-12 06:48:58 UTC
  • mfrom: (2908 compiz-core)
  • mto: (2908.6.2 trunk)
  • mto: This revision was merged to the branch mainline in revision 2916.
  • Revision ID: vanvugt@gmail.com-20120112064858-xcu22jq1dlerirmp
Merge with upstream changes (lp:compiz-core).

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
#include "test-timer.h"
27
27
 
28
 
bool
29
 
CompTimerTestSetCalling::cb (int timernum, CompTimer *t1, CompTimer *t2, CompTimer *t3)
30
 
{
31
 
    std::cout << "INFO: triggering timer " << timernum << std::endl;
32
 
 
33
 
    if (lastTimerTriggered == 0 && timernum == 1)
34
 
    {
35
 
        /* Change the timeout time of the second timer to be after the third */
36
 
        t2->setTimes (4000, 4100);
37
 
 
38
 
        /* Check if it is now at the back of the timeout list */
39
 
        if (TimeoutHandler::Default ()->timers ().back () != t2)
40
 
        {
41
 
            std::cout << "FAIL: timer with higher timeout time is not at the back of the list" << std::endl;
42
 
 
43
 
            for (std::list <CompTimer *>::iterator it = TimeoutHandler::Default ()->timers ().begin ();
44
 
                 it != TimeoutHandler::Default ()->timers ().end (); it++)
45
 
            {
46
 
                CompTimer *t = (*it);
47
 
                std::cout << "INFO: t->minLeft " << t->minLeft () << std::endl << \
48
 
                             "INFO: t->maxLeft " << t->maxLeft () << std::endl << \
49
 
                             "INFO: t->minTime " << t->minTime () << std::endl << \
50
 
                             "INFO: t->maxTime " << t->maxTime () << std::endl;
51
 
            }
52
 
 
53
 
            exit (1);
54
 
        }
55
 
    }
56
 
    else if (lastTimerTriggered == 1 && timernum == 2)
57
 
    {
58
 
        std::cout << "FAIL: timer with a higher timeout time got triggered before a timer with a lower timeout time" << std::endl;
59
 
 
60
 
        for (std::list <CompTimer *>::iterator it = TimeoutHandler::Default ()->timers ().begin ();
61
 
             it != TimeoutHandler::Default ()->timers ().end (); it++)
62
 
        {
63
 
            CompTimer *t = (*it);
64
 
            std::cout << "INFO: t->minLeft " << t->minLeft () << std::endl << \
65
 
                         "INFO: t->maxLeft " << t->maxLeft () << std::endl << \
66
 
                         "INFO: t->minTime " << t->minTime () << std::endl << \
67
 
                         "INFO: t->maxTime " << t->maxTime () << std::endl;
68
 
        }
69
 
 
70
 
        exit (1);
71
 
    }
72
 
    else if (lastTimerTriggered == 2 && timernum != 1)
73
 
    {
74
 
        std::cout << "FAIL: timer with higher timeout time didn't get triggered after a lower timeout time" << std::endl;
75
 
 
76
 
        for (std::list <CompTimer *>::iterator it = TimeoutHandler::Default ()->timers ().begin ();
77
 
             it != TimeoutHandler::Default ()->timers ().end (); it++)
78
 
        {
79
 
            CompTimer *t = (*it);
80
 
            std::cout << "INFO: t->minLeft " << t->minLeft () << std::endl << \
81
 
                         "INFO: t->maxLeft " << t->maxLeft () << std::endl << \
82
 
                         "INFO: t->minTime " << t->minTime () << std::endl << \
83
 
                         "INFO: t->maxTime " << t->maxTime () << std::endl;
84
 
        }
85
 
 
86
 
        exit (1);
87
 
    }
88
 
 
89
 
    lastTimerTriggered = timernum;
90
 
 
91
 
    if (timernum == 2)
92
 
    {
93
 
        std::cout << "PASS: retiming" << std::endl;
94
 
        ml->quit ();
95
 
    }
96
 
 
97
 
    return false;
98
 
}
99
 
 
100
 
void
101
 
CompTimerTestSetCalling::precallback ()
102
 
{
103
 
    CompTimer      *t1, *t2, *t3;
104
 
 
105
 
    std::cout << "-= TEST: changing timeout time" << std::endl;
106
 
 
107
 
    t1 = new CompTimer ();
108
 
    t2 = new CompTimer ();
109
 
    t3 = new CompTimer ();
110
 
 
111
 
    timers.push_back (t1);
112
 
    timers.push_back (t2);
113
 
    timers.push_back (t3);
114
 
 
115
 
    t1->setCallback (boost::bind (&CompTimerTestSetCalling::cb, this, 1, t1, t2, t3));
116
 
    t1->setTimes (1000, 1100);
117
 
    t1->start ();
118
 
    t2->setCallback (boost::bind (&CompTimerTestSetCalling::cb, this, 2, t1, t2, t3));
119
 
    t2->setTimes (2000, 2100);
120
 
    t2->start ();
121
 
    t3->setCallback (boost::bind (&CompTimerTestSetCalling::cb, this, 3, t1, t2, t3));
122
 
    t3->setTimes (3000, 3100);
123
 
    t3->start ();
 
28
#include <pthread.h>
 
29
 
 
30
class CompTimerTestSetTimes: public CompTimerTest
 
31
{
 
32
protected:
 
33
 
 
34
    int mlastTimerTriggered;
 
35
 
 
36
    static void* run (void* cb)
 
37
    {
 
38
        if (cb == NULL)
 
39
        {
 
40
            return NULL;
 
41
        }
 
42
        static_cast<CompTimerTestSetTimes*>(cb)->ml->run();
 
43
        return NULL;
 
44
    }
 
45
 
 
46
    pthread_t mmainLoopThread;
 
47
    std::list<int> mtriggeredTimers;
 
48
 
 
49
    void recordTimers ()
 
50
    {
 
51
        for (std::list<CompTimer *>::iterator it =
 
52
                TimeoutHandler::Default()->timers().begin();
 
53
                it != TimeoutHandler::Default()->timers().end(); it++)
 
54
        {
 
55
            CompTimer *t = (*it);
 
56
            RecordProperty("t->minLeft", t->minLeft());
 
57
            RecordProperty("t->maxLeft", t->maxLeft());
 
58
            RecordProperty("t->minTime", t->minTime());
 
59
            RecordProperty("t->maxTime", t->maxTime());
 
60
        }
 
61
    }
 
62
 
 
63
    bool cb (int timernum, CompTimer* t1, CompTimer* t2, CompTimer* t3) {
 
64
        cb_(timernum,t1,t2,t3);
 
65
        return(true);
 
66
    }
 
67
    void cb_ (int timernum, CompTimer* t1, CompTimer* t2, CompTimer* t3)
 
68
    {
 
69
        recordTimers();
 
70
        if (mlastTimerTriggered == 0 && timernum == 1)
 
71
        {
 
72
            /* Change the timeout time of the second timer to be after the third */
 
73
            t2->setTimes(4000, 4100);
 
74
 
 
75
            recordTimers();
 
76
 
 
77
            /* Check if it is now at the back of the timeout list */
 
78
            ASSERT_EQ( TimeoutHandler::Default()->timers().back(), t2 );
 
79
        }
 
80
        else if (mlastTimerTriggered == 1 && timernum == 2)
 
81
        {
 
82
            recordTimers();
 
83
            FAIL() << "timer with a higher timeout time got triggered "
 
84
                    "before a timer with a lower timeout time";
 
85
        }
 
86
        else if (mlastTimerTriggered == 2 && timernum != 1)
 
87
        {
 
88
 
 
89
            recordTimers();
 
90
            FAIL() << "timer with higher timeout time didn't get "
 
91
                    "triggered after a lower timeout time";
 
92
        }
 
93
 
 
94
        mlastTimerTriggered = timernum;
 
95
    }
 
96
 
 
97
    void SetUp ()
 
98
    {
 
99
        CompTimerTest::SetUp();
 
100
        mlastTimerTriggered = 0;
 
101
        CompTimer *t1, *t2, *t3;
 
102
 
 
103
        t1 = new CompTimer();
 
104
        t2 = new CompTimer();
 
105
        t3 = new CompTimer();
 
106
 
 
107
        timers.push_back(t1);
 
108
        timers.push_back(t2);
 
109
        timers.push_back(t3);
 
110
 
 
111
        t1->setCallback(
 
112
                boost::bind(&CompTimerTestSetTimes::cb, this, 1, t1, t2, t3));
 
113
        t1->setTimes(1000, 1100);
 
114
        t1->start();
 
115
        t2->setCallback(
 
116
                boost::bind(&CompTimerTestSetTimes::cb, this, 2, t1, t2, t3));
 
117
        t2->setTimes(2000, 2100);
 
118
        t2->start();
 
119
        t3->setCallback(
 
120
                boost::bind(&CompTimerTestSetTimes::cb, this, 3, t1, t2, t3));
 
121
        t3->setTimes(3000, 3100);
 
122
        t3->start();
 
123
 
 
124
        ASSERT_EQ(
 
125
                0,
 
126
                pthread_create(&mmainLoopThread, NULL, CompTimerTestSetTimes::run, this));
 
127
    }
 
128
 
 
129
    void TearDown ()
 
130
    {
 
131
        ml->quit();
 
132
        pthread_join(mmainLoopThread, NULL);
 
133
 
 
134
        CompTimerTest::TearDown();
 
135
    }
 
136
};
 
137
 
 
138
TEST_F(CompTimerTestSetTimes, SetTimesWhileCalling)
 
139
{
 
140
    ::sleep(4);
 
141
    // Just a dummy forcing instantiation of fixture.
124
142
}