1
1
using System . Collections ;
2
2
using System . Collections . Generic ;
3
+ using System . Linq ;
3
4
using NUnit . Framework ;
4
5
using UnityEngine ;
5
6
using UnityEngine . TestTools ;
@@ -153,9 +154,7 @@ public class NetworkBehaviourUpdaterTests : BaseMultiInstanceTest
153
154
{
154
155
// Go ahead and create maximum number of clients (not all tests will use them)
155
156
protected override int NbClients => 2 ;
156
- private const float k_TimeOutWaitPeriod = 2.0f ;
157
157
public const int NetVarValueToSet = 1 ;
158
- private static float s_TimeOutPeriod ;
159
158
private static List < GameObject > s_ClientSpawnedNetworkObjects = new List < GameObject > ( ) ;
160
159
private List < NetworkManager > m_ActiveClientsForCurrentTest ;
161
160
@@ -168,31 +167,9 @@ public static void ClientSideNotifyObjectSpawned(GameObject objectSpaned)
168
167
if ( ! s_ClientSpawnedNetworkObjects . Contains ( objectSpaned ) )
169
168
{
170
169
s_ClientSpawnedNetworkObjects . Add ( objectSpaned ) ;
171
- // As long as we are getting notified the clients are spawning objects
172
- // then bump up the timeout period
173
- AdvanceTimeOutPeriod ( ) ;
174
170
}
175
171
}
176
172
177
- /// <summary>
178
- /// This will simply advance the timeout period
179
- /// Note: When ClientSideNotifyObjectSpawned is invoked this will get
180
- /// called to handle any potential latencies due to poor performance or
181
- /// the like.
182
- /// </summary>
183
- private static void AdvanceTimeOutPeriod ( )
184
- {
185
- s_TimeOutPeriod = Time . realtimeSinceStartup + k_TimeOutWaitPeriod ;
186
- }
187
-
188
- /// <summary>
189
- /// Checks if the timeout period has elapsed
190
- /// </summary>
191
- private static bool HasTimedOut ( )
192
- {
193
- return s_TimeOutPeriod <= Time . realtimeSinceStartup ;
194
- }
195
-
196
173
public override IEnumerator Setup ( )
197
174
{
198
175
// This makes sure the server and client are not started during the setup
@@ -315,22 +292,10 @@ public IEnumerator BehaviourUpdaterAllTests([Values] bool useHost,
315
292
// wait until all objects are spawned on the clients
316
293
if ( numberOfObjectsToSpawnOnClients > 0 )
317
294
{
318
- var allClientsSpawnedObjects = false ;
319
- // Reset the time out to be k_TimeOutWaitPeriod + the current time
320
- AdvanceTimeOutPeriod ( ) ;
321
-
322
295
// Waits for all clients to spawn the NetworkObjects
323
- while ( ! allClientsSpawnedObjects && ! HasTimedOut ( ) )
324
- {
325
- allClientsSpawnedObjects = numberOfObjectsToSpawnOnClients == s_ClientSpawnedNetworkObjects . Count ;
326
- yield return new WaitForSeconds ( tickInterval ) ;
327
- }
328
-
329
- Assert . True ( ! HasTimedOut ( ) , $ "Timed out waiting for clients to report spawning objects! " +
296
+ yield return WaitForConditionOrTimeOut ( ( c ) => c == s_ClientSpawnedNetworkObjects . Count , numberOfObjectsToSpawnOnClients ) ;
297
+ Assert . IsFalse ( s_GloabalTimeOutHelper . TimedOut , $ "Timed out waiting for clients to report spawning objects! " +
330
298
$ "Total reported client-side spawned objects { s_ClientSpawnedNetworkObjects . Count } ") ;
331
-
332
- // This really should never fail as it should timeout first
333
- Assert . True ( allClientsSpawnedObjects , "Not all clients spawned their objects!" ) ;
334
299
}
335
300
336
301
// Once all clients have spawned the NetworkObjects, set the network variables for
@@ -371,30 +336,10 @@ public IEnumerator BehaviourUpdaterAllTests([Values] bool useHost,
371
336
}
372
337
}
373
338
374
- var allClientsCompleted = false ;
375
- AdvanceTimeOutPeriod ( ) ;
376
-
377
- // Wait until all clients have had their NetworkVariables updated
378
- while ( ! allClientsCompleted && ! HasTimedOut ( ) )
379
- {
380
- var completedCount = 0 ;
381
- foreach ( var clientNetVarContainer in clientSideNetVarContainers )
382
- {
383
- if ( clientNetVarContainer . HaveAllValuesChanged ( NetVarValueToSet ) )
384
- {
385
- completedCount ++ ;
386
- }
387
- }
388
-
389
- allClientsCompleted = completedCount == clientSideNetVarContainers . Count ;
390
-
391
- yield return new WaitForSeconds ( tickInterval ) ;
392
- }
393
-
394
- Assert . True ( ! HasTimedOut ( ) , $ "Timed out waiting for client side NetVarContainers to report all NetworkVariables have been updated!") ;
395
-
396
- // This really should never fail as it should timeout first
397
- Assert . True ( allClientsCompleted , "Not all client side NetworkVariables reported they were updated with target value!" ) ;
339
+ yield return WaitForConditionOrTimeOut ( ( c ) =>
340
+ clientSideNetVarContainers . Where ( d =>
341
+ d . HaveAllValuesChanged ( c ) ) . Count ( ) == clientSideNetVarContainers . Count , NetVarValueToSet ) ;
342
+ Assert . IsFalse ( s_GloabalTimeOutHelper . TimedOut , $ "Timed out waiting for client side NetVarContainers to report all NetworkVariables have been updated!") ;
398
343
}
399
344
400
345
Object . DestroyImmediate ( prefabToSpawn ) ;
0 commit comments