[jboss-svn-commits] JBoss Common SVN: r3933 - shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/exporter.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Jan 19 12:32:38 EST 2010
Author: ALRubinger
Date: 2010-01-19 12:32:38 -0500 (Tue, 19 Jan 2010)
New Revision: 3933
Modified:
shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExporterTestCase.java
Log:
[SHRINKWRAP-116] Add a (hopefully) failing test case to show OOM on export of large archives
Modified: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExporterTestCase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExporterTestCase.java 2010-01-15 11:08:04 UTC (rev 3932)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExporterTestCase.java 2010-01-19 17:32:38 UTC (rev 3933)
@@ -21,6 +21,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.util.Random;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@@ -28,11 +29,14 @@
import junit.framework.TestCase;
import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ArchivePath;
+import org.jboss.shrinkwrap.api.Archives;
import org.jboss.shrinkwrap.api.Asset;
-import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.exporter.ArchiveExportException;
import org.jboss.shrinkwrap.api.exporter.FileExistsException;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.impl.base.asset.ByteArrayAsset;
import org.jboss.shrinkwrap.impl.base.io.IOUtil;
import org.jboss.shrinkwrap.impl.base.path.BasicPath;
import org.jboss.shrinkwrap.impl.base.path.PathUtil;
@@ -88,6 +92,46 @@
}
/**
+ * Ensures that we can export archives of large sizes without
+ * leading to {@link OutOfMemoryError}
+ */
+ @Test
+ public void exportHugeArchive() throws IOException
+ {
+ // Log
+ log.info("exportHugeArchive");
+ log.info("This test may take awhile as it's intended to fill memory");
+
+ // Get an archive instance
+ JavaArchive archive = Archives.create("hugeArchive.jar", JavaArchive.class);
+
+ // Approximate the free memory to start
+ final Runtime runtime = Runtime.getRuntime();
+ final long startFreeMemBytes = totalFreeMemory(runtime);
+ long currentFreeMemBytes = startFreeMemBytes;
+ int counter = 0;
+ // Loop through and add a MB Asset
+ final String pathPrefix = "path";
+
+ // Fill up the archive until we'e got only 30% of memory left
+ while (currentFreeMemBytes > (startFreeMemBytes * .3))
+ {
+ archive.add(MegaByteAsset.newInstance(), pathPrefix + counter++);
+ System.gc(); // Signal to the VM to try to clean up a bit, not the most reliable, but makes this OK on my machine
+ currentFreeMemBytes = totalFreeMemory(runtime);
+ log.info("Current Free Memory (MB): " + currentFreeMemBytes / 1024 / 1024);
+ }
+ log.info("Wrote: " + archive.toString());
+ log.info("Started w/ free memory: " + startFreeMemBytes / 1024 / 1024 + "MB");
+ log.info("Current free memory: " + currentFreeMemBytes / 1024 / 1024 + "MB");
+
+ // Export; at this point we have less than 50% available memory so
+ // we can't carry the whole archive in RAM twice; this
+ // should ensure the ZIP impl uses an internal buffer
+ archive.as(ZipExporter.class).exportZip();
+ }
+
+ /**
* Test to make sure an archive can be exported to Zip (file) and all
* contents are correctly located in the Zip.
* @throws Exception
@@ -214,7 +258,6 @@
archive.add(new Asset()
{
-
@Override
public InputStream openStream()
{
@@ -314,4 +357,45 @@
Assert.assertNull("ZIP should not have explicit root path written (SHRINKWRAP-96)", rootEntry);
}
+ /**
+ * Obtains an estimate of the total amount of free memory available to the JVM
+ * @param runtime
+ * @return
+ */
+ private static long totalFreeMemory(final Runtime runtime)
+ {
+ return runtime.maxMemory() - runtime.totalMemory() + runtime.freeMemory();
+ }
+
+ /**
+ * An {@link Asset} which contains a megabyte of dummy data
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ */
+ private static class MegaByteAsset extends ByteArrayAsset implements Asset
+ {
+ /**
+ * Dummy megabyte
+ */
+ private static int MEGA = 1024 * 1024;
+
+ private static final Random random = new Random();
+
+ private MegaByteAsset(final byte[] content)
+ {
+ super(content);
+ }
+
+ static MegaByteAsset newInstance()
+ {
+ /**s
+ * Bytes must be random/distributed so that compressing these in ZIP
+ * isn't too efficient
+ */
+ final byte[] content = new byte[MEGA];
+ random.nextBytes(content);
+ return new MegaByteAsset(content);
+ }
+ }
+
}
More information about the jboss-svn-commits
mailing list