| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 23f9a79 commit 03aa3e7
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,66 @@ | |||
| 1 | + /* | ||
| 2 | + * Copyright 2025 Google LLC | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | + package com.google.cloud.storage; | ||
| 18 | + | ||
| 19 | + import static com.google.cloud.storage.TestUtils.assertAll; | ||
| 20 | + import static com.google.common.truth.Truth.assertWithMessage; | ||
| 21 | + | ||
| 22 | + import java.io.IOException; | ||
| 23 | + import java.util.concurrent.atomic.AtomicInteger; | ||
| 24 | + import org.junit.Test; | ||
| 25 | + | ||
| 26 | + public class IOAutoCloseableTest { | ||
| 27 | + | ||
| 28 | + @Test | ||
| 29 | + public void andThenCorrectlyOrdered() throws Exception { | ||
| 30 | + | ||
| 31 | + AtomicInteger counter = new AtomicInteger(1); | ||
| 32 | + | ||
| 33 | + TestIOAutoClosable t1 = TestIOAutoClosable.of(counter); | ||
| 34 | + TestIOAutoClosable t2 = TestIOAutoClosable.of(counter); | ||
| 35 | + TestIOAutoClosable t3 = TestIOAutoClosable.of(counter); | ||
| 36 | + | ||
| 37 | + final IOAutoCloseable then = t1.andThen(t2).andThen(t3); | ||
| 38 | + | ||
| 39 | + then.close(); | ||
| 40 | + | ||
| 41 | + assertAll( | ||
| 42 | + () -> assertWithMessage("t1.closeValue").that(t1.closeValue).isEqualTo(1), | ||
| 43 | + () -> assertWithMessage("t2.closeValue").that(t2.closeValue).isEqualTo(2), | ||
| 44 | + () -> assertWithMessage("t3.closeValue").that(t3.closeValue).isEqualTo(3)); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + static final class TestIOAutoClosable implements IOAutoCloseable { | ||
| 48 | + private final AtomicInteger counter; | ||
| 49 | + private long closeValue; | ||
| 50 | + | ||
| 51 | + private TestIOAutoClosable(AtomicInteger counter) { | ||
| 52 | + this.counter = counter; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + @Override | ||
| 56 | + public void close() throws IOException { | ||
| 57 | + if (closeValue == 0) { | ||
| 58 | + closeValue = counter.getAndIncrement(); | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + private static TestIOAutoClosable of(AtomicInteger counter) { | ||
| 63 | + return new TestIOAutoClosable(counter); | ||
| 64 | + } | ||
| 65 | + } | ||
| 66 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments