Skip to content

Commit

Permalink
[RESTEASY-1650]:Improve testcase to copy stream after system.gc()
Browse files Browse the repository at this point in the history
  • Loading branch information
jimma authored and asoldano committed Jun 29, 2017
1 parent 8b0532f commit 26c5755
Showing 1 changed file with 29 additions and 6 deletions.
Expand Up @@ -23,7 +23,15 @@

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.management.Notification;
import javax.management.NotificationEmitter;
import javax.management.NotificationListener;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation.Builder;
Expand All @@ -41,7 +49,6 @@
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.junit.Assert;

@RunWith(Arquillian.class)
Expand Down Expand Up @@ -79,11 +86,27 @@ private String generateURL(String path)
public void testStream() throws Exception
{
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
Builder builder = client.target(generateURL("/test/document/abc/content")).request();
//builder.get().readEntity explicitly on the same line below and not saved in any temp variable
//to let the JVM try finalizing the ClientResponse object
IOUtils.copy(builder.get().readEntity(InputStream.class), baos);
Assert.assertEquals(10000000, baos.size());
Builder builder = client.target(generateURL("/test/document/abc/content")).request();
//builder.get().readEntity explicitly on the same line below and not saved in any temp variable
//to let the JVM try finalizing the ClientResponse object
InputStream ins = builder.get().readEntity(InputStream.class);
//suggest jvm to do gc and wait the gc notification
final CountDownLatch coutDown = new CountDownLatch(1);
List<GarbageCollectorMXBean> gcbeans = ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean gcbean : gcbeans) {
NotificationEmitter emitter = (NotificationEmitter) gcbean;
NotificationListener listener = new NotificationListener() {
public void handleNotification(Notification notification,
Object handback) {
coutDown.countDown();
}
};
emitter.addNotificationListener(listener, null, null);
}
System.gc();
coutDown.await(10, TimeUnit.SECONDS);
IOUtils.copy(ins, baos);
Assert.assertEquals(100000000, baos.size());
}
}

Expand Down

0 comments on commit 26c5755

Please sign in to comment.