Skip to content

Commit

Permalink
Merge pull request objectionary#3694 from objectionary/3693
Browse files Browse the repository at this point in the history
com.yegor256:together
  • Loading branch information
yegor256 authored Dec 18, 2024
2 parents ecfc006 + 8667151 commit f84f212
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 71 deletions.
8 changes: 7 additions & 1 deletion eo-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ SOFTWARE.
<artifactId>farea</artifactId>
<!-- version from parent POM -->
</dependency>
<dependency>
<groupId>com.yegor256</groupId>
<artifactId>together</artifactId>
<!-- version from parent POM -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.yegor256</groupId>
<artifactId>mktmp</artifactId>
Expand All @@ -193,7 +199,7 @@ SOFTWARE.
<dependency>
<groupId>com.yegor256</groupId>
<artifactId>tojos</artifactId>
<version>0.18.3</version>
<version>0.18.4</version>
</dependency>
<dependency>
<groupId>javax.json</groupId>
Expand Down
3 changes: 2 additions & 1 deletion eo-maven-plugin/src/main/java/org/eolang/maven/Catalogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.yegor256.tojos.Mono;
import com.yegor256.tojos.TjCached;
import com.yegor256.tojos.TjDefault;
import com.yegor256.tojos.TjSynchronized;
import com.yegor256.tojos.Tojos;
import java.nio.file.Path;
import java.util.Locale;
Expand Down Expand Up @@ -130,7 +131,7 @@ private static Tojos build(final Path path, final String format) {
} else {
mono = new MnPostponed(mono, 500L);
}
return new TjCached(new TjDefault(mono));
return new TjSynchronized(new TjCached(new TjDefault(mono)));
}

}
51 changes: 14 additions & 37 deletions eo-maven-plugin/src/test/java/org/eolang/maven/CatalogsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@

import com.yegor256.Mktmp;
import com.yegor256.MktmpResolver;
import com.yegor256.Together;
import com.yegor256.tojos.Tojo;
import com.yegor256.tojos.Tojos;
import java.nio.file.Path;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.cactoos.Scalar;
import org.cactoos.experimental.Threads;
import org.cactoos.number.SumOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand All @@ -58,43 +54,24 @@ public final class CatalogsTest {
*/
public static final String TO_ADD_MESSAGE = "TO ADD ASSERTION MESSAGE";

/**
* Number of cores on the running system.
*/
private static final int CORES = Runtime.getRuntime().availableProcessors();

@Test
void readsFromTojosConcurrently(@Mktmp final Path tmp) {
final Tojos tojos = Catalogs.INSTANCE.make(tmp.resolve("foreign"), "json");
MatcherAssert.assertThat(
CatalogsTest.TO_ADD_MESSAGE,
new SumOf(
new Threads<>(
CatalogsTest.CORES,
IntStream.range(0, CatalogsTest.CORES)
.mapToObj(i -> tojos.add(UUID.randomUUID().toString()))
.map(CatalogsTest::task)
.collect(Collectors.toList())
)
"adds different elements to catalog",
new Together<>(
thread -> {
final Tojo tojo = tojos.add(UUID.randomUUID().toString());
final String key = "foo";
tojo.set(key, UUID.randomUUID().toString());
tojo.get(key);
tojo.get(key);
tojo.get(key);
tojo.get(key);
return true;
}
),
Matchers.equalTo(CatalogsTest.CORES)
Matchers.not(Matchers.hasItem(false))
);
}

/**
* Task to be executed in parallel.
* @param tojo Tojo
* @return Scalar
*/
private static Scalar<Integer> task(final Tojo tojo) {
return () -> {
final String uuid = "uuid";
tojo.set(uuid, UUID.randomUUID().toString());
tojo.get(uuid);
tojo.get(uuid);
tojo.get(uuid);
tojo.get(uuid);
return 1;
};
}
}
6 changes: 6 additions & 0 deletions eo-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ SOFTWARE.
<!-- version from parent POM -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.yegor256</groupId>
<artifactId>together</artifactId>
<!-- version from parent POM -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.yegor256</groupId>
<artifactId>maybeslow</artifactId>
Expand Down
26 changes: 10 additions & 16 deletions eo-runtime/src/test/java/org/eolang/PhDefaultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@
*/
package org.eolang;

import com.yegor256.Together;
import java.security.SecureRandom;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.cactoos.Scalar;
import org.cactoos.experimental.Threads;
import org.cactoos.set.SetOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -303,17 +299,15 @@ void makesObjectIdentity() {
@Test
void createsDifferentPhiInParallel() {
final int threads = 100;
final Set<PhDefault> objects = ConcurrentHashMap.newKeySet();
new Threads<>(
threads,
Stream.generate(
() -> (Scalar<PhDefault>) Int::new
).limit(threads).collect(Collectors.toList())
).forEach(objects::add);
MatcherAssert.assertThat(
AtCompositeTest.TO_ADD_MESSAGE,
objects,
Matchers.hasSize(threads)
"all objects are unique",
new SetOf<>(
new Together<>(
threads,
t -> new Int()
)
),
Matchers.iterableWithSize(threads)
);
}

Expand Down
29 changes: 13 additions & 16 deletions eo-runtime/src/test/java/org/eolang/PhWithTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
*/
package org.eolang;

import org.cactoos.Func;
import com.yegor256.Together;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.llorllale.cactoos.matchers.RunsInThreads;

/**
* Test case for {@link PhWith}.
Expand Down Expand Up @@ -94,21 +93,19 @@ void printsToString() {
void runsInThreads(final String data) {
final String attr = "foo";
final Phi ref = new PhWith(new DummyWithAtFree(attr), 0, new Data.ToPhi(data));
final Func<Phi, Boolean> actual = phi -> {
MatcherAssert.assertThat(
AtCompositeTest.TO_ADD_MESSAGE,
new Dataized(phi.take(attr)).asString(),
Matchers.is(data)
);
return true;
};
MatcherAssert.assertThat(
AtCompositeTest.TO_ADD_MESSAGE,
actual,
new RunsInThreads<>(
ref,
Runtime.getRuntime().availableProcessors() * 10
)
"works in multiple threads",
new Together<>(
thread -> {
MatcherAssert.assertThat(
AtCompositeTest.TO_ADD_MESSAGE,
new Dataized(ref.take(attr)).asString(),
Matchers.is(data)
);
return true;
}
),
Matchers.not(Matchers.hasItem(false))
);
}

Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ SOFTWARE.
<version>0.0.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.yegor256</groupId>
<artifactId>together</artifactId>
<version>0.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.yegor256</groupId>
<artifactId>maybeslow</artifactId>
Expand Down

0 comments on commit f84f212

Please sign in to comment.