@@ -13,6 +13,24 @@ public class NetworkObjectOnSpawnTests : BaseMultiInstanceTest
13
13
14
14
protected override int NbClients => 2 ;
15
15
16
+ private const float k_TimeOutWaitPeriod = 2.0f ;
17
+ private static float s_TimeOutPeriod ;
18
+
19
+ /// <summary>
20
+ /// This will simply advance the timeout period
21
+ /// </summary>
22
+ public static void AdvanceTimeOutPeriod ( )
23
+ {
24
+ s_TimeOutPeriod = Time . realtimeSinceStartup + k_TimeOutWaitPeriod ;
25
+ }
26
+
27
+ /// <summary>
28
+ /// Checks if the timeout period has elapsed
29
+ /// </summary>
30
+ private static bool HasTimedOut ( )
31
+ {
32
+ return s_TimeOutPeriod <= Time . realtimeSinceStartup ;
33
+ }
16
34
17
35
/// <summary>
18
36
/// Tests that instantiating a <see cref="NetworkObject"/> and destroying without spawning it
@@ -57,7 +75,6 @@ public override IEnumerator Setup()
57
75
[ UnityTearDown ]
58
76
public override IEnumerator Teardown ( )
59
77
{
60
-
61
78
if ( m_TestNetworkObjectPrefab != null )
62
79
{
63
80
Object . Destroy ( m_TestNetworkObjectPrefab ) ;
@@ -68,7 +85,6 @@ public override IEnumerator Teardown()
68
85
Object . Destroy ( m_TestNetworkObjectInstance ) ;
69
86
}
70
87
yield return base . Teardown ( ) ;
71
-
72
88
}
73
89
74
90
/// <summary>
@@ -102,66 +118,129 @@ public IEnumerator TestOnNetworkSpawnCallbacks()
102
118
// safety check despawned
103
119
Assert . AreEqual ( 0 , serverInstance . OnNetworkDespawnCalledCount ) ;
104
120
105
- // check spawned on client
106
- foreach ( var clientInstance in clientInstances )
121
+ AdvanceTimeOutPeriod ( ) ;
122
+ var timedOut = false ;
123
+ while ( ! HasTimedOut ( ) )
107
124
{
108
- Assert . AreEqual ( 1 , clientInstance . OnNetworkSpawnCalledCount ) ;
109
-
110
- // safety check despawned
111
- Assert . AreEqual ( 0 , clientInstance . OnNetworkDespawnCalledCount ) ;
125
+ var spawnedCount = 0 ;
126
+ // check spawned on client
127
+ foreach ( var clientInstance in clientInstances )
128
+ {
129
+ if ( clientInstance . OnNetworkSpawnCalledCount == 1 )
130
+ {
131
+ spawnedCount ++ ;
132
+ }
133
+
134
+ // safety check despawned
135
+ Assert . AreEqual ( 0 , clientInstance . OnNetworkDespawnCalledCount ) ;
136
+ }
137
+
138
+ timedOut = HasTimedOut ( ) ;
139
+ if ( spawnedCount >= NbClients )
140
+ {
141
+ break ;
142
+ }
143
+
144
+ yield return new WaitForSeconds ( 1.0f / m_ServerNetworkManager . NetworkConfig . TickRate ) ;
112
145
}
113
146
147
+ Assert . False ( timedOut , "Timed out while waiting for client side spawns!" ) ;
148
+
114
149
// despawn on server. However, since we'll be using this object later in the test, don't delete it (false)
115
150
serverInstance . GetComponent < NetworkObject > ( ) . Despawn ( false ) ;
116
151
117
152
// check despawned on server
118
153
Assert . AreEqual ( 1 , serverInstance . OnNetworkDespawnCalledCount ) ;
119
154
120
- // wait long enough for player object to be despawned
121
- int nextFrameNumber = Time . frameCount + 4 ;
122
- yield return new WaitUntil ( ( ) => Time . frameCount >= nextFrameNumber ) ;
123
-
124
- // check despawned on clients
125
- foreach ( var clientInstance in clientInstances )
155
+ AdvanceTimeOutPeriod ( ) ;
156
+ timedOut = false ;
157
+ while ( ! HasTimedOut ( ) )
126
158
{
127
- Assert . AreEqual ( 1 , clientInstance . OnNetworkDespawnCalledCount ) ;
159
+ var deSpawnedCount = 0 ;
160
+ foreach ( var clientInstance in clientInstances )
161
+ {
162
+ if ( clientInstance . OnNetworkDespawnCalledCount == 1 )
163
+ {
164
+ deSpawnedCount ++ ;
165
+ }
166
+ }
167
+
168
+ timedOut = HasTimedOut ( ) ;
169
+
170
+ if ( deSpawnedCount >= NbClients )
171
+ {
172
+ break ;
173
+ }
174
+
175
+ yield return new WaitForSeconds ( 1.0f / m_ServerNetworkManager . NetworkConfig . TickRate ) ;
128
176
}
129
177
130
- //----------- step 2 check spawn again and destroy
178
+ Assert . False ( timedOut , "Timed out while waiting for client side despawns!" ) ;
131
179
180
+ //----------- step 2 check spawn again and destroy
132
181
serverInstance . GetComponent < NetworkObject > ( ) . Spawn ( ) ;
133
182
134
- // wait long enough for player object to be spawned
135
- nextFrameNumber = Time . frameCount + 2 ;
136
- yield return new WaitUntil ( ( ) => Time . frameCount >= nextFrameNumber ) ;
137
-
183
+ yield return new WaitForSeconds ( 1.0f / m_ServerNetworkManager . NetworkConfig . TickRate ) ;
138
184
// check spawned again on server this is 2 because we are reusing the object which was already spawned once.
139
185
Assert . AreEqual ( 2 , serverInstance . OnNetworkSpawnCalledCount ) ;
140
186
141
- // check spawned on client
142
- foreach ( var clientInstance in clientInstances )
187
+ AdvanceTimeOutPeriod ( ) ;
188
+ timedOut = false ;
189
+ while ( ! HasTimedOut ( ) )
143
190
{
144
- Assert . AreEqual ( 1 , clientInstance . OnNetworkSpawnCalledCount ) ;
191
+ var spawnedCount = 0 ;
192
+ // check spawned on client
193
+ foreach ( var clientInstance in clientInstances )
194
+ {
195
+ if ( clientInstance . OnNetworkSpawnCalledCount == 1 )
196
+ {
197
+ spawnedCount ++ ;
198
+ }
199
+ }
200
+
201
+ timedOut = HasTimedOut ( ) ;
202
+ if ( spawnedCount >= NbClients )
203
+ {
204
+ break ;
205
+ }
206
+
207
+ yield return new WaitForSeconds ( 1.0f / m_ServerNetworkManager . NetworkConfig . TickRate ) ;
145
208
}
146
209
210
+ Assert . False ( timedOut , "Timed out while waiting for client side spawns! (2nd pass)" ) ;
211
+
147
212
// destroy the server object
148
213
Object . Destroy ( serverInstance . gameObject ) ;
149
214
150
- // wait one frame for destroy to kick in
151
- yield return null ;
215
+ yield return new WaitForSeconds ( 1.0f / m_ServerNetworkManager . NetworkConfig . TickRate ) ;
152
216
153
217
// check whether despawned was called again on server instance
154
218
Assert . AreEqual ( 2 , serverInstance . OnNetworkDespawnCalledCount ) ;
155
219
156
- // wait long enough for player object to be despawned on client
157
- nextFrameNumber = Time . frameCount + 2 ;
158
- yield return new WaitUntil ( ( ) => Time . frameCount >= nextFrameNumber ) ;
159
-
160
- // check despawned on clients
161
- foreach ( var clientInstance in clientInstances )
220
+ AdvanceTimeOutPeriod ( ) ;
221
+ timedOut = false ;
222
+ while ( ! HasTimedOut ( ) )
162
223
{
163
- Assert . AreEqual ( 1 , clientInstance . OnNetworkDespawnCalledCount ) ;
224
+ var deSpawnedCount = 0 ;
225
+ foreach ( var clientInstance in clientInstances )
226
+ {
227
+ if ( clientInstance . OnNetworkDespawnCalledCount == 1 )
228
+ {
229
+ deSpawnedCount ++ ;
230
+ }
231
+ }
232
+
233
+ timedOut = HasTimedOut ( ) ;
234
+
235
+ if ( deSpawnedCount >= NbClients )
236
+ {
237
+ break ;
238
+ }
239
+
240
+ yield return new WaitForSeconds ( 1.0f / m_ServerNetworkManager . NetworkConfig . TickRate ) ;
164
241
}
242
+
243
+ Assert . False ( timedOut , "Timed out while waiting for client side despawns! (2nd pass)" ) ;
165
244
}
166
245
167
246
private class TrackOnSpawnFunctions : NetworkBehaviour
0 commit comments