-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathNetworkManagerTests.cs
More file actions
182 lines (158 loc) · 7.14 KB
/
NetworkManagerTests.cs
File metadata and controls
182 lines (158 loc) · 7.14 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
using System.Collections;
using System.Linq;
using NUnit.Framework;
using Unity.Netcode;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
namespace TestProject.RuntimeTests
{
[TestFixture(UseSceneManagement.SceneManagementDisabled)]
[TestFixture(UseSceneManagement.SceneManagementEnabled)]
public class NetworkManagerTests : NetcodeIntegrationTest
{
private const string k_SceneToLoad = "InSceneNetworkObject";
protected override int NumberOfClients => 0;
public enum UseSceneManagement
{
SceneManagementEnabled,
SceneManagementDisabled
}
private NetworkObject m_NetworkObject;
private bool m_NetworkObjectWasSpawned;
private bool m_NetworkBehaviourIsHostWasSet;
private bool m_NetworkBehaviourIsClientWasSet;
private bool m_NetworkBehaviourIsServerWasSet;
private int m_NumberOfTimesInvoked;
private AsyncOperation m_AsyncOperation;
private NetworkObjectTestComponent m_NetworkObjectTestComponent;
private bool m_UseSceneManagement;
public NetworkManagerTests(UseSceneManagement useSceneManagement)
{
m_UseSceneManagement = useSceneManagement == UseSceneManagement.SceneManagementEnabled;
}
private void OnClientConnectedCallback(NetworkObject networkObject, int numberOfTimesInvoked, bool isHost, bool isClient, bool isServer)
{
if (networkObject != null)
{
m_NetworkObject = networkObject;
m_NetworkObjectWasSpawned = networkObject.IsSpawned;
m_NetworkBehaviourIsHostWasSet = isHost;
m_NetworkBehaviourIsClientWasSet = isClient;
m_NetworkBehaviourIsServerWasSet = isServer;
m_NumberOfTimesInvoked = numberOfTimesInvoked;
}
}
private bool TestComponentFound()
{
if (!m_AsyncOperation.isDone)
{
return false;
}
#if UNITY_2023_1_OR_NEWER
m_NetworkObjectTestComponent = Object.FindFirstObjectByType<NetworkObjectTestComponent>();
#else
m_NetworkObjectTestComponent = Object.FindObjectOfType<NetworkObjectTestComponent>();
#endif
if (m_NetworkObjectTestComponent == null)
{
return false;
}
return true;
}
protected override IEnumerator OnSetup()
{
m_AsyncOperation = SceneManager.LoadSceneAsync(k_SceneToLoad, LoadSceneMode.Additive);
yield return WaitForConditionOrTimeOut(TestComponentFound);
AssertOnTimeout($"Failed to find {nameof(NetworkObjectTestComponent)} after loading test scene {k_SceneToLoad}");
}
protected override IEnumerator OnTearDown()
{
SceneManager.UnloadSceneAsync(SceneManager.GetSceneByName(k_SceneToLoad));
yield return s_DefaultWaitForTick;
}
protected override void OnServerAndClientsCreated()
{
m_ServerNetworkManager.NetworkConfig.EnableSceneManagement = m_UseSceneManagement;
m_NetworkObjectTestComponent.ConfigureClientConnected(m_ServerNetworkManager, OnClientConnectedCallback);
}
[Test]
public void ValidateHostSettings()
{
Assert.IsTrue(m_ServerNetworkManager.LocalClient != null);
Assert.IsTrue(m_NetworkObjectWasSpawned, $"{m_NetworkObject.name} was not spawned when OnClientConnectedCallback was invoked!");
Assert.IsTrue(m_NetworkBehaviourIsHostWasSet, $"IsHost was not true when OnClientConnectedCallback was invoked!");
Assert.IsTrue(m_NetworkBehaviourIsClientWasSet, $"IsClient was not true when OnClientConnectedCallback was invoked!");
Assert.IsTrue(m_NumberOfTimesInvoked == 1, $"OnClientConnectedCallback was invoked {m_NumberOfTimesInvoked} as opposed to just once!");
Assert.IsTrue(m_NetworkBehaviourIsServerWasSet, $"IsServer was not true when OnClientConnectedCallback was invoked!");
}
public enum ShutdownChecks
{
Server,
Client
}
protected override void OnNewClientCreated(NetworkManager networkManager)
{
networkManager.NetworkConfig.EnableSceneManagement = m_UseSceneManagement;
base.OnNewClientCreated(networkManager);
}
/// <summary>
/// Validate shutting down a second time does not cause an exception.
/// </summary>
[UnityTest]
public IEnumerator ValidateShutdown([Values] ShutdownChecks shutdownCheck)
{
if (shutdownCheck == ShutdownChecks.Server)
{
// Register for the server stopped notification so we know we have shutdown completely
m_ServerNetworkManager.OnServerStopped += OnServerStopped;
// Shutdown
m_ServerNetworkManager.Shutdown();
}
else
{
// For this test (simplify the complexity) with a late joining client, just remove the
// in-scene placed NetworkObject prior to the client connecting
// (We are testing the shutdown sequence)
var spawnedObjects = m_ServerNetworkManager.SpawnManager.SpawnedObjectsList.ToList();
for (int i = spawnedObjects.Count - 1; i >= 0; i--)
{
var spawnedObject = spawnedObjects[i];
if (spawnedObject.IsSceneObject != null && spawnedObject.IsSceneObject.Value)
{
spawnedObject.Despawn();
}
}
yield return s_DefaultWaitForTick;
yield return CreateAndStartNewClient();
// Register for the server stopped notification so we know we have shutdown completely
m_ClientNetworkManagers[0].OnClientStopped += OnClientStopped;
m_ClientNetworkManagers[0].Shutdown();
}
// Let the network manager instance shutdown
yield return s_DefaultWaitForTick;
// Validate the shutdown is no longer in progress
if (shutdownCheck == ShutdownChecks.Server)
{
Assert.False(m_ServerNetworkManager.ShutdownInProgress, $"[{shutdownCheck}] Shutdown in progress was still detected!");
}
else
{
Assert.False(m_ClientNetworkManagers[0].ShutdownInProgress, $"[{shutdownCheck}] Shutdown in progress was still detected!");
}
}
private void OnClientStopped(bool obj)
{
m_ServerNetworkManager.OnServerStopped -= OnClientStopped;
// Verify that we can invoke shutdown again without an exception
m_ServerNetworkManager.Shutdown();
}
private void OnServerStopped(bool obj)
{
m_ServerNetworkManager.OnServerStopped -= OnServerStopped;
// Verify that we can invoke shutdown again without an exception
m_ServerNetworkManager.Shutdown();
}
}
}