Skip to content

Commit

Permalink
[RESTEASY-1650]:Remove the listener from GCNotification after test run
Browse files Browse the repository at this point in the history
  • Loading branch information
kanovotn authored and asoldano committed Jun 29, 2017
1 parent 26c5755 commit 77f7865
Showing 1 changed file with 52 additions and 50 deletions.
Expand Up @@ -53,61 +53,63 @@

@RunWith(Arquillian.class)
@RunAsClient
public class ResponseStreamPrematurelyClosedTest
{
public class ResponseStreamPrematurelyClosedTest {

static Client client;
static Client client;

@Deployment
public static Archive<?> deploy() throws Exception
{
WebArchive war = TestUtil.prepareArchive(ResponseStreamPrematurelyClosedTest.class.getSimpleName());
return TestUtil.finishContainerPrepare(war, null, TestResourceImpl.class);
}
@Deployment
public static Archive<?> deploy() throws Exception {
WebArchive war = TestUtil.prepareArchive(ResponseStreamPrematurelyClosedTest.class.getSimpleName());
return TestUtil.finishContainerPrepare(war, null, TestResourceImpl.class);
}

@BeforeClass
public static void init()
{
client = ClientBuilder.newClient();
}
@BeforeClass
public static void init() {
client = ClientBuilder.newClient();
}

@AfterClass
public static void after() throws Exception
{
client.close();
}
@AfterClass
public static void after() throws Exception {
client.close();
}

private String generateURL(String path)
{
return PortProviderUtil.generateURL(path, ResponseStreamPrematurelyClosedTest.class.getSimpleName());
}
private String generateURL(String path) {
return PortProviderUtil.generateURL(path, ResponseStreamPrematurelyClosedTest.class.getSimpleName());
}

@Test
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
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());
}
}
@Test
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
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();
NotificationListener listener = new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
coutDown.countDown();
}
};
try {
for (GarbageCollectorMXBean gcbean : gcbeans) {
NotificationEmitter emitter = (NotificationEmitter) gcbean;
emitter.addNotificationListener(listener, null, null);
}
System.gc();
coutDown.await(10, TimeUnit.SECONDS);

IOUtils.copy(ins, baos);
Assert.assertEquals(10000000, baos.size());
} finally {
//remove the listener
for (GarbageCollectorMXBean gcbean : gcbeans) {
((NotificationEmitter) gcbean).removeNotificationListener(listener);
}
}
}

}
}

0 comments on commit 77f7865

Please sign in to comment.