NetworkInputTests.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <CommonHierarchySetup.h>
  8. #include <MockInterfaces.h>
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/Console/Console.h>
  11. #include <AzCore/Name/Name.h>
  12. #include <AzCore/UnitTest/TestTypes.h>
  13. #include <AzCore/UnitTest/UnitTest.h>
  14. #include <AzFramework/Components/TransformComponent.h>
  15. #include <AzTest/AzTest.h>
  16. #include <Multiplayer/Components/NetBindComponent.h>
  17. #include <Multiplayer/NetworkInput/NetworkInput.h>
  18. #include <Multiplayer/NetworkEntity/EntityReplication/EntityReplicator.h>
  19. #include <Multiplayer/NetworkInput/NetworkInputArray.h>
  20. #include <Multiplayer/NetworkInput/NetworkInputHistory.h>
  21. #include <Multiplayer/NetworkInput/NetworkInputMigrationVector.h>
  22. namespace Multiplayer
  23. {
  24. using namespace testing;
  25. using namespace ::UnitTest;
  26. class NetworkInputTests : public HierarchyTests
  27. {
  28. public:
  29. void SetUp() override
  30. {
  31. HierarchyTests::SetUp();
  32. m_console->PerformCommand("net_useInputDeltaSerialization true");
  33. m_root = AZStd::make_unique<EntityInfo>(1, "root", NetEntityId{ 1 }, EntityInfo::Role::Root);
  34. PopulateNetworkEntity(*m_root);
  35. SetupEntity(m_root->m_entity, m_root->m_netId, NetEntityRole::Authority);
  36. // Create an entity replicator for the root entity
  37. const NetworkEntityHandle rootHandle(m_root->m_entity.get(), m_networkEntityTracker.get());
  38. m_root->m_replicator = AZStd::make_unique<EntityReplicator>(
  39. *m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Client, rootHandle);
  40. m_root->m_replicator->Initialize(rootHandle);
  41. m_root->m_entity->Activate();
  42. }
  43. void TearDown() override
  44. {
  45. m_root.reset();
  46. HierarchyTests::TearDown();
  47. }
  48. void PopulateNetworkEntity(const EntityInfo& entityInfo)
  49. {
  50. entityInfo.m_entity->CreateComponent<AzFramework::TransformComponent>();
  51. entityInfo.m_entity->CreateComponent<NetBindComponent>();
  52. entityInfo.m_entity->CreateComponent<NetworkTransformComponent>();
  53. }
  54. AZStd::unique_ptr<EntityInfo> m_root;
  55. };
  56. constexpr float BLEND_FACTOR_SCALE = 1.1f;
  57. constexpr uint32_t TIME_SCALE = 10;
  58. TEST_F(NetworkInputTests, NetworkInputMembers)
  59. {
  60. const NetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityTracker.get());
  61. NetworkInputArray inArray = NetworkInputArray(handle);
  62. for (uint32_t i = 0; i < NetworkInputArray::MaxElements; ++i)
  63. {
  64. inArray[i].SetClientInputId(ClientInputId(i));
  65. inArray[i].SetHostFrameId(HostFrameId(i));
  66. inArray[i].SetHostBlendFactor(i * BLEND_FACTOR_SCALE);
  67. inArray[i].SetHostTimeMs(AZ::TimeMs(i * TIME_SCALE));
  68. EXPECT_EQ(inArray[i].GetClientInputId(), ClientInputId(i));
  69. EXPECT_EQ(inArray[i].GetHostFrameId(), HostFrameId(i));
  70. EXPECT_NEAR(inArray[i].GetHostBlendFactor(), i * BLEND_FACTOR_SCALE, 0.001f);
  71. EXPECT_EQ(inArray[i].GetHostTimeMs(), AZ::TimeMs(i * TIME_SCALE));
  72. }
  73. for (uint32_t i = 0; i < NetworkInputArray::MaxElements; ++i)
  74. {
  75. ClientInputId& cid = inArray[i].ModifyClientInputId();
  76. cid = ClientInputId(i * 2);
  77. HostFrameId& hid = inArray[i].ModifyHostFrameId();
  78. hid = HostFrameId(i * 2);
  79. AZ::TimeMs& time = inArray[i].ModifyHostTimeMs();
  80. time = AZ::TimeMs(i * 2 * TIME_SCALE);
  81. EXPECT_EQ(inArray[i].GetClientInputId(), cid);
  82. EXPECT_EQ(inArray[i].GetHostFrameId(), hid);
  83. EXPECT_EQ(inArray[i].GetHostTimeMs(), time);
  84. EXPECT_EQ(inArray[i].GetComponentInputDeltaLogs().size(), 0);
  85. }
  86. }
  87. TEST_F(NetworkInputTests, NetworkInputArraySerialization)
  88. {
  89. const NetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityTracker.get());
  90. NetworkInputArray inArray = NetworkInputArray(handle);
  91. for (uint32_t i = 0; i < NetworkInputArray::MaxElements; ++i)
  92. {
  93. inArray[i].SetClientInputId(ClientInputId(i));
  94. inArray[i].SetHostFrameId(HostFrameId(i));
  95. inArray[i].SetHostBlendFactor(i * BLEND_FACTOR_SCALE);
  96. inArray[i].SetHostTimeMs(AZ::TimeMs(i * TIME_SCALE));
  97. }
  98. AZStd::array<uint8_t, 1024> buffer;
  99. AzNetworking::NetworkInputSerializer inSerializer(buffer.data(), static_cast<uint32_t>(buffer.size()));
  100. // Always serialize the full first element
  101. EXPECT_TRUE(inArray.Serialize(inSerializer));
  102. NetworkInputArray outArray;
  103. AzNetworking::NetworkOutputSerializer outSerializer(buffer.data(), static_cast<uint32_t>(buffer.size()));
  104. EXPECT_TRUE(outArray.Serialize(outSerializer));
  105. for (uint32_t i = 0; i > NetworkInputArray::MaxElements; ++i)
  106. {
  107. EXPECT_EQ(inArray[i].GetClientInputId(), outArray[i].GetClientInputId());
  108. EXPECT_EQ(inArray[i].GetHostFrameId(), outArray[i].GetHostFrameId());
  109. EXPECT_NEAR(inArray[i].GetHostBlendFactor(), outArray[i].GetHostBlendFactor(), 0.001f);
  110. EXPECT_EQ(inArray[i].GetHostTimeMs(), outArray[i].GetHostTimeMs());
  111. }
  112. }
  113. TEST_F(NetworkInputTests, NetworkInputHistory)
  114. {
  115. const NetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityTracker.get());
  116. NetworkInputArray inArray = NetworkInputArray(handle);
  117. NetworkInputHistory inHistory = NetworkInputHistory();
  118. for (uint32_t i = 0; i < NetworkInputArray::MaxElements; ++i)
  119. {
  120. inArray[i].SetClientInputId(ClientInputId(i));
  121. inArray[i].SetHostFrameId(HostFrameId(i));
  122. inArray[i].SetHostBlendFactor(i * BLEND_FACTOR_SCALE);
  123. inArray[i].SetHostTimeMs(AZ::TimeMs(i * TIME_SCALE));
  124. inHistory.PushBack(inArray[i]);
  125. EXPECT_EQ(inArray[i].GetClientInputId(), inHistory[i].GetClientInputId());
  126. }
  127. EXPECT_EQ(inHistory.Size(), NetworkInputArray::MaxElements);
  128. for (uint32_t i = 0; i < NetworkInputArray::MaxElements; ++i)
  129. {
  130. NetworkInput input = inHistory.Front();
  131. EXPECT_EQ(input.GetClientInputId(), ClientInputId(i));
  132. EXPECT_EQ(input.GetHostFrameId(), HostFrameId(i));
  133. EXPECT_NEAR(input.GetHostBlendFactor(), i * BLEND_FACTOR_SCALE, 0.001f);
  134. EXPECT_EQ(input.GetHostTimeMs(), AZ::TimeMs(i * TIME_SCALE));
  135. inHistory.PopFront();
  136. }
  137. EXPECT_EQ(inHistory.Size(), 0);
  138. }
  139. TEST_F(NetworkInputTests, ConstNetworkInputHistory)
  140. {
  141. const NetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityTracker.get());
  142. NetworkInputArray inArray = NetworkInputArray(handle);
  143. NetworkInputHistory inHistory = NetworkInputHistory();
  144. for (uint32_t i = 0; i < NetworkInputArray::MaxElements; ++i)
  145. {
  146. inArray[i].SetClientInputId(ClientInputId(i));
  147. inArray[i].SetHostFrameId(HostFrameId(i));
  148. inArray[i].SetHostBlendFactor(i * BLEND_FACTOR_SCALE);
  149. inArray[i].SetHostTimeMs(AZ::TimeMs(i * TIME_SCALE));
  150. inHistory.PushBack(inArray[i]);
  151. }
  152. const NetworkInputArray constInArray = NetworkInputArray(inArray);
  153. const NetworkInputHistory constInHistory = NetworkInputHistory(inHistory);
  154. for (uint32_t i = 0; i < NetworkInputArray::MaxElements; ++i)
  155. {
  156. const NetworkInput input = constInHistory[i];
  157. EXPECT_EQ(constInHistory[i].GetClientInputId(), constInArray[i].GetClientInputId());
  158. EXPECT_EQ(constInHistory[i].GetHostFrameId(), constInArray[i].GetHostFrameId());
  159. EXPECT_NEAR(constInHistory[i].GetHostBlendFactor(), constInArray[i].GetHostBlendFactor(), 0.001f);
  160. EXPECT_EQ(constInHistory[i].GetHostTimeMs(), constInArray[i].GetHostTimeMs());
  161. }
  162. }
  163. TEST_F(NetworkInputTests, NetworkInputMigrationVector)
  164. {
  165. const NetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityTracker.get());
  166. NetworkInputArray inArray = NetworkInputArray(handle);
  167. NetworkInputMigrationVector inVector = NetworkInputMigrationVector();
  168. for (uint32_t i = 0; i < NetworkInputArray::MaxElements; ++i)
  169. {
  170. inArray[i].SetClientInputId(ClientInputId(i));
  171. inArray[i].SetHostFrameId(HostFrameId(i));
  172. inArray[i].SetHostBlendFactor(i * BLEND_FACTOR_SCALE);
  173. inArray[i].SetHostTimeMs(AZ::TimeMs(i * TIME_SCALE));
  174. inVector.PushBack(inArray[i]);
  175. }
  176. EXPECT_EQ(inVector.GetSize(), NetworkInputArray::MaxElements);
  177. AZStd::array<uint8_t, 1024> buffer;
  178. AzNetworking::NetworkInputSerializer inSerializer(buffer.data(), static_cast<uint32_t>(buffer.size()));
  179. // Always serialize the full first element
  180. EXPECT_TRUE(inVector.Serialize(inSerializer));
  181. NetworkInputArray outArray;
  182. AzNetworking::NetworkOutputSerializer outSerializer(buffer.data(), static_cast<uint32_t>(buffer.size()));
  183. NetworkInputMigrationVector outVector = NetworkInputMigrationVector();
  184. EXPECT_TRUE(outVector.Serialize(outSerializer));
  185. for (uint32_t i = 0; i > NetworkInputArray::MaxElements; ++i)
  186. {
  187. EXPECT_EQ(inVector[i].GetClientInputId(), outVector[i].GetClientInputId());
  188. EXPECT_EQ(inVector[i].GetHostFrameId(), outVector[i].GetHostFrameId());
  189. EXPECT_NEAR(inVector[i].GetHostBlendFactor(), outVector[i].GetHostBlendFactor(), 0.001f);
  190. EXPECT_EQ(inVector[i].GetHostTimeMs(), outVector[i].GetHostTimeMs());
  191. }
  192. EXPECT_EQ(inVector.GetSize(), outVector.GetSize());
  193. }
  194. TEST_F(NetworkInputTests, NetworkInputEntityName)
  195. {
  196. const NetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityTracker.get());
  197. NetworkInputArray inArray = NetworkInputArray(handle);
  198. EXPECT_EQ("root", inArray[0].GetOwnerName());
  199. }
  200. TEST_F(NetworkInputTests, NetworkInputAssignConst)
  201. {
  202. const NetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityTracker.get());
  203. NetworkInputArray inArray = NetworkInputArray(handle);
  204. for (uint32_t i = 0; i < NetworkInputArray::MaxElements; ++i)
  205. {
  206. inArray[i].SetClientInputId(ClientInputId(i));
  207. inArray[i].SetHostFrameId(HostFrameId(i));
  208. inArray[i].SetHostBlendFactor(i * BLEND_FACTOR_SCALE);
  209. inArray[i].SetHostTimeMs(AZ::TimeMs(i * TIME_SCALE));
  210. }
  211. const NetworkInput constInput(inArray[0]);
  212. NetworkInput input(inArray[1]);
  213. input = constInput;
  214. EXPECT_EQ(input.GetClientInputId(), constInput.GetClientInputId());
  215. }
  216. } // namespace Multiplayer