Clean up after failed MemoryFileTest.testPurge()

MemoryFileTest.testPurge() can fail if the process runs
out of file descriptors before the kernel starts purging ashmem areas.
Before, there was no finally block to close the open ashmem
file descriptions, which caused random other tests to fail.

I also changed the size of the memory areas to 10MB, which
should cause it to run out of memory before it runs out of file
descriptors. It now fails with this instead:

java.io.IOException
	at android.os.MemoryFile.native_pin(Native Method)
	at android.os.MemoryFile.allowPurging(MemoryFile.java:189)
	at android.os.MemoryFileTest.testPurge(MemoryFileTest.java:53)
	at java.lang.reflect.Method.invokeNative(Native Method)
	at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
	at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
	at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
	at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)

I'm not sure why, but it could be that the kernel is purging the just
allocated area instead of an old one. That seems like bug.

See http://b/issue?id=2203775

Change-Id: Ia7930a714378474dd4c2c6709da40c08e715ab6f
This commit is contained in:
Bjorn Bringert
2010-02-16 23:27:18 +00:00
parent cf06dd0a8a
commit 1017a134c1

View File

@@ -16,10 +16,8 @@
package android.os;
import android.os.MemoryFile;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import java.io.File;
@@ -44,25 +42,30 @@ public class MemoryFileTest extends AndroidTestCase {
/**
* Keep allocating new files till the system purges them.
*/
@MediumTest
@LargeTest
public void testPurge() throws Exception {
List<MemoryFile> files = new ArrayList<MemoryFile>();
while (true) {
MemoryFile newFile = new MemoryFile("MemoryFileTest", 1000000);
newFile.allowPurging(true);
newFile.writeBytes(testString, 0, 0, testString.length);
files.add(newFile);
for (MemoryFile file : files) {
try {
file.readBytes(testString, 0, 0, testString.length);
} catch (IOException e) {
// Expected
for (MemoryFile fileToClose : files) {
fileToClose.close();
try {
while (true) {
// This will fail if the process runs out of file descriptors before
// the kernel starts purging ashmem areas.
MemoryFile newFile = new MemoryFile("MemoryFileTest", 10000000);
newFile.allowPurging(true);
newFile.writeBytes(testString, 0, 0, testString.length);
files.add(newFile);
for (MemoryFile file : files) {
try {
file.readBytes(testString, 0, 0, testString.length);
} catch (IOException e) {
// Expected
return;
}
return;
}
}
} finally {
for (MemoryFile fileToClose : files) {
fileToClose.close();
}
}
}