2020-10-21 17:56:05 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2022-04-20 14:24:59 -04:00
|
|
|
#if WITH_LOW_LEVEL_TESTS
|
|
|
|
|
|
2022-04-15 07:21:27 -04:00
|
|
|
#include "TestHarness.h"
|
|
|
|
|
|
2020-10-21 17:56:05 -04:00
|
|
|
#include "CoreMinimal.h"
|
|
|
|
|
#include "UObject/ObjectHandle.h"
|
2022-05-06 04:29:17 -04:00
|
|
|
#include "Misc/ScopeLock.h"
|
2020-10-21 17:56:05 -04:00
|
|
|
|
2022-04-15 07:21:27 -04:00
|
|
|
|
|
|
|
|
class FObjectRefTrackingTestBase
|
2020-10-21 17:56:05 -04:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
uint32 GetNumResolves() const { return NumResolves; }
|
|
|
|
|
uint32 GetNumFailedResolves() const { return NumFailedResolves; }
|
|
|
|
|
uint32 GetNumReads() const { return NumReads; }
|
|
|
|
|
|
|
|
|
|
struct FSnapshotObjectRefMetrics
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
FSnapshotObjectRefMetrics(FObjectRefTrackingTestBase& InTest)
|
|
|
|
|
: Test(InTest)
|
|
|
|
|
, OriginalNumResolves(Test.GetNumResolves())
|
|
|
|
|
, OriginalNumFailedResolves(Test.GetNumFailedResolves())
|
|
|
|
|
, OriginalNumReads(Test.GetNumReads())
|
2021-02-16 19:22:58 -04:00
|
|
|
{
|
|
|
|
|
Test.ConditionalInstallCallbacks();
|
|
|
|
|
}
|
2020-10-21 17:56:05 -04:00
|
|
|
|
|
|
|
|
bool TestNumResolves(const TCHAR* What, uint32 ExpectedDelta)
|
|
|
|
|
{
|
|
|
|
|
#if UE_WITH_OBJECT_HANDLE_TRACKING
|
2022-04-07 10:34:55 -04:00
|
|
|
bool bValue = OriginalNumResolves + ExpectedDelta == Test.GetNumResolves();
|
|
|
|
|
TEST_TRUE(What, bValue);
|
|
|
|
|
return bValue;
|
2020-10-21 17:56:05 -04:00
|
|
|
#endif
|
2022-03-31 07:45:37 -04:00
|
|
|
return true;
|
2020-10-21 17:56:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TestNumFailedResolves(const TCHAR* What, uint32 ExpectedDelta)
|
|
|
|
|
{
|
|
|
|
|
#if UE_WITH_OBJECT_HANDLE_TRACKING
|
2022-04-07 10:34:55 -04:00
|
|
|
bool bValue = OriginalNumFailedResolves + ExpectedDelta == Test.GetNumFailedResolves();
|
|
|
|
|
TEST_TRUE(What, bValue);
|
|
|
|
|
return bValue;
|
2020-10-21 17:56:05 -04:00
|
|
|
#endif
|
2022-03-31 07:45:37 -04:00
|
|
|
return true;
|
|
|
|
|
|
2020-10-21 17:56:05 -04:00
|
|
|
}
|
|
|
|
|
|
2021-02-02 21:46:17 -04:00
|
|
|
bool TestNumReads(const TCHAR* What, uint32 ExpectedDelta, bool bAllowAdditionalReads = false)
|
2020-10-21 17:56:05 -04:00
|
|
|
{
|
|
|
|
|
#if UE_WITH_OBJECT_HANDLE_TRACKING
|
2022-04-07 10:34:55 -04:00
|
|
|
bool bValue = false;
|
2021-02-02 21:46:17 -04:00
|
|
|
if (bAllowAdditionalReads)
|
|
|
|
|
{
|
2022-04-07 10:34:55 -04:00
|
|
|
bValue = Test.GetNumReads() >= OriginalNumReads + ExpectedDelta;
|
|
|
|
|
TEST_TRUE(What, bValue);
|
2021-02-02 21:46:17 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-07 10:34:55 -04:00
|
|
|
bValue = OriginalNumReads + ExpectedDelta == Test.GetNumReads();
|
|
|
|
|
TEST_TRUE(What, bValue);
|
2021-02-02 21:46:17 -04:00
|
|
|
}
|
2022-04-07 10:34:55 -04:00
|
|
|
return bValue;
|
2020-10-21 17:56:05 -04:00
|
|
|
#endif
|
2022-03-31 07:45:37 -04:00
|
|
|
return true;
|
|
|
|
|
|
2020-10-21 17:56:05 -04:00
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
FObjectRefTrackingTestBase& Test;
|
|
|
|
|
uint32 OriginalNumResolves;
|
|
|
|
|
uint32 OriginalNumFailedResolves;
|
|
|
|
|
uint32 OriginalNumReads;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
#if UE_WITH_OBJECT_HANDLE_TRACKING
|
|
|
|
|
static void OnRefResolved(const FObjectRef& ObjectRef, UPackage* Pkg, UObject* Obj)
|
|
|
|
|
{
|
|
|
|
|
NumResolves++;
|
|
|
|
|
if (!IsObjectRefNull(ObjectRef) && !Obj)
|
|
|
|
|
{
|
|
|
|
|
NumFailedResolves++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (PrevResolvedFunc)
|
|
|
|
|
{
|
|
|
|
|
PrevResolvedFunc(ObjectRef, Pkg, Obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
static void OnRefRead(UObject* Obj)
|
|
|
|
|
{
|
|
|
|
|
NumReads++;
|
|
|
|
|
if (PrevReadFunc)
|
|
|
|
|
{
|
|
|
|
|
PrevReadFunc(Obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static void ConditionalInstallCallbacks()
|
|
|
|
|
{
|
|
|
|
|
static bool bCallbacksInstalled = false;
|
|
|
|
|
static FCriticalSection CallbackInstallationLock;
|
|
|
|
|
|
|
|
|
|
if (bCallbacksInstalled)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FScopeLock ScopeLock(&CallbackInstallationLock);
|
|
|
|
|
if (!bCallbacksInstalled)
|
|
|
|
|
{
|
|
|
|
|
#if UE_WITH_OBJECT_HANDLE_TRACKING
|
|
|
|
|
PrevResolvedFunc = SetObjectHandleReferenceResolvedCallback(OnRefResolved);
|
|
|
|
|
PrevReadFunc = SetObjectHandleReadCallback(OnRefRead);
|
|
|
|
|
#endif
|
|
|
|
|
bCallbacksInstalled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UE_WITH_OBJECT_HANDLE_TRACKING
|
|
|
|
|
static ObjectHandleReferenceResolvedFunction* PrevResolvedFunc;
|
|
|
|
|
static ObjectHandleReadFunction* PrevReadFunc;
|
|
|
|
|
#endif
|
|
|
|
|
static thread_local uint32 NumResolves;
|
|
|
|
|
static thread_local uint32 NumFailedResolves;
|
|
|
|
|
static thread_local uint32 NumReads;
|
|
|
|
|
};
|
2022-03-31 07:45:37 -04:00
|
|
|
|
2022-04-20 14:24:59 -04:00
|
|
|
#endif
|