1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
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
36 TData() : ExpectingAnswer(false) {}
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
54 if ( data
.PayloadString
== "Payload" )
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
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
[] =
83 // Test suite for layer 3
84 class CUTNetLayer3
: public Test::Suite
93 TEST_ADD(CUTNetLayer3::sendReceiveUpdate
);
109 void sendReceiveUpdate()
111 // Prepare messages for tests
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
);
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
136 _Client
->send( msgoutExpectingAnswer0
);
137 for ( uint i
=0; i
!=10; ++i
) // give some time to receive
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
147 for ( uint i
=0; i
!=20; ++i
) // send 20 messages
148 _Client
->send( msgoutSimple0
);
149 while ( NbTestReceived
< 20 )
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
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
166 NLMISC::nlSleep( 10 );
168 _Server
->update2( -1 ); // receive all
169 TEST_ASSERT( NbTestReceived
== 20 );
171 // TEST: CONSTRAINED update mode on the receiver
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
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
189 while ( NbTestReceived
< 20 )
191 _Client
->send( msgoutSimple0
);
192 _Client
->send( msgoutSimple0
); // send 2 messages at a time
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 );
202 NLNET::CCallbackServer
*_Server
;
203 NLNET::CCallbackClient
*_Client
;