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");
}
}
}));
}
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());
}
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);
}
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());
}
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);
}
Aggregations