diff --git a/src/main/java/org/cactoos/io/InputWithRetry.java b/src/main/java/org/cactoos/io/InputWithRetry.java new file mode 100644 index 000000000..fc09b602b --- /dev/null +++ b/src/main/java/org/cactoos/io/InputWithRetry.java @@ -0,0 +1,77 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2024 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.cactoos.io; + +import java.io.InputStream; +import java.time.Duration; +import org.cactoos.Input; +import org.cactoos.func.Retry; + +/** + * Input with retry. + * + * @since 1.0 + */ +public final class InputWithRetry implements Input { + + /** + * Retry mechanism for {@link Input} operations, attempting to open + * an {@link InputStream} multiple times in case of failure. + * + *

The retry logic wraps around the {@link Input#stream()} method, + * enabling conditional reattempts according to the provided retry + * strategy. The number of attempts and the duration between attempts + * are configurable through the {@link Retry} instance. + */ + private final Retry retry; + + /** + * The original {@link Input} instance to be used for retry operations. + * + *

This input provides the underlying data stream that the retry + * mechanism will attempt to read from. It is passed into the + * {@link Retry} logic to enable multiple attempts at accessing the + * input stream in case of failure. + */ + private final Input input; + + public InputWithRetry(final Input main, final int att) { + this(main, new Retry<>(Input::stream, att)); + } + + private InputWithRetry(final Input input, final Retry retry) { + this.input = input; + this.retry = retry; + } + + public InputWithRetry(final Input main, final int att, final Duration dur) { + this(main, new Retry<>(Input::stream, att, dur)); + } + + @Override + public InputStream stream() throws Exception { + return this.retry.apply(this.input); + } +} diff --git a/src/test/java/org/cactoos/io/InputWithRetryTest.java b/src/test/java/org/cactoos/io/InputWithRetryTest.java new file mode 100644 index 000000000..b10076659 --- /dev/null +++ b/src/test/java/org/cactoos/io/InputWithRetryTest.java @@ -0,0 +1,79 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2024 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.cactoos.io; + +import java.time.Duration; +import java.util.concurrent.atomic.AtomicInteger; +import org.hamcrest.core.IsEqual; +import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.Throws; + +/** + * Test case for {@link InputWithRetry}. + * + * @since 1.0 + */ +final class InputWithRetryTest { + + @Test + void testInputWithRetryShouldRead() throws Exception { + final AtomicInteger cnt = new AtomicInteger(0); + final InputWithRetry iwr = new InputWithRetry( + () -> { + if (cnt.getAndIncrement() < 2) { + throw new IllegalArgumentException("Test exception"); + } + return new DeadInputStream(); + }, + 3, + Duration.ZERO + ); + new Assertion<>( + "should read -1 from stream", + iwr.stream().read(), + new IsEqual<>(-1) + ).affirm(); + } + + @Test + void testExceededNumberOfAttempts() { + final AtomicInteger cnt = new AtomicInteger(0); + final InputWithRetry iwr = new InputWithRetry( + () -> { + if (cnt.getAndIncrement() < 3) { + throw new IllegalArgumentException("Test exception"); + } + return new DeadInputStream(); + }, + 3 + ); + new Assertion<>( + "should throw same exception", + iwr::stream, + new Throws<>(IllegalArgumentException.class) + ).affirm(); + } +}