Update checkRpItemsPosition and getTeam
[ryzomcore.git] / nel / tools / nel_unit_test / ut_net_layer3.h
blobcd7044c5d8bf79cd2ec83d3b5fc700a5ea14f6f6
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef UT_NET_LAYER3
18 #define UT_NET_LAYER3
20 #include <nel/net/callback_client.h>
21 #include <nel/net/callback_server.h>
23 uint16 TestPort1 = 56000;
25 uint NbTestReceived = 0;
27 NLNET::CMessage msgoutExpectingAnswer0, msgoutSimple0, msgoutSimple50;
29 // Data structure for messages
30 struct TData
32 string PayloadString;
33 bool ExpectingAnswer;
35 // Constructor
36 TData() : ExpectingAnswer(false) {}
38 // Serial
39 void serial( NLMISC::IStream& s )
41 s.serial( PayloadString );
42 s.serial( ExpectingAnswer );
46 // This callback must not take more than 10 ms
47 void cbTest( NLNET::CMessage &msgin, NLNET::TSockId from, NLNET::CCallbackNetBase &netbase )
49 NLMISC::TTime before = NLMISC::CTime::getLocalTime();
51 // Read data from the message
52 TData data;
53 msgin.serial( data );
54 if ( data.PayloadString == "Payload" )
55 ++NbTestReceived;
57 // Send the answer if required
58 if ( data.ExpectingAnswer )
59 netbase.send( msgoutSimple0, from );
61 // Check that the duration is compatible with our timeout tests
62 NLMISC::TTime maxDuration;
63 if ( msgin.getName() == "TEST_50" )
65 while ( NLMISC::CTime::getLocalTime() - before < 49 ); // wait
66 maxDuration = 70;
68 else
69 maxDuration = 10;
70 NLMISC::TTime actualDuration = NLMISC::CTime::getLocalTime() - before;
71 if ( actualDuration > maxDuration )
72 nlerror( "The callback cbTest takes too long (%u) for %s, please fix the test", (uint)actualDuration, msgin.getName().c_str() );
76 static NLNET::TCallbackItem CallbackArray[] =
78 { "TEST_0", cbTest },
79 { "TEST_50", cbTest }
83 // Test suite for layer 3
84 class CUTNetLayer3: public Test::Suite
86 public:
89 CUTNetLayer3 ()
91 _Server = NULL;
92 _Client = NULL;
93 TEST_ADD(CUTNetLayer3::sendReceiveUpdate);
98 ~CUTNetLayer3 ()
100 if ( _Server )
101 delete _Server;
102 _Server = NULL;
103 if ( _Client )
104 delete _Client;
105 _Client = NULL;
109 void sendReceiveUpdate()
111 // Prepare messages for tests
112 TData data;
113 data.PayloadString = "Payload";
114 data.ExpectingAnswer = true;
115 msgoutExpectingAnswer0.clear(); // optional
116 msgoutExpectingAnswer0.setType( "TEST_0" ); // could be passed to the constructor
117 msgoutExpectingAnswer0.serial( data );
118 data.ExpectingAnswer = false;
119 msgoutSimple0.clear(); // optional
120 msgoutSimple0.setType( "TEST_0" ); // could be passed to the constructor
121 msgoutSimple0.serial( data );
122 msgoutSimple50.clear(); // optional
123 msgoutSimple50.setType( "TEST_50" ); // could be passed to the constructor
124 msgoutSimple50.serial( data );
126 // Init connections
127 _Server = new NLNET::CCallbackServer();
128 _Server->init( TestPort1 );
129 _Server->addCallbackArray( CallbackArray, sizeof(CallbackArray)/sizeof(NLNET::TCallbackItem) );
130 _Client = new NLNET::CCallbackClient();
131 _Client->connect( NLNET::CInetAddress( "localhost", TestPort1 ) );
132 _Client->addCallbackArray( CallbackArray, sizeof(CallbackArray)/sizeof(NLNET::TCallbackItem) );
134 // TEST: Simple message transmission
135 NbTestReceived = 0;
136 _Client->send( msgoutExpectingAnswer0 );
137 for ( uint i=0; i!=10; ++i ) // give some time to receive
139 _Client->update();
140 _Server->update(); // legacy version
141 NLMISC::nlSleep( 50 );
143 TEST_ASSERT( NbTestReceived == 2 ); // answer and reply
145 // TEST: ONE-SHOT update mode on the receiver
146 NbTestReceived = 0;
147 for ( uint i=0; i!=20; ++i ) // send 20 messages
148 _Client->send( msgoutSimple0 );
149 while ( NbTestReceived < 20 )
151 _Client->update2();
152 uint prevNbTestReceived = NbTestReceived;
153 _Server->update2( 0 ); // shortest time-out = ONE-SHOT mode
154 TEST_ASSERT( (NbTestReceived == prevNbTestReceived) ||
155 (NbTestReceived == prevNbTestReceived + 1) );
156 NLMISC::nlSleep( 10 );
159 // TEST: GREEDY update mode on the receiver
160 NbTestReceived = 0;
161 for ( uint i=0; i!=20; ++i ) // send 20 messages
162 _Client->send( msgoutSimple0 );
163 for ( uint i=0; i!=10; ++i ) // make sure all messages are flushed
165 _Client->update2();
166 NLMISC::nlSleep( 10 );
168 _Server->update2( -1 ); // receive all
169 TEST_ASSERT( NbTestReceived == 20 );
171 // TEST: CONSTRAINED update mode on the receiver
172 NbTestReceived = 0;
173 for ( uint i=0; i!=20; ++i ) // send 20 messages that will trigger a time-consuming callback
174 _Client->send( msgoutSimple50 );
175 for ( uint i=0; i!=10; ++i ) // make sure all messages are flushed
177 _Client->update2();
178 NLMISC::nlSleep( 10 );
180 while ( NbTestReceived < 20 )
182 uint prevNbTestReceived = NbTestReceived;
183 _Server->update2( 80 ); // no more time than two callback executions
184 TEST_ASSERT( NbTestReceived <= prevNbTestReceived + 2 );
187 // TEST: CONSTRAINED with minTime update mode on the receiver
188 NbTestReceived = 0;
189 while ( NbTestReceived < 20 )
191 _Client->send( msgoutSimple0 );
192 _Client->send( msgoutSimple0 ); // send 2 messages at a time
193 _Client->update2();
194 NLMISC::TTime before = NLMISC::CTime::getLocalTime();
195 _Server->update2( -1, 30 );
196 NLMISC::TTime duration = NLMISC::CTime::getLocalTime() - before;
197 TEST_ASSERT( duration >= 30 );
201 private:
202 NLNET::CCallbackServer *_Server;
203 NLNET::CCallbackClient *_Client;
207 #endif