Line data Source code
1 :
2 : /* Copyright (c) 2013-2014, Stefan.Eilemann@epfl.ch
3 : *
4 : * This library is free software; you can redistribute it and/or modify it under
5 : * the terms of the GNU Lesser General Public License version 2.1 as published
6 : * by the Free Software Foundation.
7 : *
8 : * This library is distributed in the hope that it will be useful, but WITHOUT
9 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 : * details.
12 : *
13 : * You should have received a copy of the GNU Lesser General Public License
14 : * along with this library; if not, write to the Free Software Foundation, Inc.,
15 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 : */
17 :
18 : #ifdef WARN_DEPRECATED
19 : # undef WARN_DEPRECATED
20 : #endif
21 :
22 : #include "test.h"
23 :
24 : #include <lunchbox/mtQueue.h>
25 : #include <lunchbox/requestHandler.h>
26 : #include <lunchbox/sleep.h>
27 : #include <lunchbox/thread.h>
28 : #include <servus/uint128_t.h>
29 :
30 : using servus::uint128_t;
31 :
32 1 : lunchbox::RequestHandler handler_;
33 1 : lunchbox::MTQueue< uint32_t > requestQ_;
34 1 : const uint128_t uuid = servus::make_UUID();
35 :
36 2 : class Thread : public lunchbox::Thread
37 : {
38 : public:
39 1 : virtual void run() final
40 : {
41 1 : const bool boolIn = true;
42 1 : uint8_t* payload = (uint8_t*)42;
43 :
44 1 : uint32_t request = requestQ_.pop();
45 1 : TEST( handler_.getRequestData( request ) == ++payload );
46 1 : handler_.serveRequest( request, boolIn );
47 :
48 1 : const uint32_t uint32In = 0xC0FFEE;
49 1 : request = requestQ_.pop();
50 1 : TEST( handler_.getRequestData( request ) == ++payload );
51 1 : handler_.serveRequest( request, uint32In );
52 :
53 1 : request = requestQ_.pop();
54 1 : TEST( handler_.getRequestData( request ) == ++payload );
55 1 : handler_.serveRequest( request );
56 :
57 1 : request = requestQ_.pop();
58 1 : TESTINFO( handler_.getRequestData( request ) == ++payload,
59 : (size_t)handler_.getRequestData( request ) << " for " <<
60 0 : request );
61 1 : handler_.serveRequest( request );
62 :
63 1 : request = requestQ_.pop();
64 1 : TEST( handler_.getRequestData( request ) == ++payload );
65 1 : handler_.serveRequest( request, uuid );
66 :
67 1 : request = requestQ_.pop();
68 1 : TEST( handler_.getRequestData( request ) == ++payload );
69 1 : handler_.serveRequest( request );
70 1 : }
71 : };
72 :
73 1 : int main( int, char** )
74 : {
75 1 : uint8_t* payload = (uint8_t*)42;
76 1 : Thread thread;
77 1 : thread.start();
78 :
79 1 : uint32_t request = handler_.registerRequest( ++payload );
80 1 : requestQ_.push( request );
81 1 : bool boolOut = false;
82 1 : TEST( handler_.waitRequest( request, boolOut ));
83 1 : TEST( boolOut == true );
84 :
85 : lunchbox::Request< uint32_t > future =
86 2 : handler_.registerRequest< uint32_t >( ++payload );
87 1 : requestQ_.push( future.getID( ));
88 :
89 1 : request = handler_.registerRequest( ++payload );
90 1 : requestQ_.push( request );
91 1 : TEST( handler_.waitRequest( request ));
92 :
93 : lunchbox::Request< void > voidFuture =
94 2 : handler_.registerRequest< void >( ++payload );
95 : lunchbox::Request< uint128_t > uint128Future =
96 2 : handler_.registerRequest< uint128_t >( ++payload );
97 :
98 1 : requestQ_.push( voidFuture.getID( ));
99 1 : requestQ_.push( uint128Future.getID( ));
100 :
101 1 : TEST( future.wait( ) == 0xC0FFEE );
102 1 : TEST( future.wait( ));
103 1 : TEST( uint128Future.wait() == uuid );
104 1 : TEST( handler_.isRequestServed( voidFuture.getID( )));
105 1 : TEST( voidFuture.isReady( ));
106 1 : voidFuture.wait( );
107 :
108 : {
109 : lunchbox::Request< void > waitAtDestructor =
110 1 : handler_.registerRequest< void >( ++payload );
111 1 : requestQ_.push( waitAtDestructor.getID( ));
112 : }
113 :
114 : {
115 : lunchbox::Request< void > wontBeServed =
116 1 : handler_.registerRequest< void >( ++payload );
117 : try
118 : {
119 1 : wontBeServed.wait( 1 );
120 0 : lunchbox::abort();
121 : }
122 1 : catch( const lunchbox::FutureTimeout& ) {}
123 :
124 1 : TEST( handler_.hasPendingRequests( ))
125 1 : wontBeServed.unregister();
126 1 : TEST( !handler_.hasPendingRequests( ))
127 :
128 : try
129 : {
130 1 : wontBeServed.wait();
131 0 : lunchbox::abort();
132 : }
133 2 : catch( const lunchbox::UnregisteredRequest& ) {}
134 : }
135 :
136 1 : TEST( thread.join( ));
137 2 : return EXIT_SUCCESS;
138 3 : }
|