Skip to content

Commit

Permalink
test: add to map
Browse files Browse the repository at this point in the history
  • Loading branch information
stawirej committed Oct 21, 2023
1 parent b48620a commit 6a6fad2
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.RepeatedTest;
Expand Down Expand Up @@ -44,4 +46,32 @@ void Thread_safe_adding_to_list() {
// Then
then(list).hasSize(threadsCount).containsOnly("bar");
}

@RepeatedTest(10)
void Thread_safe_adding_to_map() {
// Given
Map<String, String> map = new ConcurrentHashMap<>();

// When
try (ThreadsCollider threadsCollider = threadsCollider().withAvailableProcessors().build()) {
threadsCollider.collide(() -> map.put("foo", "bar"));
}

// Then
then(map).hasSize(1).containsEntry("foo", "bar");
}

@RepeatedTest(10)
void Thread_safe_adding_to_map_when_absent() {
// Given
Map<String, String> map = new ConcurrentHashMap<>();

// When
try (ThreadsCollider threadsCollider = threadsCollider().withAvailableProcessors().build()) {
threadsCollider.collide(() -> map.putIfAbsent("foo", "bar"));
}

// Then
then(map).hasSize(1).containsEntry("foo", "bar");
}
}

0 comments on commit 6a6fad2

Please sign in to comment.