-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathClientSynchronizationValidationTest.cs
More file actions
175 lines (153 loc) · 7.75 KB
/
ClientSynchronizationValidationTest.cs
File metadata and controls
175 lines (153 loc) · 7.75 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
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using Unity.Netcode;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
namespace TestProject.RuntimeTests
{
public class ClientSynchronizationValidationTest : NetcodeIntegrationTest
{
protected override int NumberOfClients => 0;
private const string k_FirstSceneToLoad = "UnitTestBaseScene";
private const string k_SecondSceneToLoad = "InSceneNetworkObject";
private const string k_ThirdSceneToSkip = "EmptyScene";
private Scene m_RuntimeGeneratedScene;
private bool m_IncludeSceneVerificationHandler;
private bool m_RuntimeSceneWasExcludedFromSynch;
private List<ClientSceneVerificationHandler> m_ClientSceneVerifiers = new List<ClientSceneVerificationHandler>();
protected override void OnNewClientStarted(NetworkManager networkManager)
{
if (m_IncludeSceneVerificationHandler)
{
m_ClientSceneVerifiers.Add(new ClientSceneVerificationHandler(networkManager));
}
base.OnNewClientStarted(networkManager);
}
/// <summary>
/// Handle excluding runtime scene from synchronization
/// </summary>
private bool OnServerVerifySceneBeforeLoading(int sceneIndex, string sceneName, LoadSceneMode loadSceneMode)
{
// exclude test runner scene
if (sceneName.StartsWith(NetcodeIntegrationTestHelpers.FirstPartOfTestRunnerSceneName))
{
return false;
}
// Exclude the runtime generated scene
if (sceneIndex == m_RuntimeGeneratedScene.buildIndex && m_RuntimeGeneratedScene.name == sceneName)
{
m_RuntimeSceneWasExcludedFromSynch = true;
return false;
}
return true;
}
/// <summary>
/// Test that validates users can exclude runtime generated scenes from the initial client synchronization
/// process using <see cref="NetworkSceneManager.VerifySceneBeforeLoading"/>
/// </summary>
[UnityTest]
public IEnumerator ClientSynchWithServerSideRuntimeGeneratedScene()
{
m_IncludeSceneVerificationHandler = false;
m_ServerNetworkManager.SceneManager.VerifySceneBeforeLoading = OnServerVerifySceneBeforeLoading;
m_ServerNetworkManager.SceneManager.DisableValidationWarnings(true);
// For this test we want to disable the check for scenes in build list
m_ServerNetworkManager.SceneManager.ExcludeSceneFromSychronization = null;
// Create a runtime scene in the server side
m_RuntimeGeneratedScene = SceneManager.CreateScene("RuntimeGeneratedScene");
yield return s_DefaultWaitForTick;
yield return CreateAndStartNewClient();
Assert.True(m_RuntimeSceneWasExcludedFromSynch, $"Server did not exclude the runtime generated scene when creating synchronization message data!");
}
/// <summary>
/// Validates that connecting clients will exclude scenes using <see cref="NetworkSceneManager.VerifySceneBeforeLoading"/>
/// </summary>
[UnityTest]
public IEnumerator ClientVerifySceneBeforeLoading()
{
m_IncludeSceneVerificationHandler = true;
var scenesToLoad = new List<string>() { k_FirstSceneToLoad, k_SecondSceneToLoad, k_ThirdSceneToSkip };
m_ServerNetworkManager.SceneManager.OnLoadComplete += OnLoadComplete;
foreach (var sceneToLoad in scenesToLoad)
{
m_SceneBeingLoadedIsLoaded = false;
m_SceneBeingLoaded = sceneToLoad;
m_ServerNetworkManager.SceneManager.LoadScene(sceneToLoad, LoadSceneMode.Additive);
yield return WaitForConditionOrTimeOut(() => m_SceneBeingLoadedIsLoaded);
AssertOnTimeout($"Timed out waiting for scene {m_SceneBeingLoaded} to finish loading!");
}
yield return CreateAndStartNewClient();
yield return WaitForConditionOrTimeOut(m_ClientSceneVerifiers[0].HasLoadedExpectedScenes);
AssertOnTimeout($"Timed out waiting for the client to have loaded the expected scenes");
// Check to make sure only the two scenes were loaded and one
// completely skipped.
foreach (var clientSceneVerifier in m_ClientSceneVerifiers)
{
clientSceneVerifier.ValidateScenesLoaded();
}
}
private string m_SceneBeingLoaded;
private bool m_SceneBeingLoadedIsLoaded;
private void OnLoadComplete(ulong clientId, string sceneName, LoadSceneMode loadSceneMode)
{
if (m_SceneBeingLoaded == sceneName)
{
m_SceneBeingLoadedIsLoaded = true;
}
}
/// <summary>
/// Determines if all clients loaded only two of the 3 scenes
/// </summary>
private class ClientSceneVerificationHandler
{
private NetworkManager m_NetworkManager;
private Dictionary<string, int> m_ValidSceneEventCount = new Dictionary<string, int>();
public ClientSceneVerificationHandler(NetworkManager networkManager)
{
m_NetworkManager = networkManager;
m_NetworkManager.SceneManager.DisableValidationWarnings(true);
m_NetworkManager.SceneManager.VerifySceneBeforeLoading = VerifySceneBeforeLoading;
m_NetworkManager.SceneManager.OnLoad += ClientSceneManager_OnLoad;
m_NetworkManager.SceneManager.OnLoadComplete += ClientSceneManager_OnLoadComplete;
m_ValidSceneEventCount.Add(k_FirstSceneToLoad, 0);
m_ValidSceneEventCount.Add(k_SecondSceneToLoad, 0);
m_ValidSceneEventCount.Add(k_ThirdSceneToSkip, 0);
}
public bool HasLoadedExpectedScenes()
{
return m_ValidSceneEventCount[k_FirstSceneToLoad] == 2 && m_ValidSceneEventCount[k_SecondSceneToLoad] == 2;
}
public void ValidateScenesLoaded()
{
Assert.IsTrue(m_ValidSceneEventCount[k_ThirdSceneToSkip] == 0, $"Client still loaded the invalidated scene {k_ThirdSceneToSkip}!");
Assert.IsTrue(m_ValidSceneEventCount[k_FirstSceneToLoad] == 2, $"Client did not load and process the validated scene {k_FirstSceneToLoad}! Expected (1) but was ({m_ValidSceneEventCount[k_FirstSceneToLoad]})");
Assert.IsTrue(m_ValidSceneEventCount[k_SecondSceneToLoad] == 2, $"Client did not load and process the validated scene {k_SecondSceneToLoad}! Expected (1) but was ({m_ValidSceneEventCount[k_SecondSceneToLoad]})");
}
private void ClientSceneManager_OnLoadComplete(ulong clientId, string sceneName, LoadSceneMode loadSceneMode)
{
if (m_ValidSceneEventCount.ContainsKey(sceneName))
{
m_ValidSceneEventCount[sceneName]++;
}
}
private void ClientSceneManager_OnLoad(ulong clientId, string sceneName, LoadSceneMode loadSceneMode, AsyncOperation asyncOperation)
{
if (m_ValidSceneEventCount.ContainsKey(sceneName))
{
m_ValidSceneEventCount[sceneName]++;
}
}
private bool VerifySceneBeforeLoading(int sceneIndex, string sceneName, LoadSceneMode loadSceneMode)
{
if (sceneName == k_ThirdSceneToSkip)
{
return false;
}
return true;
}
}
}
}