From 1017a134c19b3d296c7c05cf640db4b08816d090 Mon Sep 17 00:00:00 2001 From: Bjorn Bringert Date: Tue, 16 Feb 2010 23:27:18 +0000 Subject: [PATCH] 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 --- .../src/android/os/MemoryFileTest.java | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/core/tests/coretests/src/android/os/MemoryFileTest.java b/core/tests/coretests/src/android/os/MemoryFileTest.java index 411bdaa309f87..1f7ee266d94a0 100644 --- a/core/tests/coretests/src/android/os/MemoryFileTest.java +++ b/core/tests/coretests/src/android/os/MemoryFileTest.java @@ -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 files = new ArrayList(); - 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(); + } } }