@@ -85,18 +85,18 @@ public async Task StressOpeningConnections()
8585 {
8686 Console . WriteLine ( "StressOpeningConnections" ) ;
8787
88- int connections_to_test = 100 ;
88+ int connections_to_test = 10 ;
8989 int port = Util . GetFreePort ( ) ;
9090 var ep = new IPEndPoint ( IPAddress . Loopback , port ) ;
91- ConcurrentStack < UdpClientConnection > connections = new ConcurrentStack < UdpClientConnection > ( ) ;
91+ List < UdpClientConnection > connections = new List < UdpClientConnection > ( ) ;
9292 int con_count = 0 ;
93- var allConnectedTcs = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
93+ ManualResetEvent mutex = new ManualResetEvent ( false ) ;
9494
9595 using ( var listener = new UdpConnectionListener ( ep ) )
9696 {
9797 listener . NewConnection += obj =>
9898 {
99- Interlocked . Increment ( ref con_count ) ;
99+ con_count ++ ;
100100
101101 obj . Connection . DataReceived += data_args =>
102102 {
@@ -109,17 +109,16 @@ public async Task StressOpeningConnections()
109109 } ;
110110
111111 obj . Recycle ( ) ;
112-
112+ Console . WriteLine ( con_count ) ;
113113 if ( con_count == connections_to_test )
114114 {
115- allConnectedTcs . TrySetResult ( ) ;
115+ mutex . Set ( ) ;
116116 }
117117 } ;
118118
119119 listener . Start ( ) ;
120120
121121 // Launch all client connections
122- var tasks = new List < Task > ( ) ;
123122 for ( int i = 0 ; i < connections_to_test ; i ++ )
124123 {
125124 var handshake = UdpMessageFactory . BuildHandshakeMessage ( ) ;
@@ -129,16 +128,16 @@ public async Task StressOpeningConnections()
129128 connection . DataReceived += data_args => data_args . Recycle ( ) ;
130129 connection . Disconnected += e => e . Recycle ( ) ;
131130
132- tasks . Add ( connection . Connect ( handshake ) ) ;
133- connections . Push ( connection ) ;
131+ await connection . Connect ( handshake ) ;
132+ connections . Add ( connection ) ;
134133
135134 // same problem as with stress test reliable messages
136- await Task . Delay ( 1 ) ;
135+ await Task . Delay ( 100 ) ;
137136 }
138137
139138 // Wait for all clients to finish connecting
140- var completed = await Task . WhenAny ( allConnectedTcs . Task , Task . Delay ( 10000 ) ) ;
141- Assert . True ( allConnectedTcs . Task . IsCompleted , "Not all connections established in time." ) ;
139+ mutex . WaitOne ( 5000 ) ;
140+ Assert . Equal ( connections_to_test , con_count ) ;
142141
143142 // Verify listener connection count
144143 Assert . Equal ( connections_to_test , listener . ConnectionCount ) ;
@@ -164,15 +163,19 @@ public async Task StressReliableMessages()
164163
165164 int count = 0 ;
166165
167- int messages_to_try = 100 ;
166+ int messages_to_try = 10000 ;
168167
169168 var mutex = new ManualResetEvent ( false ) ;
170169
170+ UdpServerConnection server_connection = null ;
171+
171172 using ( UdpConnectionListener listener = new UdpConnectionListener ( ep ) )
172173 using ( UdpConnection connection = new UdpClientConnection ( new TestLogger ( "Client" ) , ep ) )
173174 {
174175 listener . NewConnection += ( evt ) =>
175176 {
177+ server_connection = ( UdpServerConnection ) evt . Connection ;
178+
176179 evt . Connection . Disconnected += delegate ( DisconnectedEvent obj )
177180 {
178181 obj . Recycle ( ) ;
@@ -185,17 +188,6 @@ public async Task StressReliableMessages()
185188 {
186189 mutex . Set ( ) ;
187190 sw . Stop ( ) ;
188- Console . WriteLine ( "Readers: " + Core . Pools . ReaderPool . InUse . ToString ( ) ) ;
189- Console . WriteLine ( "Packets: " + Infinity . Udp . Pools . PacketPool . InUse . ToString ( ) ) ;
190- Console . WriteLine ( "Fragmented: " + Infinity . Udp . Pools . FragmentedMessagePool . InUse . ToString ( ) ) ;
191- Console . WriteLine ( "Writers: " + Core . Pools . WriterPool . InUse . ToString ( ) ) ;
192-
193- Console . WriteLine ( "DataReceived: " + Core . Pools . DataReceivedEventPool . InUse . ToString ( ) ) ;
194- Console . WriteLine ( "Disconnected: " + Core . Pools . DisconnectedEventPool . InUse . ToString ( ) ) ;
195- Console . WriteLine ( "NewConnection: " + Core . Pools . NewConnectionPool . InUse . ToString ( ) ) ;
196-
197- Console . WriteLine ( "Server packets: " + ( ( UdpConnection ) ( obj . Connection ) ) . reliable_data_packets_sent . Count ) ;
198- Console . WriteLine ( "Client packets: " + ( ( UdpConnection ) connection ) . reliable_data_packets_sent . Count ) ;
199191 }
200192 obj . Recycle ( ) ;
201193 } ;
@@ -220,16 +212,28 @@ public async Task StressReliableMessages()
220212 var message = UdpMessageFactory . BuildReliableMessage ( ) ;
221213 message . Write ( 123 ) ;
222214
223- _ = connection . Send ( message ) ;
215+ await connection . Send ( message ) ;
224216
225217 // if we dont have this delay something weird happens and the packets and readers are not recycled, need to figure stuff out
226- await Task . Delay ( 1 ) ;
218+ // await Task.Delay(1);
227219 }
228220
229- mutex . WaitOne ( 2000 ) ;
221+ mutex . WaitOne ( 10000 ) ;
230222 Assert . Equal ( messages_to_try , count ) ;
231- await Task . Delay ( 1000 ) ;
223+ await Task . Delay ( 10000 ) ;
232224 Console . WriteLine ( $ "StressReliableMessages took { sw . ElapsedMilliseconds } ms") ;
225+
226+ Console . WriteLine ( "Readers: " + Core . Pools . ReaderPool . InUse . ToString ( ) ) ;
227+ Console . WriteLine ( "Packets: " + Infinity . Udp . Pools . PacketPool . InUse . ToString ( ) ) ;
228+ Console . WriteLine ( "Fragmented: " + Infinity . Udp . Pools . FragmentedMessagePool . InUse . ToString ( ) ) ;
229+ Console . WriteLine ( "Writers: " + Core . Pools . WriterPool . InUse . ToString ( ) ) ;
230+
231+ Console . WriteLine ( "DataReceived: " + Core . Pools . DataReceivedEventPool . InUse . ToString ( ) ) ;
232+ Console . WriteLine ( "Disconnected: " + Core . Pools . DisconnectedEventPool . InUse . ToString ( ) ) ;
233+ Console . WriteLine ( "NewConnection: " + Core . Pools . NewConnectionPool . InUse . ToString ( ) ) ;
234+
235+ Console . WriteLine ( "Server packets: " + server_connection . reliable_data_packets_sent . Count ) ;
236+ Console . WriteLine ( "Client packets: " + ( ( UdpConnection ) connection ) . reliable_data_packets_sent . Count ) ;
233237 }
234238 }
235239
0 commit comments