-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathNetworkVarBufferCopyTest.cs
More file actions
187 lines (157 loc) · 7.38 KB
/
NetworkVarBufferCopyTest.cs
File metadata and controls
187 lines (157 loc) · 7.38 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
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine.TestTools;
namespace Unity.Netcode.RuntimeTests
{
[TestFixture(HostOrServer.DAHost)]
[TestFixture(HostOrServer.Host)]
internal class NetworkVarBufferCopyTest : NetcodeIntegrationTest
{
internal class DummyNetVar : NetworkVariableBase
{
private const int k_DummyValue = 0x13579BDF;
public bool DeltaWritten;
public bool FieldWritten;
public bool DeltaRead;
public bool FieldRead;
public override void WriteDelta(FastBufferWriter writer)
{
writer.TryBeginWrite(FastBufferWriter.GetWriteSize(k_DummyValue) + 1);
using (var bitWriter = writer.EnterBitwiseContext())
{
bitWriter.WriteBits(1, 1);
}
writer.WriteValue(k_DummyValue);
DeltaWritten = true;
}
public override void WriteField(FastBufferWriter writer)
{
writer.TryBeginWrite(FastBufferWriter.GetWriteSize(k_DummyValue) + 1);
using (var bitWriter = writer.EnterBitwiseContext())
{
bitWriter.WriteBits(1, 1);
}
writer.WriteValue(k_DummyValue);
FieldWritten = true;
}
public override void ReadField(FastBufferReader reader)
{
reader.TryBeginRead(FastBufferWriter.GetWriteSize(k_DummyValue) + 1);
using (var bitReader = reader.EnterBitwiseContext())
{
bitReader.ReadBits(out byte b, 1);
}
reader.ReadValue(out int i);
Assert.AreEqual(k_DummyValue, i);
FieldRead = true;
}
public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
{
reader.TryBeginRead(FastBufferWriter.GetWriteSize(k_DummyValue) + 1);
using (var bitReader = reader.EnterBitwiseContext())
{
bitReader.ReadBits(out byte b, 1);
}
reader.ReadValue(out int i);
Assert.AreEqual(k_DummyValue, i);
DeltaRead = true;
}
public DummyNetVar(
NetworkVariableReadPermission readPerm = DefaultReadPerm,
NetworkVariableWritePermission writePerm = DefaultWritePerm) : base(readPerm, writePerm) { }
}
internal class DummyNetBehaviour : NetworkBehaviour
{
public static bool DistributedAuthority;
internal DummyNetVar NetVar;
private void Awake()
{
if (DistributedAuthority)
{
NetVar = new DummyNetVar(writePerm: NetworkVariableWritePermission.Owner);
}
else
{
NetVar = new DummyNetVar();
}
}
public override void OnNetworkSpawn()
{
if ((NetworkManager.DistributedAuthorityMode && !IsOwner) || (!NetworkManager.DistributedAuthorityMode && !IsServer))
{
ClientDummyNetBehaviourSpawned(this);
}
base.OnNetworkSpawn();
}
}
protected override int NumberOfClients => 1;
public NetworkVarBufferCopyTest(HostOrServer hostOrServer) : base(hostOrServer)
{
}
private static List<DummyNetBehaviour> s_ClientDummyNetBehavioursSpawned = new List<DummyNetBehaviour>();
public static void ClientDummyNetBehaviourSpawned(DummyNetBehaviour dummyNetBehaviour)
{
s_ClientDummyNetBehavioursSpawned.Add(dummyNetBehaviour);
}
protected override IEnumerator OnSetup()
{
s_ClientDummyNetBehavioursSpawned.Clear();
return base.OnSetup();
}
protected override void OnCreatePlayerPrefab()
{
DummyNetBehaviour.DistributedAuthority = m_DistributedAuthority;
m_PlayerPrefab.AddComponent<DummyNetBehaviour>();
}
[UnityTest]
public IEnumerator TestEntireBufferIsCopiedOnNetworkVariableDelta()
{
// This is the *SERVER/SESSION OWNER VERSION* of the *CLIENT PLAYER*
var authority = GetAuthorityNetworkManager();
var nonAuthority = GetNonAuthorityNetworkManager();
var serverClientPlayerResult = new NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject>();
yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation(
x => x.IsPlayerObject && x.OwnerClientId == nonAuthority.LocalClientId,
authority, serverClientPlayerResult);
var serverSideClientPlayer = serverClientPlayerResult.Result;
var serverComponent = serverSideClientPlayer.GetComponent<DummyNetBehaviour>();
// This is the *CLIENT VERSION* of the *CLIENT PLAYER*
var clientClientPlayerResult = new NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject>();
yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation(
x => x.IsPlayerObject && x.OwnerClientId == nonAuthority.LocalClientId,
nonAuthority, clientClientPlayerResult);
var clientSideClientPlayer = clientClientPlayerResult.Result;
var clientComponent = clientSideClientPlayer.GetComponent<DummyNetBehaviour>();
// Wait for the DummyNetBehaviours on the client side to notify they have been initialized and spawned
yield return WaitForConditionOrTimeOut(() => s_ClientDummyNetBehavioursSpawned.Count >= 1);
Assert.IsFalse(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for client side DummyNetBehaviour to register it was spawned!");
// Check that FieldWritten is written when dirty
var authorityComponent = m_DistributedAuthority ? clientComponent : serverComponent;
var nonAuthorityComponent = m_DistributedAuthority ? serverComponent : clientComponent;
authorityComponent.NetVar.SetDirty(true);
yield return s_DefaultWaitForTick;
Assert.True(authorityComponent.NetVar.FieldWritten);
// Check that DeltaWritten is written when dirty
authorityComponent.NetVar.SetDirty(true);
yield return s_DefaultWaitForTick;
Assert.True(authorityComponent.NetVar.DeltaWritten);
// Check that both FieldRead and DeltaRead were invoked on the client side
yield return WaitForConditionOrTimeOut(() => nonAuthorityComponent.NetVar.FieldRead == true && nonAuthorityComponent.NetVar.DeltaRead == true);
var timedOutMessage = "Timed out waiting for client reads: ";
if (s_GlobalTimeoutHelper.TimedOut)
{
if (!nonAuthorityComponent.NetVar.FieldRead)
{
timedOutMessage += "[FieldRead]";
}
if (!nonAuthorityComponent.NetVar.DeltaRead)
{
timedOutMessage += "[DeltaRead]";
}
}
Assert.IsFalse(s_GlobalTimeoutHelper.TimedOut, timedOutMessage);
}
}
}