Files
UnrealEngineUWP/Engine/Source/Programs/WebTests/Private/TestWebSockets.cpp
lorry li 2186b4d4d5 Add tests for websockets in WebTests;
Move WebTests web server from Engine/Source/Programs/WebTests/WebServer to Engine/Source/Programs/WebTestsServer, so to make sure not include all files underneath into VS project.

[REVIEW] [at]michael.kirzinger [at]stephen.ma [at]chuck.zhu [at]michael.atchison [at]rafa.lecina
#jira UE-191978
#rb stephen.ma [at]rafa.lecina [at]michael.atchison

[CL 27189120 by lorry li in ue5-main branch]
2023-08-17 18:35:29 -04:00

113 lines
3.1 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "Containers/BackgroundableTicker.h"
#include "CoreMinimal.h"
#include "HAL/PlatformProcess.h"
#include "Misc/CommandLine.h"
#include "WebSocketsModule.h"
#include "IWebSocket.h"
#include "TestHarness.h"
/**
* WebSockets Tests
* -----------------------------------------------------------------------------------------------
*
* PURPOSE:
*
* Integration Tests to make sure all kinds of WebSockets client features in C++ work well on different platforms,
* including but not limited to error handing, retrying, threading, SSL and profiling.
*
* -----------------------------------------------------------------------------------------------
*/
#define WEBSOCKETS_TAG "[WebSockets]"
class FWebSocketsModuleTestFixture
{
public:
FWebSocketsModuleTestFixture()
: WebServerIp(TEXT("127.0.0.1"))
, WebServerWebSocketsPort(8000)
{
ParseSettingsFromCommandLine();
WebSocketsModule = new FWebSocketsModule();
IModuleInterface* Module = WebSocketsModule;
Module->StartupModule();
}
virtual ~FWebSocketsModuleTestFixture()
{
IModuleInterface* Module = WebSocketsModule;
Module->ShutdownModule();
delete Module;
}
void ParseSettingsFromCommandLine()
{
FParse::Value(FCommandLine::Get(), TEXT("web_server_ip"), WebServerIp);
}
const FString UrlWithInvalidPortToTestConnectTimeout() const { return FString::Format(TEXT("ws://{0}:{1}"), { *WebServerIp, 8765 }); }
const FString UrlBase() const { return FString::Format(TEXT("ws://{0}:{1}"), { *WebServerIp, WebServerWebSocketsPort }); }
const FString UrlWebSocketsTests() const { return FString::Format(TEXT("{0}/webtests/websocketstests"), { *UrlBase() }); }
FString WebServerIp;
uint32 WebServerWebSocketsPort;
FWebSocketsModule* WebSocketsModule;
};
class FRunUntilQuitRequestedFixture : public FWebSocketsModuleTestFixture
{
public:
FRunUntilQuitRequestedFixture()
{
}
~FRunUntilQuitRequestedFixture()
{
RunUntilQuitRequested();
}
void RunUntilQuitRequested()
{
while (!bQuitRequested)
{
FTSBackgroundableTicker::GetCoreTicker().Tick(TickFrequency);
FPlatformProcess::Sleep(TickFrequency);
}
}
float TickFrequency = 1.0f / 60; /*60 FPS*/;
bool bQuitRequested = false;
bool bSucceed = false;
};
TEST_CASE_METHOD(FRunUntilQuitRequestedFixture, "WebSockets can connect then send and receive message", WEBSOCKETS_TAG)
{
TSharedPtr<IWebSocket> WebSocket = WebSocketsModule->CreateWebSocket(FString::Format(TEXT("{0}/echo/"), { *UrlWebSocketsTests() }));
WebSocket->OnConnected().AddLambda([this, WebSocket](){
WebSocket->Send(TEXT("hi websockets tests"));
});
WebSocket->OnMessage().AddLambda([this, WebSocket](const FString& MessageString) {
CHECK(MessageString == TEXT("hi websockets tests"));
WebSocket->Close();
bSucceed = true;
});
WebSocket->OnClosed().AddLambda([this, WebSocket](int32 /* StatusCode */, const FString& /* Reason */, bool /* bWasClean */){
CHECK(bSucceed);
bQuitRequested = true;
});
WebSocket->OnConnectionError().AddLambda([this, WebSocket](const FString& /* Error */){
CHECK(false);
bQuitRequested = true;
});
WebSocket->Connect();
}