Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Oct 23, 2022
2 parents f8baf59 + 21421ad commit b3c11a4
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/main/java/com/yegor256/tojos/TjSynchronized.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 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 com.yegor256.tojos;

import java.io.IOException;
import java.util.List;
import java.util.function.Predicate;

/**
* All Tojos in a {@link Mono}.
*
* The class is thread-safe.
*
* @since 0.3.0
*/
public final class TjSynchronized implements Tojos {

/**
* The wrapped tojos.
*/
private final Tojos wrapped;

/**
* Ctor.
*
* @param tojos The tojos
*/
public TjSynchronized(final Tojos tojos) {
this.wrapped = tojos;
}

@Override
public Tojo add(final String name) {
synchronized (this.wrapped) {
return this.wrapped.add(name);
}
}

@Override
public List<Tojo> select(final Predicate<Tojo> filter) {
return this.wrapped.select(filter);
}

@Override
public void close() throws IOException {
this.wrapped.close();
}
}
69 changes: 69 additions & 0 deletions src/test/java/com/yegor256/tojos/TjSynchronizedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 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 com.yegor256.tojos;

import java.nio.file.Path;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/**
* Test case for {@link TjSynchronized}.
*
* @since 0.3.0
*/
class TjSynchronizedTest {

@ParameterizedTest
@ValueSource(strings = {"x.csv", "x.json"})
void addTojoParallel(final String file, @TempDir final Path temp) throws InterruptedException {
final Tojos tojos = new TjSynchronized(new TjDefault(new MnJson(temp.resolve(file))));
final int threads = 100;
final ExecutorService service = Executors.newFixedThreadPool(threads);
final CountDownLatch latch = new CountDownLatch(1);
for (int idx = 0; idx < threads; ++idx) {
final int curr = idx;
service.submit(
(Callable<?>) () -> {
latch.await();
return tojos.add("foo".concat(String.valueOf(curr)));
}
);
}
latch.countDown();
service.shutdown();
assert service.awaitTermination(1, TimeUnit.MINUTES);
MatcherAssert.assertThat(
new TjSmart(tojos).size(),
Matchers.equalTo(threads)
);
}
}

0 comments on commit b3c11a4

Please sign in to comment.