-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |