Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
@@ -0,0 +1,192 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/TcpTransport/TcpNetworkInterface.h>
#include <AzNetworking/Framework/NetworkingSystemComponent.h>
#include <AzNetworking/AutoGen/CorePackets.AutoPackets.h>
#include <AzCore/Interface/Interface.h>
#include <AzCore/Console/LoggerSystemComponent.h>
#include <AzCore/Time/TimeSystemComponent.h>
#include <AzCore/Name/NameDictionary.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
using namespace AzNetworking;
class TestTcpConnectionListener
: public IConnectionListener
{
public:
ConnectResult ValidateConnect([[maybe_unused]] const IpAddress& remoteAddress, [[maybe_unused]] const IPacketHeader& packetHeader, [[maybe_unused]] ISerializer& serializer)
{
return ConnectResult::Accepted;
}
void OnConnect([[maybe_unused]] IConnection* connection)
{
;
}
bool OnPacketReceived([[maybe_unused]] IConnection* connection, const IPacketHeader& packetHeader, [[maybe_unused]] ISerializer& serializer)
{
EXPECT_TRUE((packetHeader.GetPacketType() == static_cast<PacketType>(CorePackets::PacketType::InitiateConnectionPacket))
|| (packetHeader.GetPacketType() == static_cast<PacketType>(CorePackets::PacketType::HeartbeatPacket)));
return false;
}
void OnPacketLost([[maybe_unused]] IConnection* connection, [[maybe_unused]] PacketId packetId)
{
}
void OnDisconnect([[maybe_unused]] IConnection* connection, [[maybe_unused]] DisconnectReason reason, [[maybe_unused]] TerminationEndpoint endpoint)
{
}
};
class TestTcpClient
{
public:
TestTcpClient()
{
AZStd::string name = AZStd::string::format("TcpClient%d", ++s_numClients);
m_name = name;
m_clientNetworkInterface = AZ::Interface<INetworking>::Get()->CreateNetworkInterface(m_name, ProtocolType::Tcp, TrustZone::ExternalClientToServer, m_connectionListener);
m_clientNetworkInterface->Connect(IpAddress(127, 0, 0, 1, 12345));
}
~TestTcpClient()
{
AZ::Interface<INetworking>::Get()->DestroyNetworkInterface(m_name);
}
AZ::Name m_name;
TestTcpConnectionListener m_connectionListener;
INetworkInterface* m_clientNetworkInterface;
static inline int32_t s_numClients = 0;
};
class TestTcpServer
{
public:
TestTcpServer()
{
m_serverNetworkInterface = AZ::Interface<INetworking>::Get()->CreateNetworkInterface(m_name, ProtocolType::Tcp, TrustZone::ExternalClientToServer, m_connectionListener);
m_serverNetworkInterface->Listen(12345);
}
~TestTcpServer()
{
AZ::Interface<INetworking>::Get()->DestroyNetworkInterface(m_name);
}
AZ::Name m_name = AZ::Name(AZStd::string_view("TcpServer"));
TestTcpConnectionListener m_connectionListener;
INetworkInterface* m_serverNetworkInterface;
};
class TcpTransportTests
: public AllocatorsFixture
{
public:
void SetUp() override
{
SetupAllocator();
AZ::NameDictionary::Create();
m_loggerComponent = new AZ::LoggerSystemComponent;
m_timeComponent = new AZ::TimeSystemComponent;
m_networkingSystemComponent = new AzNetworking::NetworkingSystemComponent;
}
void TearDown() override
{
delete m_networkingSystemComponent;
delete m_timeComponent;
delete m_loggerComponent;
AZ::NameDictionary::Destroy();
TeardownAllocator();
}
AZ::LoggerSystemComponent* m_loggerComponent;
AZ::TimeSystemComponent* m_timeComponent;
AzNetworking::NetworkingSystemComponent* m_networkingSystemComponent;
};
#if AZ_TRAIT_DISABLE_FAILED_NETWORKING_TESTS
TEST_F(TcpTransportTests, DISABLED_TestSingleClient)
#else
TEST_F(TcpTransportTests, TestSingleClient)
#endif // AZ_TRAIT_DISABLE_FAILED_NETWORKING_TESTS
{
TestTcpServer testServer;
TestTcpClient testClient;
constexpr AZ::TimeMs TotalIterationTimeMs = AZ::TimeMs{ 5000 };
const AZ::TimeMs startTimeMs = AZ::GetElapsedTimeMs();
for (;;)
{
AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(25));
m_networkingSystemComponent->OnTick(0.0f, AZ::ScriptTimePoint());
bool timeExpired = (AZ::GetElapsedTimeMs() - startTimeMs > TotalIterationTimeMs);
bool canTerminate = (testServer.m_serverNetworkInterface->GetConnectionSet().GetConnectionCount() == 1)
&& (testClient.m_clientNetworkInterface->GetConnectionSet().GetConnectionCount() == 1);
if (canTerminate || timeExpired)
{
break;
}
}
EXPECT_EQ(testServer.m_serverNetworkInterface->GetConnectionSet().GetConnectionCount(), 1);
EXPECT_EQ(testClient.m_clientNetworkInterface->GetConnectionSet().GetConnectionCount(), 1);
}
#if AZ_TRAIT_DISABLE_FAILED_NETWORKING_TESTS
TEST_F(TcpTransportTests, DISABLED_TestMultipleClients)
#else
TEST_F(TcpTransportTests, TestMultipleClients)
#endif // AZ_TRAIT_DISABLE_FAILED_NETWORKING_TESTS
{
constexpr uint32_t NumTestClients = 50;
TestTcpServer testServer;
TestTcpClient testClient[NumTestClients];
constexpr AZ::TimeMs TotalIterationTimeMs = AZ::TimeMs{ 5000 };
const AZ::TimeMs startTimeMs = AZ::GetElapsedTimeMs();
for (;;)
{
AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(25));
m_networkingSystemComponent->OnTick(0.0f, AZ::ScriptTimePoint());
bool timeExpired = (AZ::GetElapsedTimeMs() - startTimeMs > TotalIterationTimeMs);
bool canTerminate = testServer.m_serverNetworkInterface->GetConnectionSet().GetConnectionCount() == NumTestClients;
for (uint32_t i = 0; i < NumTestClients; ++i)
{
canTerminate &= testClient[i].m_clientNetworkInterface->GetConnectionSet().GetConnectionCount() == 1;
}
if (canTerminate || timeExpired)
{
break;
}
}
EXPECT_EQ(testServer.m_serverNetworkInterface->GetConnectionSet().GetConnectionCount(), NumTestClients);
for (uint32_t i = 0; i < NumTestClients; ++i)
{
EXPECT_EQ(testClient[i].m_clientNetworkInterface->GetConnectionSet().GetConnectionCount(), 1);
}
}
}