Author: clebert.suconic(a)jboss.com
Date: 2010-05-18 17:16:38 -0400 (Tue, 18 May 2010)
New Revision: 9246
Modified:
trunk/tests/src/org/hornetq/tests/unit/core/asyncio/AsynchronousFileTest.java
trunk/tests/src/org/hornetq/tests/util/UnitTestCase.java
Log:
Tweak on test to avoid one failure
Modified: trunk/tests/src/org/hornetq/tests/unit/core/asyncio/AsynchronousFileTest.java
===================================================================
---
trunk/tests/src/org/hornetq/tests/unit/core/asyncio/AsynchronousFileTest.java 2010-05-18
17:09:09 UTC (rev 9245)
+++
trunk/tests/src/org/hornetq/tests/unit/core/asyncio/AsynchronousFileTest.java 2010-05-18
21:16:38 UTC (rev 9246)
@@ -105,48 +105,55 @@
public void testReleaseBuffers() throws Exception
{
AsynchronousFileImpl controller = new AsynchronousFileImpl(executor,
pollerExecutor);
- controller.open(FILE_NAME, 10000);
- WeakReference<ByteBuffer> bufferCheck = new
WeakReference<ByteBuffer>(controller.getHandler());
- controller.fill(0, 10, 1024, (byte)0);
-
- ByteBuffer write = AsynchronousFileImpl.newBuffer(1024);
-
- for (int i = 0; i < 1024; i++)
+ WeakReference<ByteBuffer> bufferCheck = null;
+ try
{
- write.put(UnitTestCase.getSamplebyte(i));
- }
-
- final CountDownLatch latch = new CountDownLatch(1);
-
- controller.write(0, 1024, write, new AIOCallback()
- {
-
- public void onError(final int errorCode, final String errorMessage)
+ controller.open(FILE_NAME, 10000);
+ bufferCheck = new WeakReference<ByteBuffer>(controller.getHandler());
+ controller.fill(0, 10, 1024, (byte)0);
+
+ ByteBuffer write = AsynchronousFileImpl.newBuffer(1024);
+
+ for (int i = 0; i < 1024; i++)
{
+ write.put(UnitTestCase.getSamplebyte(i));
}
-
- public void done()
+
+ final CountDownLatch latch = new CountDownLatch(1);
+
+ controller.write(0, 1024, write, new AIOCallback()
{
- latch.countDown();
- }
- });
+
+ public void onError(final int errorCode, final String errorMessage)
+ {
+ }
+
+ public void done()
+ {
+ latch.countDown();
+ }
+ });
+
+ Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
+
+ WeakReference<ByteBuffer> bufferCheck2 = new
WeakReference<ByteBuffer>(write);
+
+ AsynchronousFileImpl.destroyBuffer(write);
+
+ write = null;
+
+ UnitTestCase.forceGC(bufferCheck2, 5000);
+
+ Assert.assertNull(bufferCheck2.get());
+ }
+ finally
+ {
+ controller.close();
+ }
- Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
-
- WeakReference<ByteBuffer> bufferCheck2 = new
WeakReference<ByteBuffer>(write);
-
- AsynchronousFileImpl.destroyBuffer(write);
-
- write = null;
-
- UnitTestCase.forceGC();
-
- Assert.assertNull(bufferCheck2.get());
-
- controller.close();
controller = null;
- UnitTestCase.forceGC();
+ UnitTestCase.forceGC(bufferCheck, 5000);
Assert.assertNull(bufferCheck.get());
}
Modified: trunk/tests/src/org/hornetq/tests/util/UnitTestCase.java
===================================================================
--- trunk/tests/src/org/hornetq/tests/util/UnitTestCase.java 2010-05-18 17:09:09 UTC (rev
9245)
+++ trunk/tests/src/org/hornetq/tests/util/UnitTestCase.java 2010-05-18 21:16:38 UTC (rev
9246)
@@ -140,6 +140,30 @@
}
}
+ public static void forceGC(WeakReference<?> ref, long timeout)
+ {
+ long waitUntil = System.currentTimeMillis() + timeout;
+ // A loop that will wait GC, using the minimal time as possible
+ while (ref.get() != null && System.currentTimeMillis() < waitUntil)
+ {
+ ArrayList<String> list = new ArrayList<String>();
+ for (int i = 0 ; i < 1000; i++)
+ {
+ list.add("Some string with garbage with concatenation " + i);
+ }
+ list.clear();
+ list = null;
+ System.gc();
+ try
+ {
+ Thread.sleep(500);
+ }
+ catch (InterruptedException e)
+ {
+ }
+ }
+ }
+
// verify if these weak references are released after a few GCs
public static void checkWeakReferences(final WeakReference<?>... references)
{
Show replies by date