Search in sources :

Example 41 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project druid by druid-io.

the class CompressionUtilsTest method testStreamErrorGunzip.

@Test(expected = IOException.class)
public void testStreamErrorGunzip() throws Exception {
    final File tmpDir = temporaryFolder.newFolder("testGoodGzipByteSource");
    final File gzFile = new File(tmpDir, testFile.getName() + ".gz");
    Assert.assertFalse(gzFile.exists());
    CompressionUtils.gzip(Files.asByteSource(testFile), Files.asByteSink(gzFile), Predicates.<Throwable>alwaysTrue());
    Assert.assertTrue(gzFile.exists());
    try (final InputStream inputStream = CompressionUtils.gzipInputStream(new FileInputStream(gzFile))) {
        assertGoodDataStream(inputStream);
    }
    if (testFile.exists() && !testFile.delete()) {
        throw new RuntimeException(String.format("Unable to delete file [%s]", testFile.getAbsolutePath()));
    }
    Assert.assertFalse(testFile.exists());
    final AtomicLong flushes = new AtomicLong(0L);
    CompressionUtils.gunzip(new FileInputStream(gzFile), new FilterOutputStream(new FileOutputStream(testFile) {

        @Override
        public void flush() throws IOException {
            if (flushes.getAndIncrement() > 0) {
                super.flush();
            } else {
                throw new IOException("Test exception");
            }
        }
    }));
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) GZIPInputStream(java.util.zip.GZIPInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FilterOutputStream(java.io.FilterOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 42 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project druid by druid-io.

the class StreamUtilsTest method testRetryExceptionOnFlush.

@Test
public void testRetryExceptionOnFlush() {
    final byte[] bytes = new byte[1 << 10];
    Random random = new Random(47831947819L);
    random.nextBytes(bytes);
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final AtomicLong outputFlushes = new AtomicLong(0);
    Assert.assertEquals(bytes.length, StreamUtils.retryCopy(new ByteSource() {

        @Override
        public InputStream openStream() throws IOException {
            return new ByteArrayInputStream(bytes);
        }
    }, new ByteSink() {

        @Override
        public OutputStream openStream() throws IOException {
            byteArrayOutputStream.reset();
            return new FilterOutputStream(byteArrayOutputStream) {

                @Override
                public void flush() throws IOException {
                    if (outputFlushes.getAndIncrement() > 0) {
                        out.flush();
                    } else {
                        throw new IOException("Test exception");
                    }
                }
            };
        }
    }, FileUtils.IS_EXCEPTION, 10));
    // 2 closes and 2 manual flushes
    Assert.assertEquals(4, outputFlushes.get());
    Assert.assertArrayEquals(bytes, byteArrayOutputStream.toByteArray());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ByteSink(com.google.common.io.ByteSink) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteSource(com.google.common.io.ByteSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FilterOutputStream(java.io.FilterOutputStream) Test(org.junit.Test)

Example 43 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project druid by druid-io.

the class LifecycleTest method testStartStopOnce.

@Test
public void testStartStopOnce() throws Exception {
    final Lifecycle lifecycle = new Lifecycle();
    final AtomicLong startedCount = new AtomicLong(0L);
    final AtomicLong failedCount = new AtomicLong(0L);
    Lifecycle.Handler exceptionalHandler = new Lifecycle.Handler() {

        final AtomicBoolean started = new AtomicBoolean(false);

        @Override
        public void start() throws Exception {
            if (!started.compareAndSet(false, true)) {
                failedCount.incrementAndGet();
                throw new ISE("Already started");
            }
            startedCount.incrementAndGet();
        }

        @Override
        public void stop() {
            if (!started.compareAndSet(true, false)) {
                failedCount.incrementAndGet();
                throw new ISE("Not yet started started");
            }
        }
    };
    lifecycle.addHandler(exceptionalHandler);
    lifecycle.start();
    lifecycle.stop();
    lifecycle.stop();
    lifecycle.stop();
    lifecycle.start();
    lifecycle.stop();
    Assert.assertEquals(2, startedCount.get());
    Assert.assertEquals(0, failedCount.get());
    Exception ex = null;
    try {
        exceptionalHandler.stop();
    } catch (Exception e) {
        ex = e;
    }
    Assert.assertNotNull("Should have exception", ex);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) ISE(io.druid.java.util.common.ISE) Test(org.junit.Test)

Example 44 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project druid by druid-io.

the class LimitedSequenceTest method testNoSideEffects.

@Test
public void testNoSideEffects() throws Exception {
    final List<Integer> nums = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    final AtomicLong accumulated = new AtomicLong(0);
    final Sequence<Integer> seq = Sequences.limit(Sequences.simple(Iterables.transform(nums, new Function<Integer, Integer>() {

        @Override
        public Integer apply(Integer input) {
            accumulated.addAndGet(input);
            return input;
        }
    })), 5);
    Assert.assertEquals(10, seq.accumulate(0, new IntAdditionAccumulator()).intValue());
    Assert.assertEquals(10, accumulated.get());
    Assert.assertEquals(10, seq.accumulate(0, new IntAdditionAccumulator()).intValue());
    Assert.assertEquals(20, accumulated.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 45 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project druid by druid-io.

the class CloserRuleTest method testClosesEverything.

@Test
public void testClosesEverything() {
    final AtomicLong counter = new AtomicLong(0L);
    final CloserRule closer = new CloserRule(true);
    final String ioExceptionMsg = "You can't triple stamp a double stamp!";
    final List<IOException> ioExceptions = Arrays.<IOException>asList(new IOException(ioExceptionMsg), null, new IOException(ioExceptionMsg), null, new IOException(ioExceptionMsg), null);
    for (final IOException throwable : ioExceptions) {
        closer.closeLater(new Closeable() {

            @Override
            public void close() throws IOException {
                counter.incrementAndGet();
                if (throwable != null) {
                    throw throwable;
                }
            }
        });
    }
    Throwable ex = null;
    try {
        run(closer, Runnables.doNothing());
    } catch (Throwable throwable) {
        ex = throwable;
    }
    Assert.assertNotNull(ex);
    Assert.assertEquals(ioExceptions.size(), counter.get());
    Assert.assertEquals(2, ex.getSuppressed().length);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Closeable(java.io.Closeable) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

AtomicLong (java.util.concurrent.atomic.AtomicLong)678 Test (org.junit.Test)261 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)93 IOException (java.io.IOException)82 CountDownLatch (java.util.concurrent.CountDownLatch)68 ArrayList (java.util.ArrayList)65 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)64 HashMap (java.util.HashMap)47 Map (java.util.Map)45 Random (java.util.Random)43 List (java.util.List)42 AtomicReference (java.util.concurrent.atomic.AtomicReference)40 File (java.io.File)34 ExecutorService (java.util.concurrent.ExecutorService)24 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)23 HashSet (java.util.HashSet)21 Test (org.testng.annotations.Test)21 InetSocketAddress (java.net.InetSocketAddress)16 InputStream (java.io.InputStream)13 Set (java.util.Set)13