From 40a355a42d4a9444dc753c04c6608dade2f06a23 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:13:27 +0200 Subject: Adding upstream version 125.0.1. Signed-off-by: Daniel Baumann --- xpcom/tests/gtest/TestAutoOwningEventTarget.cpp | 120 +++++++++ xpcom/tests/gtest/TestThreadUtils.cpp | 327 +++++------------------- xpcom/tests/gtest/moz.build | 7 +- 3 files changed, 188 insertions(+), 266 deletions(-) create mode 100644 xpcom/tests/gtest/TestAutoOwningEventTarget.cpp (limited to 'xpcom/tests/gtest') diff --git a/xpcom/tests/gtest/TestAutoOwningEventTarget.cpp b/xpcom/tests/gtest/TestAutoOwningEventTarget.cpp new file mode 100644 index 0000000000..2bd34ecfee --- /dev/null +++ b/xpcom/tests/gtest/TestAutoOwningEventTarget.cpp @@ -0,0 +1,120 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +// vim:cindent:ts=4:et:sw=4: +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "nsCOMPtr.h" +#include "nsISupportsImpl.h" +#include "nsNetCID.h" +#include "nsServiceManagerUtils.h" +#include "nsThreadUtils.h" +#include "gtest/gtest.h" +#include "mozilla/SyncRunnable.h" +#include "mozilla/TaskQueue.h" +#include "mozilla/gtest/MozAssertions.h" + +namespace TestAutoOwningEventTarget { + +using namespace mozilla; + +#ifdef MOZ_THREAD_SAFETY_OWNERSHIP_CHECKS_SUPPORTED + +namespace { + +static MozExternalRefCountType GetRefCount(nsISupports* aSupports) { + aSupports->AddRef(); + return aSupports->Release(); +} + +void CheckAutoOwningEventTarget( + nsISerialEventTarget* aSerialEventTarget, + const nsAutoOwningEventTarget& aAutoOwningEventTarget, + const bool aIsCurrent) { + ASSERT_TRUE(aSerialEventTarget); + + ASSERT_EQ(aAutoOwningEventTarget.IsCurrentThread(), aIsCurrent); + + { + const auto refCountBefore = GetRefCount(aSerialEventTarget); + + { + nsAutoOwningEventTarget copyConstructedEventTarget( + aAutoOwningEventTarget); + ASSERT_EQ(copyConstructedEventTarget.IsCurrentThread(), aIsCurrent); + } + + const auto refCountAfter = GetRefCount(aSerialEventTarget); + + ASSERT_GE(refCountAfter, refCountBefore); + ASSERT_EQ(refCountAfter - refCountBefore, 0u); + } + + { + const auto refCountBefore = GetRefCount(aSerialEventTarget); + + { + nsAutoOwningEventTarget copyAssignedEventTarget; + ASSERT_TRUE(copyAssignedEventTarget.IsCurrentThread()); + + copyAssignedEventTarget = aAutoOwningEventTarget; + ASSERT_EQ(copyAssignedEventTarget.IsCurrentThread(), aIsCurrent); + } + + const auto refCountAfter = GetRefCount(aSerialEventTarget); + + ASSERT_GE(refCountAfter, refCountBefore); + ASSERT_EQ(refCountAfter - refCountBefore, 0u); + } +} + +} // namespace + +TEST(TestAutoOwningEventTarget, Simple) +{ + { + nsAutoOwningEventTarget autoOwningEventTarget; + + ASSERT_NO_FATAL_FAILURE(CheckAutoOwningEventTarget( + GetCurrentSerialEventTarget(), autoOwningEventTarget, + /* aIsCurrent */ true)); + } +} + +TEST(TestAutoOwningEventTarget, TaskQueue) +{ + nsresult rv; + nsCOMPtr threadPool = + do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv); + ASSERT_NS_SUCCEEDED(rv); + + auto taskQueue = TaskQueue::Create(threadPool.forget(), "TestTaskQueue", + /* aSupportsTailDispatch */ false); + + nsAutoOwningEventTarget autoOwningEventTarget; + + // XXX This call can't be wrapped with ASSERT_NS_SUCCEEDED directly because + // base-toolchains builds fail with: error: duplicate label + // 'gtest_label_testnofatal_111' + rv = SyncRunnable::DispatchToThread( + taskQueue, + NS_NewRunnableFunction( + "TestRunnable", [taskQueue, &autoOwningEventTarget] { + { + ASSERT_NO_FATAL_FAILURE(CheckAutoOwningEventTarget( + taskQueue, autoOwningEventTarget, /* aIsCurrent */ false)); + } + + { + nsAutoOwningEventTarget autoOwningEventTarget; + + ASSERT_NO_FATAL_FAILURE(CheckAutoOwningEventTarget( + taskQueue, autoOwningEventTarget, /* aIsCurrent */ true)); + } + })); + ASSERT_NS_SUCCEEDED(rv); +} + +#endif + +} // namespace TestAutoOwningEventTarget diff --git a/xpcom/tests/gtest/TestThreadUtils.cpp b/xpcom/tests/gtest/TestThreadUtils.cpp index 2b9ff97192..96b1342885 100644 --- a/xpcom/tests/gtest/TestThreadUtils.cpp +++ b/xpcom/tests/gtest/TestThreadUtils.cpp @@ -885,122 +885,88 @@ TEST(ThreadUtils, IdleTaskRunner) TEST(ThreadUtils, TypeTraits) { - static_assert(!mozilla::IsRefcountedSmartPointer::value, - "IsRefcountedSmartPointer should be false"); - static_assert(mozilla::IsRefcountedSmartPointer>::value, - "IsRefcountedSmartPointer> should be true"); - static_assert(mozilla::IsRefcountedSmartPointer>::value, - "IsRefcountedSmartPointer> should be true"); - static_assert( - mozilla::IsRefcountedSmartPointer>::value, - "IsRefcountedSmartPointer> should be true"); - static_assert( - mozilla::IsRefcountedSmartPointer>::value, - "IsRefcountedSmartPointer> should be true"); - static_assert(mozilla::IsRefcountedSmartPointer>::value, - "IsRefcountedSmartPointer> should be true"); - static_assert(mozilla::IsRefcountedSmartPointer>::value, - "IsRefcountedSmartPointer> should be true"); - static_assert( - mozilla::IsRefcountedSmartPointer>::value, - "IsRefcountedSmartPointer> should be true"); - static_assert( - mozilla::IsRefcountedSmartPointer>::value, - "IsRefcountedSmartPointer> should be true"); - - static_assert(std::is_same_v::Type>, - "RemoveSmartPointer::Type should be int"); - static_assert(std::is_same_v::Type>, - "RemoveSmartPointer::Type should be int*"); - static_assert( - std::is_same_v, - mozilla::RemoveSmartPointer>::Type>, - "RemoveSmartPointer>::Type should be UniquePtr"); - static_assert( - std::is_same_v>::Type>, - "RemoveSmartPointer>::Type should be int"); - static_assert( - std::is_same_v>::Type>, - "RemoveSmartPointer>::Type should be int"); + static_assert(std::is_same_v>, + "RemoveSmartPointer should be int"); + static_assert(std::is_same_v>, + "RemoveSmartPointer should be int*"); + static_assert(std::is_same_v, + mozilla::RemoveSmartPointer>>, + "RemoveSmartPointer> should be UniquePtr"); + static_assert(std::is_same_v>>, + "RemoveSmartPointer> should be int"); + static_assert( + std::is_same_v>>, + "RemoveSmartPointer> should be int"); + static_assert( + std::is_same_v>>, + "RemoveSmartPointer> should be int"); static_assert( std::is_same_v>::Type>, - "RemoveSmartPointer>::Type should be int"); - static_assert( - std::is_same_v< - int, mozilla::RemoveSmartPointer>::Type>, - "RemoveSmartPointer>::Type should be int"); + mozilla::RemoveSmartPointer>>, + "RemoveSmartPointer> should be int"); + static_assert(std::is_same_v>>, + "RemoveSmartPointer> should be int"); static_assert( - std::is_same_v>::Type>, - "RemoveSmartPointer>::Type should be int"); + std::is_same_v>>, + "RemoveSmartPointer> should be int"); static_assert( - std::is_same_v>::Type>, - "RemoveSmartPointer>::Type should be int"); + std::is_same_v>>, + "RemoveSmartPointer> should be int"); static_assert( std::is_same_v>::Type>, - "RemoveSmartPointer>::Type should be int"); - static_assert( - std::is_same_v< - int, mozilla::RemoveSmartPointer>::Type>, - "RemoveSmartPointer>::Type should be int"); + mozilla::RemoveSmartPointer>>, + "RemoveSmartPointer> should be int"); - static_assert( - std::is_same_v::Type>, - "RemoveRawOrSmartPointer::Type should be int"); + static_assert(std::is_same_v>, + "RemoveRawOrSmartPointer should be int"); static_assert( std::is_same_v, - mozilla::RemoveRawOrSmartPointer>::Type>, - "RemoveRawOrSmartPointer>::Type should be UniquePtr"); - static_assert( - std::is_same_v::Type>, - "RemoveRawOrSmartPointer::Type should be int"); + mozilla::RemoveRawOrSmartPointer>>, + "RemoveRawOrSmartPointer> should be UniquePtr"); + static_assert(std::is_same_v>, + "RemoveRawOrSmartPointer should be int"); static_assert( - std::is_same_v::Type>, - "RemoveRawOrSmartPointer::Type should be const int"); + std::is_same_v>, + "RemoveRawOrSmartPointer should be const int"); static_assert( std::is_same_v::Type>, - "RemoveRawOrSmartPointer::Type should be volatile int"); + mozilla::RemoveRawOrSmartPointer>, + "RemoveRawOrSmartPointer should be volatile int"); static_assert( - std::is_same_v::Type>, - "RemoveRawOrSmartPointer::Type should be const " + std::is_same_v>, + "RemoveRawOrSmartPointer should be const " "volatile int"); static_assert( - std::is_same_v>::Type>, - "RemoveRawOrSmartPointer>::Type should be int"); + std::is_same_v>>, + "RemoveRawOrSmartPointer> should be int"); + static_assert( + std::is_same_v>>, + "RemoveRawOrSmartPointer> should be int"); static_assert( std::is_same_v>::Type>, - "RemoveRawOrSmartPointer>::Type should be int"); + mozilla::RemoveRawOrSmartPointer>>, + "RemoveRawOrSmartPointer> should be int"); static_assert( std::is_same_v< - int, mozilla::RemoveRawOrSmartPointer>::Type>, - "RemoveRawOrSmartPointer>::Type should be int"); - static_assert( - std::is_same_v>::Type>, - "RemoveRawOrSmartPointer>::Type should be " + int, mozilla::RemoveRawOrSmartPointer>>, + "RemoveRawOrSmartPointer> should be " "int"); + static_assert( + std::is_same_v>>, + "RemoveRawOrSmartPointer> should be int"); static_assert( std::is_same_v>::Type>, - "RemoveRawOrSmartPointer>::Type should be int"); + mozilla::RemoveRawOrSmartPointer>>, + "RemoveRawOrSmartPointer> should be int"); static_assert( - std::is_same_v< - int, mozilla::RemoveRawOrSmartPointer>::Type>, - "RemoveRawOrSmartPointer>::Type should be int"); + std::is_same_v>>, + "RemoveRawOrSmartPointer> should be int"); static_assert( std::is_same_v< - int, mozilla::RemoveRawOrSmartPointer>::Type>, - "RemoveRawOrSmartPointer>::Type should be int"); - static_assert( - std::is_same_v>::Type>, - "RemoveRawOrSmartPointer>::Type should be " + int, mozilla::RemoveRawOrSmartPointer>>, + "RemoveRawOrSmartPointer> should be " "int"); } @@ -1288,15 +1254,9 @@ TEST(ThreadUtils, main) static_assert(!IsParameterStorageClass::value, "'int' should not be recognized as Storage Class"); - static_assert( - IsParameterStorageClass>::value, - "StoreCopyPassByValue should be recognized as Storage Class"); static_assert( IsParameterStorageClass>::value, "StoreCopyPassByConstLRef should be recognized as Storage Class"); - static_assert( - IsParameterStorageClass>::value, - "StoreCopyPassByLRef should be recognized as Storage Class"); static_assert( IsParameterStorageClass>::value, "StoreCopyPassByRRef should be recognized as Storage Class"); @@ -1315,12 +1275,6 @@ TEST(ThreadUtils, main) static_assert( IsParameterStorageClass>::value, "StoreConstPtrPassByConstPtr should be recognized as Storage Class"); - static_assert( - IsParameterStorageClass>::value, - "StoreCopyPassByConstPtr should be recognized as Storage Class"); - static_assert( - IsParameterStorageClass>::value, - "StoreCopyPassByPtr should be recognized as Storage Class"); RefPtr rpt(new ThreadUtilsObject); int count = 0; @@ -1359,11 +1313,6 @@ TEST(ThreadUtils, main) StoreCopyPassByConstLRef>, "detail::ParameterStorage::Type should be " "StoreCopyPassByConstLRef"); - static_assert(std::is_same_v< - ::detail::ParameterStorage>::Type, - StoreCopyPassByValue>, - "detail::ParameterStorage>::Type " - "should be StoreCopyPassByValue"); r1 = NewRunnableMethod("TestThreadUtils::ThreadUtilsObject::Test1i", rpt, &ThreadUtilsObject::Test1i, 12); @@ -1480,37 +1429,6 @@ TEST(ThreadUtils, main) EXPECT_EQ(i, rpt->mA0); } - // Raw pointer to copy. - static_assert(std::is_same_v::stored_type, int>, - "StoreCopyPassByPtr::stored_type should be int"); - static_assert(std::is_same_v::passed_type, int*>, - "StoreCopyPassByPtr::passed_type should be int*"); - { - int i = 1202; - r1 = NewRunnableMethod>( - "TestThreadUtils::ThreadUtilsObject::Test1pi", rpt, - &ThreadUtilsObject::Test1pi, i); - r1->Run(); - EXPECT_EQ(count += 2, rpt->mCount); - EXPECT_EQ(i, rpt->mA0); - } - - // Raw pointer to const copy. - static_assert(std::is_same_v::stored_type, int>, - "StoreCopyPassByConstPtr::stored_type should be int"); - static_assert( - std::is_same_v::passed_type, const int*>, - "StoreCopyPassByConstPtr::passed_type should be const int*"); - { - int i = 1203; - r1 = NewRunnableMethod>( - "TestThreadUtils::ThreadUtilsObject::Test1pci", rpt, - &ThreadUtilsObject::Test1pci, i); - r1->Run(); - EXPECT_EQ(count += 2, rpt->mCount); - EXPECT_EQ(i, rpt->mA0); - } - // nsRefPtr to pointer. static_assert( std::is_same_v<::detail::ParameterStorage< @@ -1536,7 +1454,7 @@ TEST(ThreadUtils, main) // (more nsRefPtr tests below) // nsRefPtr for ref-countable classes that do not derive from ISupports. - static_assert(::detail::HasRefCountMethods::value, + static_assert(::detail::HasRefCountMethods, "ThreadUtilsRefCountedFinal has AddRef() and Release()"); static_assert( std::is_same_v< @@ -1544,7 +1462,7 @@ TEST(ThreadUtils, main) StoreRefPtrPassByPtr>, "ParameterStorage::Type should be " "StoreRefPtrPassByPtr"); - static_assert(::detail::HasRefCountMethods::value, + static_assert(::detail::HasRefCountMethods, "ThreadUtilsRefCountedBase has AddRef() and Release()"); static_assert( std::is_same_v< @@ -1552,9 +1470,8 @@ TEST(ThreadUtils, main) StoreRefPtrPassByPtr>, "ParameterStorage::Type should be " "StoreRefPtrPassByPtr"); - static_assert( - ::detail::HasRefCountMethods::value, - "ThreadUtilsRefCountedDerived has AddRef() and Release()"); + static_assert(::detail::HasRefCountMethods, + "ThreadUtilsRefCountedDerived has AddRef() and Release()"); static_assert( std::is_same_v< ::detail::ParameterStorage::Type, @@ -1562,7 +1479,7 @@ TEST(ThreadUtils, main) "ParameterStorage::Type should be " "StoreRefPtrPassByPtr"); - static_assert(!::detail::HasRefCountMethods::value, + static_assert(!::detail::HasRefCountMethods, "ThreadUtilsNonRefCounted doesn't have AddRef() and Release()"); static_assert(!std::is_same_v< ::detail::ParameterStorage::Type, @@ -1736,126 +1653,6 @@ TEST(ThreadUtils, main) // Verify copy/move assumptions. - Spy::ClearAll(); - if (gDebug) { - printf("%d - Test: Store copy from lvalue, pass by value\n", __LINE__); - } - { // Block around nsCOMPtr lifetime. - nsCOMPtr r2; - { // Block around Spy lifetime. - if (gDebug) { - printf("%d - Spy s(10)\n", __LINE__); - } - Spy s(10); - EXPECT_EQ(1, gConstructions); - EXPECT_EQ(1, gAlive); - if (gDebug) { - printf( - "%d - r2 = " - "NewRunnableMethod>(&TestByValue, s)\n", - __LINE__); - } - r2 = NewRunnableMethod>( - "TestThreadUtils::ThreadUtilsObject::TestByValue", rpt, - &ThreadUtilsObject::TestByValue, s); - EXPECT_EQ(2, gAlive); - EXPECT_LE(1, gCopyConstructions); // At least 1 copy-construction. - Spy::ClearActions(); - if (gDebug) { - printf("%d - End block with Spy s(10)\n", __LINE__); - } - } - EXPECT_EQ(1, gDestructions); - EXPECT_EQ(1, gAlive); - Spy::ClearActions(); - if (gDebug) { - printf("%d - Run()\n", __LINE__); - } - r2->Run(); - EXPECT_LE(1, gCopyConstructions); // Another copy-construction in call. - EXPECT_EQ(10, rpt->mSpy.mID); - EXPECT_LE(1, gDestructions); - EXPECT_EQ(1, gAlive); - Spy::ClearActions(); - if (gDebug) { - printf("%d - End block with r\n", __LINE__); - } - } - if (gDebug) { - printf("%d - After end block with r\n", __LINE__); - } - EXPECT_EQ(1, gDestructions); - EXPECT_EQ(0, gAlive); - - Spy::ClearAll(); - if (gDebug) { - printf("%d - Test: Store copy from prvalue, pass by value\n", __LINE__); - } - { - if (gDebug) { - printf( - "%d - r3 = " - "NewRunnableMethod>(&TestByValue, " - "Spy(11))\n", - __LINE__); - } - nsCOMPtr r3 = NewRunnableMethod>( - "TestThreadUtils::ThreadUtilsObject::TestByValue", rpt, - &ThreadUtilsObject::TestByValue, Spy(11)); - EXPECT_EQ(1, gAlive); - EXPECT_EQ(1, gConstructions); - EXPECT_LE(1, gMoveConstructions); - Spy::ClearActions(); - if (gDebug) { - printf("%d - Run()\n", __LINE__); - } - r3->Run(); - EXPECT_LE(1, gCopyConstructions); // Another copy-construction in call. - EXPECT_EQ(11, rpt->mSpy.mID); - EXPECT_LE(1, gDestructions); - EXPECT_EQ(1, gAlive); - Spy::ClearActions(); - if (gDebug) { - printf("%d - End block with r\n", __LINE__); - } - } - if (gDebug) { - printf("%d - After end block with r\n", __LINE__); - } - EXPECT_EQ(1, gDestructions); - EXPECT_EQ(0, gAlive); - - Spy::ClearAll(); - { // Store copy from xvalue, pass by value. - nsCOMPtr r4; - { - Spy s(12); - EXPECT_EQ(1, gConstructions); - EXPECT_EQ(1, gAlive); - Spy::ClearActions(); - r4 = NewRunnableMethod>( - "TestThreadUtils::ThreadUtilsObject::TestByValue", rpt, - &ThreadUtilsObject::TestByValue, std::move(s)); - EXPECT_LE(1, gMoveConstructions); - EXPECT_EQ(1, gAlive); - EXPECT_EQ(1, gZombies); - Spy::ClearActions(); - } - EXPECT_EQ(1, gDestructions); - EXPECT_EQ(1, gAlive); - EXPECT_EQ(0, gZombies); - Spy::ClearActions(); - r4->Run(); - EXPECT_LE(1, gCopyConstructions); // Another copy-construction in call. - EXPECT_EQ(12, rpt->mSpy.mID); - EXPECT_LE(1, gDestructions); - EXPECT_EQ(1, gAlive); - Spy::ClearActions(); - } - EXPECT_EQ(1, gDestructions); - EXPECT_EQ(0, gAlive); - // Won't test xvalues anymore, prvalues are enough to verify all rvalues. - Spy::ClearAll(); if (gDebug) { printf("%d - Test: Store copy from lvalue, pass by const lvalue ref\n", diff --git a/xpcom/tests/gtest/moz.build b/xpcom/tests/gtest/moz.build index 57ec43a371..6fd509d7b2 100644 --- a/xpcom/tests/gtest/moz.build +++ b/xpcom/tests/gtest/moz.build @@ -9,6 +9,7 @@ UNIFIED_SOURCES += [ "TestArenaAllocator.cpp", "TestArrayAlgorithm.cpp", "TestAtoms.cpp", + "TestAutoOwningEventTarget.cpp", "TestAutoRefCnt.cpp", "TestBase64.cpp", "TestCallTemplates.cpp", @@ -120,10 +121,14 @@ else: if CONFIG["OS_TARGET"] == "Darwin": UNIFIED_SOURCES += [ "TestAvailableMemoryWatcherMac.cpp", - "TestMacNSURLEscaping.mm", "TestThreads_mac.mm", ] +if CONFIG["TARGET_OS"] == "OSX": + UNIFIED_SOURCES += [ + "TestMacNSURLEscaping.mm", + ] + if CONFIG["OS_TARGET"] == "Linux": UNIFIED_SOURCES += [ "TestAvailableMemoryWatcherLinux.cpp", -- cgit v1.2.3