From 77f78659fef47af72a8b5f2315c38131c6bf1f68 Mon Sep 17 00:00:00 2001 From: kanovotn Date: Wed, 28 Jun 2017 16:47:22 +0200 Subject: [PATCH] [RESTEASY-1650]:Remove the listener from GCNotification after test run --- .../ResponseStreamPrematurelyClosedTest.java | 102 +++++++++--------- 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/testsuite/integration-tests/src/test/java/org/jboss/resteasy/test/response/ResponseStreamPrematurelyClosedTest.java b/testsuite/integration-tests/src/test/java/org/jboss/resteasy/test/response/ResponseStreamPrematurelyClosedTest.java index b83fa59788e..4c6d6842b87 100644 --- a/testsuite/integration-tests/src/test/java/org/jboss/resteasy/test/response/ResponseStreamPrematurelyClosedTest.java +++ b/testsuite/integration-tests/src/test/java/org/jboss/resteasy/test/response/ResponseStreamPrematurelyClosedTest.java @@ -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 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 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); + } + } + } + + } }