NetworkServerTest: use MirrorTest CreateNetworked

This commit is contained in:
vis2k 2021-05-19 19:39:20 +08:00
parent f6c13424cf
commit 181464521c

View File

@ -539,16 +539,13 @@ public void CommandMessageCallsCommand()
connection.isAuthenticated = true; connection.isAuthenticated = true;
// add an identity with two networkbehaviour components // add an identity with two networkbehaviour components
GameObject go = new GameObject(); CreateNetworked(out GameObject go, out NetworkIdentity identity, out CommandTestNetworkBehaviour comp0, out CommandTestNetworkBehaviour comp1);
NetworkIdentity identity = go.AddComponent<NetworkIdentity>();
identity.netId = 42; identity.netId = 42;
// for authority check // for authority check
identity.connectionToClient = connection; identity.connectionToClient = connection;
CommandTestNetworkBehaviour comp0 = go.AddComponent<CommandTestNetworkBehaviour>();
Assert.That(comp0.called, Is.EqualTo(0));
CommandTestNetworkBehaviour comp1 = go.AddComponent<CommandTestNetworkBehaviour>();
Assert.That(comp1.called, Is.EqualTo(0));
connection.identity = identity; connection.identity = identity;
Assert.That(comp0.called, Is.EqualTo(0));
Assert.That(comp1.called, Is.EqualTo(0));
// register the command delegate, otherwise it's not found // register the command delegate, otherwise it's not found
int registeredHash = RemoteCallHelper.RegisterDelegate(typeof(CommandTestNetworkBehaviour), int registeredHash = RemoteCallHelper.RegisterDelegate(typeof(CommandTestNetworkBehaviour),
@ -623,11 +620,6 @@ public void CommandMessageCallsCommand()
NetworkIdentity.spawned.Clear(); NetworkIdentity.spawned.Clear();
RemoteCallHelper.RemoveDelegate(registeredHash); RemoteCallHelper.RemoveDelegate(registeredHash);
NetworkServer.Shutdown(); NetworkServer.Shutdown();
// destroy the test gameobject AFTER server was stopped.
// otherwise isServer is true in OnDestroy, which means it would try
// to call Destroy(go). but we need to use DestroyImmediate in
// Editor
GameObject.DestroyImmediate(go);
} }
[Test] [Test]
@ -647,11 +639,6 @@ public void ActivateHostSceneCallsOnStartClient()
// clean up // clean up
NetworkIdentity.spawned.Clear(); NetworkIdentity.spawned.Clear();
NetworkServer.Shutdown(); NetworkServer.Shutdown();
// destroy the test gameobject AFTER server was stopped.
// otherwise isServer is true in OnDestroy, which means it would try
// to call Destroy(go). but we need to use DestroyImmediate in
// Editor
GameObject.DestroyImmediate(go);
} }
[Test] [Test]
@ -733,7 +720,6 @@ public void RegisterUnregisterClearHandler()
[Test] [Test]
public void SendToClientOfPlayer() public void SendToClientOfPlayer()
{ {
// listen // listen
NetworkServer.Listen(1); NetworkServer.Listen(1);
Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0));
@ -753,7 +739,7 @@ public void SendToClientOfPlayer()
TestMessage1 message = new TestMessage1 { IntValue = 1, DoubleValue = 2, StringValue = "3" }; TestMessage1 message = new TestMessage1 { IntValue = 1, DoubleValue = 2, StringValue = "3" };
// create a gameobject and networkidentity // create a gameobject and networkidentity
NetworkIdentity identity = new GameObject().AddComponent<NetworkIdentity>(); CreateNetworked(out GameObject _, out NetworkIdentity identity);
identity.connectionToClient = connection; identity.connectionToClient = connection;
// send it to that player // send it to that player
@ -767,25 +753,18 @@ public void SendToClientOfPlayer()
// clean up // clean up
NetworkServer.Shutdown(); NetworkServer.Shutdown();
// destroy GO after shutdown, otherwise isServer is true in OnDestroy and it tries to call
// GameObject.Destroy (but we need DestroyImmediate in Editor)
GameObject.DestroyImmediate(identity.gameObject);
} }
[Test] [Test]
public void GetNetworkIdentityShouldFindNetworkIdentity() public void GetNetworkIdentityShouldFindNetworkIdentity()
{ {
// create a GameObject with NetworkIdentity // create a GameObject with NetworkIdentity
GameObject go = new GameObject(); CreateNetworked(out GameObject go, out NetworkIdentity identity);
NetworkIdentity identity = go.AddComponent<NetworkIdentity>();
// GetNetworkIdentity // GetNetworkIdentity
bool result = NetworkServer.GetNetworkIdentity(go, out NetworkIdentity value); bool result = NetworkServer.GetNetworkIdentity(go, out NetworkIdentity value);
Assert.That(result, Is.True); Assert.That(result, Is.True);
Assert.That(value, Is.EqualTo(identity)); Assert.That(value, Is.EqualTo(identity));
// clean up
GameObject.DestroyImmediate(go);
} }
[Test] [Test]
@ -807,7 +786,6 @@ public void GetNetworkIdentityErrorIfNotFound()
[Test] [Test]
public void ShowForConnection() public void ShowForConnection()
{ {
// listen // listen
NetworkServer.Listen(1); NetworkServer.Listen(1);
Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0));
@ -826,7 +804,7 @@ public void ShowForConnection()
NetworkServer.AddConnection(connection); NetworkServer.AddConnection(connection);
// create a gameobject and networkidentity and some unique values // create a gameobject and networkidentity and some unique values
NetworkIdentity identity = new GameObject().AddComponent<NetworkIdentity>(); CreateNetworked(out GameObject _, out NetworkIdentity identity);
identity.connectionToClient = connection; identity.connectionToClient = connection;
// call ShowForConnection // call ShowForConnection
@ -847,9 +825,6 @@ public void ShowForConnection()
// clean up // clean up
NetworkServer.Shutdown(); NetworkServer.Shutdown();
// destroy GO after shutdown, otherwise isServer is true in OnDestroy and it tries to call
// GameObject.Destroy (but we need DestroyImmediate in Editor)
GameObject.DestroyImmediate(identity.gameObject);
} }
[Test] [Test]
@ -873,7 +848,7 @@ public void HideForConnection()
NetworkServer.AddConnection(connection); NetworkServer.AddConnection(connection);
// create a gameobject and networkidentity // create a gameobject and networkidentity
NetworkIdentity identity = new GameObject().AddComponent<NetworkIdentity>(); CreateNetworked(out GameObject _, out NetworkIdentity identity);
identity.connectionToClient = connection; identity.connectionToClient = connection;
// call HideForConnection // call HideForConnection
@ -887,17 +862,13 @@ public void HideForConnection()
// clean up // clean up
NetworkServer.Shutdown(); NetworkServer.Shutdown();
// destroy GO after shutdown, otherwise isServer is true in OnDestroy and it tries to call
// GameObject.Destroy (but we need DestroyImmediate in Editor)
GameObject.DestroyImmediate(identity.gameObject);
} }
[Test] [Test]
public void ValidateSceneObject() public void ValidateSceneObject()
{ {
// create a gameobject and networkidentity // create a gameobject and networkidentity
GameObject go = new GameObject(); CreateNetworked(out GameObject go, out NetworkIdentity identity);
NetworkIdentity identity = go.AddComponent<NetworkIdentity>();
identity.sceneId = 42; identity.sceneId = 42;
// should be valid as long as it has a sceneId // should be valid as long as it has a sceneId
@ -913,25 +884,20 @@ public void ValidateSceneObject()
Assert.That(NetworkServer.ValidateSceneObject(identity), Is.False); Assert.That(NetworkServer.ValidateSceneObject(identity), Is.False);
go.hideFlags = HideFlags.HideAndDontSave; go.hideFlags = HideFlags.HideAndDontSave;
Assert.That(NetworkServer.ValidateSceneObject(identity), Is.False); Assert.That(NetworkServer.ValidateSceneObject(identity), Is.False);
// clean up
GameObject.DestroyImmediate(go);
} }
[Test] [Test]
public void SpawnObjects() public void SpawnObjects()
{ {
// create a gameobject and networkidentity that lives in the scene(=has sceneid) // create a gameobject and networkidentity that lives in the scene(=has sceneid)
GameObject go = new GameObject("Test"); CreateNetworked(out GameObject go, out NetworkIdentity identity);
NetworkIdentity identity = go.AddComponent<NetworkIdentity>();
// lives in the scene from the start // lives in the scene from the start
identity.sceneId = 42; identity.sceneId = 42;
// unspawned scene objects are set to inactive before spawning // unspawned scene objects are set to inactive before spawning
go.SetActive(false); go.SetActive(false);
// create a gameobject that looks like it was instantiated and doesn't live in the scene // create a gameobject that looks like it was instantiated and doesn't live in the scene
GameObject go2 = new GameObject("Test2"); CreateNetworked(out GameObject go2, out NetworkIdentity identity2);
NetworkIdentity identity2 = go2.AddComponent<NetworkIdentity>();
// not a scene object // not a scene object
identity2.sceneId = 0; identity2.sceneId = 0;
// unspawned scene objects are set to inactive before spawning // unspawned scene objects are set to inactive before spawning
@ -956,8 +922,6 @@ public void SpawnObjects()
identity.isServer = false; identity.isServer = false;
identity2.isServer = false; identity2.isServer = false;
NetworkServer.Shutdown(); NetworkServer.Shutdown();
GameObject.DestroyImmediate(go);
GameObject.DestroyImmediate(go2);
// need to clear spawned list as SpawnObjects adds items to that list // need to clear spawned list as SpawnObjects adds items to that list
NetworkIdentity.spawned.Clear(); NetworkIdentity.spawned.Clear();
} }
@ -966,9 +930,7 @@ public void SpawnObjects()
public void UnSpawn() public void UnSpawn()
{ {
// create a gameobject and networkidentity that lives in the scene(=has sceneid) // create a gameobject and networkidentity that lives in the scene(=has sceneid)
GameObject go = new GameObject("Test"); CreateNetworked(out GameObject go, out NetworkIdentity identity, out OnStopClientTestNetworkBehaviour comp);
NetworkIdentity identity = go.AddComponent<NetworkIdentity>();
OnStopClientTestNetworkBehaviour comp = go.AddComponent<OnStopClientTestNetworkBehaviour>();
// lives in the scene from the start // lives in the scene from the start
identity.sceneId = 42; identity.sceneId = 42;
// spawned objects are active // spawned objects are active
@ -980,15 +942,11 @@ public void UnSpawn()
// it should have been reset now // it should have been reset now
Assert.That(identity.netId, Is.Zero); Assert.That(identity.netId, Is.Zero);
// clean up
GameObject.DestroyImmediate(go);
} }
[Test] [Test]
public void ShutdownCleanup() public void ShutdownCleanup()
{ {
// listen // listen
NetworkServer.Listen(1); NetworkServer.Listen(1);
Assert.That(NetworkServer.active, Is.True); Assert.That(NetworkServer.active, Is.True);
@ -1033,7 +991,6 @@ public void SendCalledWhileNotActive_ShouldGiveWarning(string functionName)
} }
} }
[Test] [Test]
public void NoExternalConnectionsTest_WithNoConnection() public void NoExternalConnectionsTest_WithNoConnection()
{ {
@ -1122,8 +1079,7 @@ public void UpdateDetectsDestroyedEntryInObserving()
NetworkServer.Listen(1); NetworkServer.Listen(1);
// add a connection that is observed by a destroyed entity // add a connection that is observed by a destroyed entity
GameObject go = new GameObject(); CreateNetworked(out GameObject go, out NetworkIdentity ni);
NetworkIdentity ni = go.AddComponent<NetworkIdentity>();
NetworkServer.connections[42] = new FakeNetworkConnection{isReady=true}; NetworkServer.connections[42] = new FakeNetworkConnection{isReady=true};
NetworkServer.connections[42].observing.Add(ni); NetworkServer.connections[42].observing.Add(ni);
GameObject.DestroyImmediate(go); GameObject.DestroyImmediate(go);