Bug 1458020 - 1b. Add forceGarbageCollection; r=me

Add GeckoSessionTestRule.forceGarbageCollection() for improving the
stability of resource-intensive tests, by garbage-collecting before
running the test.

MozReview-Commit-ID: A4ITTPsPNNy
This commit is contained in:
Jim Chen 2018-05-18 10:16:04 -04:00
parent 4dd13f4142
commit 0a61f5d916
4 changed files with 57 additions and 1 deletions

View File

@ -1537,4 +1537,11 @@ class GeckoSessionTestRuleTest : BaseSessionTest(noErrorCollector = true) {
}
})
}
@WithDevToolsAPI
@Test fun forceGarbageCollection() {
sessionRule.forceGarbageCollection()
sessionRule.session.reload()
sessionRule.session.waitForPageStop()
}
}

View File

@ -0,0 +1,23 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* 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/. */
package org.mozilla.geckoview.test.rdp;
/**
* Provide access to the memory API.
*/
public final class Memory extends Actor {
/* package */ Memory(final RDPConnection connection, final String name) {
super(connection, name);
}
public void forceCycleCollection() {
sendPacket("{\"type\":\"forceCycleCollection\"}", JSON_PARSER).get();
}
public void forceGarbageCollection() {
sendPacket("{\"type\":\"forceGarbageCollection\"}", JSON_PARSER).get();
}
}

View File

@ -75,4 +75,15 @@ public final class Tab extends Actor {
final Actor promises = connection.getActor(name);
return (promises != null) ? (Promises) promises : new Promises(connection, name);
}
/**
* Get the memory object for access to the memory API.
*
* @return Memory object.
*/
public Memory getMemory() {
final String name = mTab.optString("memoryActor", null);
final Actor memory = connection.getActor(name);
return (memory != null) ? (Memory) memory : new Memory(connection, name);
}
}

View File

@ -1837,13 +1837,16 @@ public class GeckoSessionTestRule extends UiThreadTestRule {
public Object evaluateChromeJS(final @NonNull String js) {
assertThat("Must enable RDP using @WithDevToolsAPI",
mWithDevTools, equalTo(true));
ensureChromeProcess();
return evaluateJS(mRDPChromeProcess, js);
}
private void ensureChromeProcess() {
if (mRDPChromeProcess == null) {
mRDPChromeProcess = sRDPConnection.getChromeProcess();
assertThat("Should have chrome process object",
mRDPChromeProcess, notNullValue());
}
return evaluateJS(mRDPChromeProcess, js);
}
private Object evaluateJS(final @NonNull Tab tab, final @NonNull String js) {
@ -1969,4 +1972,16 @@ public class GeckoSessionTestRule extends UiThreadTestRule {
mWithDevTools, equalTo(true));
mWaitScopeDelegates.setPrefs(prefs);
}
/**
* Force cycle/garbage collection in the content to clean up previous resources. RDP must
* be enabled first using the {@link WithDevToolsAPI} annotation.
*/
public void forceGarbageCollection() {
assertThat("Must enable RDP using @WithDevToolsAPI",
mWithDevTools, equalTo(true));
ensureChromeProcess();
mRDPChromeProcess.getMemory().forceCycleCollection();
mRDPChromeProcess.getMemory().forceGarbageCollection();
}
}