Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed May 2, 2024
2 parents c1c34c1 + a3c0e82 commit f184d9a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import com.jcabi.aspects.RetryOnFailure;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.cactoos.Text;
import org.cactoos.scalar.Unchecked;
import org.cactoos.text.Sticky;
import org.cactoos.text.TextEnvelope;
import org.cactoos.text.TextOf;

Expand All @@ -49,15 +47,13 @@ final class CommitHashesText extends TextEnvelope {
/**
* Cache.
*/
private static final Text CACHE = new Sticky(
CommitHashesText.asText(CommitHashesText.HOME)
);
private static final String CACHE = CommitHashesText.asText(CommitHashesText.HOME);

/**
* Constructor.
*/
CommitHashesText() {
super(CommitHashesText.CACHE);
super(() -> CommitHashesText.CACHE);
}

/**
Expand All @@ -66,10 +62,9 @@ final class CommitHashesText extends TextEnvelope {
* @return The body of the web page
*/
@RetryOnFailure(delay = 1L, unit = TimeUnit.SECONDS)
private static Text asText(final String url) {
final String body = new Unchecked<>(
private static String asText(final String url) {
return new Unchecked<>(
() -> new TextOf(new URL(url)).asString()
).value();
return new TextOf(body);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
package org.eolang.maven.hash;

import com.yegor256.WeAreOnline;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.cactoos.Scalar;
import org.cactoos.experimental.Threads;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand All @@ -33,6 +37,10 @@
* Test case for {@link CommitHashesText}.
*
* @since 0.37.0
* @todo #3122:60min Add "Reload" to the test CommitHashesTextTest#isThreadSafe
* when issue about "Reload" annotation will be solved.
* We need to reinitialize some static fields of the class
* before the test will be executed.
*/
final class CommitHashesTextTest {

Expand All @@ -45,4 +53,23 @@ void downloadsDefaultList() throws Exception {
Matchers.containsString("master")
);
}

@Test
void isThreadSafe() {
final int threads = 200;
boolean nonulls = true;
for (final boolean bool: new Threads<>(
threads,
Stream.generate(
() -> (Scalar<Boolean>) () -> new CommitHashesText().asString() != null
).limit(threads).collect(Collectors.toList())
)) {
nonulls &= bool;
}
MatcherAssert.assertThat(
"Can be used in different threads without NPE",
nonulls,
Matchers.equalTo(true)
);
}
}

0 comments on commit f184d9a

Please sign in to comment.