Tests: SerializationException cleanup to prepare for SyncDirection

This commit is contained in:
vis2k 2022-02-16 21:38:54 +08:00
parent a45d3adf42
commit 61a98fd97b

View File

@ -63,47 +63,49 @@ public void OnSerializeAndDeserializeAllSafely()
[Test] [Test]
public void SerializationException() public void SerializationException()
{ {
CreateNetworked(out GameObject _, out NetworkIdentity identity, // the exception component will log exception errors all the way
out SerializeExceptionNetworkBehaviour compExc, // through this function, starting from spawning where it's
out SerializeTest2NetworkBehaviour comp2); // serialized for the first time.
// set some unique values to serialize
compExc.syncMode = SyncMode.Observers;
comp2.value = "67890";
comp2.syncMode = SyncMode.Owner;
// serialize all - should work even if compExc throws an exception
// error log because of the exception is expected
LogAssert.ignoreFailingMessages = true; LogAssert.ignoreFailingMessages = true;
identity.OnSerializeAllSafely(true, ownerWriter, observersWriter);
LogAssert.ignoreFailingMessages = false;
// owner & observers should have written something // need two of both versions so we can serialize -> deserialize
Assert.That(ownerWriter.Position, Is.GreaterThan(0)); // spawning the exception component will already show an exception.
Assert.That(observersWriter.Position, Is.GreaterThan(0)); // ignore it.
CreateNetworkedAndSpawn(
out _, out NetworkIdentity serverIdentity, out SerializeExceptionNetworkBehaviour serverCompExc, out SerializeTest2NetworkBehaviour serverComp2,
out _, out NetworkIdentity clientIdentity, out SerializeExceptionNetworkBehaviour clientCompExc, out SerializeTest2NetworkBehaviour clientComp2);
// reset component values // set sync modes
comp2.value = null; serverCompExc.syncMode = clientCompExc.syncMode = SyncMode.Observers;
serverComp2.syncMode = clientComp2.syncMode = SyncMode.Owner;
// deserialize all for owner - should work even if compExc throws an exception // set unique values on server components
serverComp2.value = "42";
// serialize server object
// should work even if compExc throws an exception.
// error log because of the exception is expected.
serverIdentity.OnSerializeAllSafely(true, ownerWriter, observersWriter);
// deserialize client object with OWNER payload
// should work even if compExc throws an exception
// error log because of the exception is expected
NetworkReader reader = new NetworkReader(ownerWriter.ToArray()); NetworkReader reader = new NetworkReader(ownerWriter.ToArray());
// error log because of the exception is expected clientIdentity.OnDeserializeAllSafely(reader, true);
LogAssert.ignoreFailingMessages = true; Assert.That(clientComp2.value, Is.EqualTo("42"));
identity.OnDeserializeAllSafely(reader, true);
LogAssert.ignoreFailingMessages = false;
Assert.That(comp2.value, Is.EqualTo("67890"));
// reset component values // reset component values
comp2.value = null; clientComp2.value = null;
// deserialize all for observers - should work even if compExc throws an exception // deserialize client object with OBSERVER payload
reader = new NetworkReader(observersWriter.ToArray()); // should work even if compExc throws an exception
// error log because of the exception is expected // error log because of the exception is expected
LogAssert.ignoreFailingMessages = true; reader = new NetworkReader(observersWriter.ToArray());
identity.OnDeserializeAllSafely(reader, true); clientIdentity.OnDeserializeAllSafely(reader, true);
Assert.That(clientComp2.value, Is.EqualTo(null)); // owner mode should be in data
// restore error checks
LogAssert.ignoreFailingMessages = false; LogAssert.ignoreFailingMessages = false;
// owner mode, should not be in data
Assert.That(comp2.value, Is.EqualTo(null));
} }
// OnSerializeAllSafely supports at max 64 components, because our // OnSerializeAllSafely supports at max 64 components, because our