-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathSceneManagementSynchronizationTests.cs
More file actions
244 lines (205 loc) · 9.34 KB
/
SceneManagementSynchronizationTests.cs
File metadata and controls
244 lines (205 loc) · 9.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Unity.Collections;
using Unity.Netcode;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine.TestTools;
namespace TestProject.RuntimeTests
{
[TestFixture(HostOrServer.Host)]
[TestFixture(HostOrServer.Server)]
[TestFixture(HostOrServer.DAHost)]
public class SceneManagementSynchronizationTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 1;
public SceneManagementSynchronizationTests(HostOrServer hostOrServer) : base(hostOrServer)
{
}
private struct ExpectedEvent
{
public SceneEvent SceneEvent;
public ConnectionEventData ConnectionEvent;
}
private readonly Queue<ExpectedEvent> m_ExpectedEventQueue = new();
private static int s_NumEventsProcessed;
private void OnSceneEvent(SceneEvent sceneEvent)
{
VerboseDebug($"OnSceneEvent! Type: {sceneEvent.SceneEventType}.");
AssertEventMatchesExpectedEvent(expectedEvent =>
ValidateSceneEventsAreEqual(expectedEvent, sceneEvent), sceneEvent.SceneEventType);
}
private void OnConnectionEvent(NetworkManager manager, ConnectionEventData eventData)
{
VerboseDebug($"OnConnectionEvent! Type: {eventData.EventType} - Client-{eventData.ClientId}");
AssertEventMatchesExpectedEvent(expectedEvent =>
ValidateConnectionEventsAreEqual(expectedEvent, eventData), eventData.EventType);
}
private void AssertEventMatchesExpectedEvent<T>(Action<ExpectedEvent> predicate, T eventType)
{
if (m_ExpectedEventQueue.Count > 0)
{
var expectedEvent = m_ExpectedEventQueue.Dequeue();
predicate(expectedEvent);
}
else
{
Assert.Fail($"Received unexpected event at index {s_NumEventsProcessed}: {eventType}");
}
s_NumEventsProcessed++;
}
private NetworkManager m_ManagerToTest;
private void SetManagerToTest(NetworkManager manager)
{
m_ManagerToTest = manager;
m_ManagerToTest.OnConnectionEvent += OnConnectionEvent;
m_ManagerToTest.SceneManager.OnSceneEvent += OnSceneEvent;
}
protected override void OnNewClientStarted(NetworkManager networkManager)
{
// If m_ManagerToTest isn't set at this point, it means we are testing the newly created NetworkManager
if (m_ManagerToTest == null)
{
SetManagerToTest(networkManager);
}
base.OnNewClientCreated(networkManager);
}
protected override IEnumerator OnTearDown()
{
m_ManagerToTest.OnConnectionEvent -= OnConnectionEvent;
m_ManagerToTest.SceneManager.OnSceneEvent -= OnSceneEvent;
m_ManagerToTest = null;
m_ExpectedEventQueue.Clear();
s_NumEventsProcessed = 0;
yield return base.OnTearDown();
}
[UnityTest]
public IEnumerator SynchronizationCallbacks_Authority()
{
SetManagerToTest(GetAuthorityNetworkManager());
// Calculate the expected ID of the newly connecting networkManager
var expectedClientId = GetNonAuthorityNetworkManager().LocalClientId + 1;
// Setup expected events
m_ExpectedEventQueue.Enqueue(new ExpectedEvent()
{
SceneEvent = new SceneEvent()
{
SceneEventType = SceneEventType.Synchronize,
ClientId = expectedClientId
},
});
m_ExpectedEventQueue.Enqueue(new ExpectedEvent()
{
SceneEvent = new SceneEvent()
{
SceneEventType = SceneEventType.SynchronizeComplete,
ClientId = expectedClientId,
},
});
m_ExpectedEventQueue.Enqueue(new ExpectedEvent()
{
ConnectionEvent = new ConnectionEventData()
{
EventType = ConnectionEvent.ClientConnected,
ClientId = expectedClientId,
}
});
if (m_UseHost)
{
m_ExpectedEventQueue.Enqueue(new ExpectedEvent()
{
ConnectionEvent = new ConnectionEventData()
{
EventType = ConnectionEvent.PeerConnected,
ClientId = expectedClientId,
}
});
}
m_EnableVerboseDebug = true;
//////////////////////////////////////////
// Testing event notifications
yield return CreateAndStartNewClient();
yield return null;
Assert.IsEmpty(m_ExpectedEventQueue, "Not all expected callbacks were received");
}
[UnityTest]
public IEnumerator SynchronizationCallbacks_NonAuthority()
{
var authorityId = GetAuthorityNetworkManager().LocalClientId;
var peerClientId = GetNonAuthorityNetworkManager().LocalClientId;
var expectedClientId = peerClientId + 1;
var expectedPeerClientIds = m_UseHost ? new[] { authorityId, peerClientId } : new[] { peerClientId };
// Setup expected events
m_ExpectedEventQueue.Enqueue(new ExpectedEvent()
{
ConnectionEvent = new ConnectionEventData()
{
EventType = ConnectionEvent.ClientConnected,
ClientId = expectedClientId,
PeerClientIds = new NativeArray<ulong>(expectedPeerClientIds.ToArray(), Allocator.Persistent),
}
});
m_ExpectedEventQueue.Enqueue(new ExpectedEvent()
{
SceneEvent = new SceneEvent()
{
SceneEventType = SceneEventType.SynchronizeComplete,
ClientId = expectedClientId,
},
});
Assert.Null(m_ManagerToTest, "m_ManagerToTest should be null as we should be testing newly created client");
//////////////////////////////////////////
// Testing event notifications
// CreateAndStartNewClient will configure m_ManagerToTest inside OnNewClientStarted
yield return CreateAndStartNewClient();
yield return null;
Assert.IsEmpty(m_ExpectedEventQueue, "Not all expected callbacks were received");
}
[UnityTest]
public IEnumerator LateJoiningClient_PeerCallbacks()
{
SetManagerToTest(GetNonAuthorityNetworkManager());
// Setup expected events
m_ExpectedEventQueue.Enqueue(new ExpectedEvent()
{
ConnectionEvent = new ConnectionEventData()
{
EventType = ConnectionEvent.PeerConnected,
ClientId = m_ManagerToTest.LocalClientId + 1,
}
});
//////////////////////////////////////////
// Testing event notifications
yield return CreateAndStartNewClient();
yield return null;
Assert.IsEmpty(m_ExpectedEventQueue, "Not all expected callbacks were received");
}
private static void ValidateSceneEventsAreEqual(ExpectedEvent expectedEvent, SceneEvent sceneEvent)
{
Assert.NotNull(expectedEvent.SceneEvent, $"Received unexpected scene event {sceneEvent.SceneEventType} at index {s_NumEventsProcessed}");
AssertField(expectedEvent.SceneEvent.SceneEventType, sceneEvent.SceneEventType, nameof(sceneEvent.SceneEventType), sceneEvent.SceneEventType);
AssertField(expectedEvent.SceneEvent.ClientId, sceneEvent.ClientId, nameof(sceneEvent.ClientId), sceneEvent.SceneEventType);
}
private static void ValidateConnectionEventsAreEqual(ExpectedEvent expectedEvent, ConnectionEventData eventData)
{
Assert.NotNull(expectedEvent.ConnectionEvent, $"Received unexpected connection event {eventData.EventType} at index {s_NumEventsProcessed}");
AssertField(expectedEvent.ConnectionEvent.EventType, eventData.EventType, nameof(eventData.EventType), eventData.EventType);
AssertField(expectedEvent.ConnectionEvent.ClientId, eventData.ClientId, nameof(eventData.ClientId), eventData.EventType);
AssertField(expectedEvent.ConnectionEvent.PeerClientIds.Length, eventData.PeerClientIds.Length, "length of PeerClientIds", eventData.EventType);
if (eventData.PeerClientIds.Length > 0)
{
var peerIds = eventData.PeerClientIds.ToArray();
foreach (var expectedClientId in expectedEvent.ConnectionEvent.PeerClientIds)
{
Assert.Contains(expectedClientId, peerIds, "PeerClientIds does not contain all expected client IDs.");
}
}
}
private static void AssertField<T, TK>(T expected, T actual, string fieldName, TK type)
{
Assert.AreEqual(expected, actual, $"Failed on event {s_NumEventsProcessed} - {type}. Incorrect {fieldName}");
}
}
}