Fixed resource leaks in tests
Project: http://git-wip-us.apache.org/repos/asf/nifi/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/598e32f3 Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/598e32f3 Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/598e32f3 Branch: refs/heads/NIFI-1073 Commit: 598e32f3dc56ab628ae9d251e1502a788512f9c7 Parents: 1e6c10f Author: Tony Kurc <[email protected]> Authored: Sun Nov 8 22:52:16 2015 -0500 Committer: Tony Kurc <[email protected]> Committed: Sun Nov 8 22:52:16 2015 -0500 ---------------------------------------------------------------------- .../stream/io/TestLeakyBucketThrottler.java | 56 ++++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi/blob/598e32f3/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/TestLeakyBucketThrottler.java ---------------------------------------------------------------------- diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/TestLeakyBucketThrottler.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/TestLeakyBucketThrottler.java index e1d6ce0..ac9bb62 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/TestLeakyBucketThrottler.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/TestLeakyBucketThrottler.java @@ -55,11 +55,11 @@ public class TestLeakyBucketThrottler { @Test(timeout = 10000) public void testInputStreamInterface() throws IOException { - // throttle rate at 1 MB/sec - final LeakyBucketStreamThrottler throttler = new LeakyBucketStreamThrottler(1024 * 1024); final byte[] data = new byte[1024 * 1024 * 4]; - try ( final ByteArrayInputStream bais = new ByteArrayInputStream(data); + // throttle rate at 1 MB/sec + try ( final LeakyBucketStreamThrottler throttler = new LeakyBucketStreamThrottler(1024 * 1024); + final ByteArrayInputStream bais = new ByteArrayInputStream(data); final InputStream throttledIn = throttler.newThrottledInputStream(bais); final ByteArrayOutputStream baos = new ByteArrayOutputStream() ){ @@ -80,36 +80,36 @@ public class TestLeakyBucketThrottler { @Test(timeout = 10000) public void testDirectInterface() throws IOException, InterruptedException { // throttle rate at 1 MB/sec - final LeakyBucketStreamThrottler throttler = new LeakyBucketStreamThrottler(1024 * 1024); - - // create 3 threads, each sending ~2 MB - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final List<Thread> threads = new ArrayList<Thread>(); - for (int i = 0; i < 3; i++) { - final Thread t = new WriterThread(i, throttler, baos); - threads.add(t); - } + try (final LeakyBucketStreamThrottler throttler = new LeakyBucketStreamThrottler(1024 * 1024); + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); ){ + // create 3 threads, each sending ~2 MB + final List<Thread> threads = new ArrayList<Thread>(); + for (int i = 0; i < 3; i++) { + final Thread t = new WriterThread(i, throttler, baos); + threads.add(t); + } - final long start = System.currentTimeMillis(); - for (final Thread t : threads) { - t.start(); - } + final long start = System.currentTimeMillis(); + for (final Thread t : threads) { + t.start(); + } - for (final Thread t : threads) { - t.join(); - } - final long elapsed = System.currentTimeMillis() - start; + for (final Thread t : threads) { + t.join(); + } + final long elapsed = System.currentTimeMillis() - start; - throttler.close(); + throttler.close(); - // To send 15 MB, it should have taken at least 5 seconds and no more than 7 seconds, to - // allow for busy-ness and the fact that we could write a tiny bit more than the limit. - assertTrue(elapsed > 5000); - assertTrue(elapsed < 7000); + // To send 15 MB, it should have taken at least 5 seconds and no more than 7 seconds, to + // allow for busy-ness and the fact that we could write a tiny bit more than the limit. + assertTrue(elapsed > 5000); + assertTrue(elapsed < 7000); - // ensure bytes were copied out appropriately - assertEquals(3 * (2 * 1024 * 1024 + 1), baos.getBufferLength()); - assertEquals((byte) 'A', baos.getUnderlyingBuffer()[baos.getBufferLength() - 1]); + // ensure bytes were copied out appropriately + assertEquals(3 * (2 * 1024 * 1024 + 1), baos.getBufferLength()); + assertEquals((byte) 'A', baos.getUnderlyingBuffer()[baos.getBufferLength() - 1]); + } } private static class WriterThread extends Thread {
