gianm commented on a change in pull request #10247: URL: https://github.com/apache/druid/pull/10247#discussion_r553770448
########## File path: core/src/test/java/org/apache/druid/utils/CloseableUtilsTest.java ########## @@ -0,0 +1,415 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.utils; + +import com.google.common.base.Throwables; +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.Test; +import org.junit.internal.matchers.ThrowableCauseMatcher; +import org.junit.internal.matchers.ThrowableMessageMatcher; + +import javax.annotation.Nullable; +import java.io.Closeable; +import java.io.IOException; +import java.util.Arrays; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Consumer; + +public class CloseableUtilsTest +{ + private final TestCloseable quietCloseable = new TestCloseable(null); + private final TestCloseable quietCloseable2 = new TestCloseable(null); + private final TestCloseable ioExceptionCloseable = new TestCloseable(new IOException()); + private final TestCloseable runtimeExceptionCloseable = new TestCloseable(new IllegalArgumentException()); + + // For closeAndSuppressException tests. + private final AtomicLong chomped = new AtomicLong(); + private final Consumer<Exception> chomper = e -> chomped.incrementAndGet(); + + @Test + public void test_closeAll_array_quiet() throws IOException + { + CloseableUtils.closeAll(quietCloseable, null, quietCloseable2); + assertClosed(quietCloseable, quietCloseable2); + } + + @Test + public void test_closeAll_list_quiet() throws IOException + { + CloseableUtils.closeAll(Arrays.asList(quietCloseable, null, quietCloseable2)); + assertClosed(quietCloseable, quietCloseable2); + } + + @Test + public void test_closeAll_array_loud() + { + Exception e = null; + try { + CloseableUtils.closeAll(quietCloseable, null, ioExceptionCloseable, quietCloseable2, runtimeExceptionCloseable); + } + catch (Exception e2) { + e = e2; + } + + assertClosed(quietCloseable, ioExceptionCloseable, quietCloseable2, runtimeExceptionCloseable); + + // First exception + Assert.assertThat(e, CoreMatchers.instanceOf(IOException.class)); + + // Second exception + Assert.assertEquals(1, e.getSuppressed().length); + Assert.assertThat(e.getSuppressed()[0], CoreMatchers.instanceOf(RuntimeException.class)); Review comment: Ah, yeah, that's a better test. I changed it. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
