1 | #include "gtest/gtest.h"
|
---|
2 | #include "ariba/utility/system/SystemQueue.h"
|
---|
3 |
|
---|
4 | #include <unistd.h> // usleep
|
---|
5 |
|
---|
6 | #include <ostream>
|
---|
7 |
|
---|
8 | using namespace ::testing;
|
---|
9 | using namespace ariba::utility;
|
---|
10 | using namespace std;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Tests if the SystemQueue is initialized empty and not running.
|
---|
14 | */
|
---|
15 | TEST(SystemQueue, Instantiation)
|
---|
16 | {
|
---|
17 | SystemQueue& sysq = SystemQueue::instance();
|
---|
18 |
|
---|
19 | // cout << &sysq << endl;
|
---|
20 |
|
---|
21 | EXPECT_FALSE( sysq.isRunning() );
|
---|
22 | EXPECT_TRUE( sysq.isEmpty() );
|
---|
23 | }
|
---|
24 |
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Tests whether calling the SystemQueue::instance() always returns the same object.
|
---|
28 | *
|
---|
29 | * NOTE: This is an easy case, since this is the same compile unit..
|
---|
30 | * But can't hurt to test it anyway.
|
---|
31 | */
|
---|
32 | TEST(SystemQueue, Singleton)
|
---|
33 | {
|
---|
34 | SystemQueue& sysq = SystemQueue::instance();
|
---|
35 | SystemQueue& sysq2 = SystemQueue::instance();
|
---|
36 |
|
---|
37 | // cout << &sysq << endl;
|
---|
38 |
|
---|
39 | EXPECT_TRUE( &sysq == &sysq2 );
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Start and stop the SystemQueue
|
---|
45 | */
|
---|
46 | TEST(SystemQueue, StartStop)
|
---|
47 | {
|
---|
48 | SystemQueue& sysq = SystemQueue::instance();
|
---|
49 |
|
---|
50 | EXPECT_FALSE( sysq.isRunning() );
|
---|
51 |
|
---|
52 | // start
|
---|
53 | sysq.run();
|
---|
54 |
|
---|
55 | EXPECT_TRUE( sysq.isRunning() );
|
---|
56 |
|
---|
57 | // stop
|
---|
58 | sysq.cancel();
|
---|
59 |
|
---|
60 | EXPECT_FALSE( sysq.isRunning() );
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Test fixture
|
---|
66 | *
|
---|
67 | * class that can be called by the SystemQueue
|
---|
68 | */
|
---|
69 | // To use a test fixture, derive a class from testing::Test.
|
---|
70 | class SystemQueueTest :
|
---|
71 | public testing::Test
|
---|
72 | {
|
---|
73 | public:
|
---|
74 |
|
---|
75 | SystemQueueTest() :
|
---|
76 | checkmark(false)
|
---|
77 | {
|
---|
78 | }
|
---|
79 |
|
---|
80 | void Check()
|
---|
81 | {
|
---|
82 | checkmark = true;
|
---|
83 | // cout << "Hallo Mario ^^" << endl;
|
---|
84 | }
|
---|
85 |
|
---|
86 | bool checkmark;
|
---|
87 | };
|
---|
88 |
|
---|
89 | TEST_F(SystemQueueTest, ScheduleCall)
|
---|
90 | {
|
---|
91 | #define MAX_WAIT 100 // microseconds
|
---|
92 | #define SLEEP_TIME 10 // microseconds
|
---|
93 |
|
---|
94 | SystemQueue& sysq = SystemQueue::instance();
|
---|
95 | checkmark = false; // just to be sure..
|
---|
96 |
|
---|
97 | // start
|
---|
98 | sysq.run();
|
---|
99 |
|
---|
100 | // scheduleCall
|
---|
101 | sysq.scheduleCall(
|
---|
102 | boost::bind(&SystemQueueTest::Check, this)
|
---|
103 | );
|
---|
104 |
|
---|
105 | // wait for the event to happen, (but not too long!)
|
---|
106 | for ( int i = 0; i < MAX_WAIT / SLEEP_TIME; i++)
|
---|
107 | {
|
---|
108 | if ( checkmark )
|
---|
109 | break;
|
---|
110 |
|
---|
111 | cout << "/// sleeping for " << SLEEP_TIME << " microseconds ..." << endl;
|
---|
112 | usleep(SLEEP_TIME);
|
---|
113 | }
|
---|
114 |
|
---|
115 | // stop
|
---|
116 | sysq.cancel();
|
---|
117 |
|
---|
118 | EXPECT_TRUE( checkmark ) << "Function was called within " << MAX_WAIT << " microseconds.";
|
---|
119 | }
|
---|
120 |
|
---|