Quelle TestAvailableMemoryWatcherLinux.cpp
Sprache: C
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* 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/. */
// Dummy tab unloader whose one job is to dispatch a low memory event. class MockTabUnloader final : public nsITabUnloader {
NS_DECL_THREADSAFE_ISUPPORTS public:
MockTabUnloader() = default;
NS_IMETHOD UnloadTabAsync() override { // We want to issue a memory pressure event for
NS_NotifyOfEventualMemoryPressure(MemoryPressureState::LowMemory); return NS_OK;
}
// Class that gradually increases the percent memory threshold // until it reaches 100%, which should guarantee a memory pressure // notification. class AvailableMemoryChecker final : public nsITimerCallback, public nsINamed { public:
NS_DECL_ISUPPORTS
NS_DECL_NSITIMERCALLBACK
NS_DECL_NSINAMED
// Class that listens for a given notification, then records // if it was received. class Spinner final : public nsIObserver {
nsCOMPtr<nsIObserverService> mObserverSvc;
nsDependentCString mTopic; bool mTopicObserved;
// Force the loop to move in case there is no event in the queue.
nsCOMPtr<nsIRunnable> dummyEvent = new Runnable(__func__);
NS_DispatchToMainThread(dummyEvent);
} return NS_OK;
} void StartListening() {
mObserverSvc->AddObserver(this, mTopic.get(), false);
} bool TopicObserved() { return mTopicObserved; } bool WaitForNotification();
};
NS_IMPL_ISUPPORTS(Spinner, nsIObserver);
// This timer should time us out if we never observe our notification. // Set to 5000 since the memory checker should finish incrementing the // pref by then, and if it hasn't then it is probably stuck somehow.
NS_NewTimerWithFuncCallback(
getter_AddRefs(timer),
[](nsITimer*, void* isTimeout) {
*reinterpret_cast<bool*>(isTimeout) = true;
},
&isTimeout, 5000, nsITimer::TYPE_ONE_SHOT, __func__);
// Start polling for low memory.
StartUserInteraction(observerSvc);
RefPtr<AvailableMemoryChecker> checker = new AvailableMemoryChecker();
checker->Init();
aSpinner->WaitForNotification();
// The checker should have dispatched a low memory event before reaching 100% // memory pressure threshold, so the topic should be observed by the spinner.
EXPECT_TRUE(aSpinner->TopicObserved());
checker->Shutdown();
}
TEST(AvailableMemoryWatcher, MemoryLowToHigh)
{ // Setting this pref to 100 ensures we start in a low memory scenario.
Preferences::SetUint("browser.low_commit_space_threshold_percent", 100);
// Start polling for low memory. We should start with low memory when we start // the checker.
RefPtr<AvailableMemoryChecker> checker = new AvailableMemoryChecker();
checker->Init();
lowMemorySpinner->WaitForNotification();
EXPECT_TRUE(lowMemorySpinner->TopicObserved());
RefPtr<Spinner> highMemorySpinner = new Spinner(observerSvc, "memory-pressure-stop");
highMemorySpinner->StartListening();
// Now that we are definitely low on memory, let's reset the pref to 0 to // exit low memory.
Preferences::SetUint("browser.low_commit_space_threshold_percent", 0);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.