Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stawirej committed Dec 22, 2023
1 parent b7bc181 commit acb69d2
Showing 1 changed file with 236 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.RepeatedTest;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
Expand Down Expand Up @@ -43,4 +44,239 @@ void Build_multi_threads_collider_list() {
then(exceptions).isEmpty();
then(list).hasSize(threadsCount).containsOnly("text");
}

@Nested
class Build_threads_collider_with_custom_executor_await_termination_timeout {

@RepeatedTest(10)
void as_nanoseconds() {
// Given
List<String> list = Collections.synchronizedList(new ArrayList<>());
List<Exception> exceptions = new ArrayList<>();
int threadsCount = Runtime.getRuntime().availableProcessors();
int addThreadsCount = threadsCount / 2;
int removeThreadsCount = threadsCount / 2;

for (int i = 0; i < threadsCount; i++) {
list.add("text");
}

// When
try (MultiThreadsCollider threadsCollider =
multiThreadsCollider()
.withAction(() -> list.add("text"))
.times(addThreadsCount)
.withAction(() -> list.remove("text"))
.times(removeThreadsCount)
.withThreadsExceptionsConsumer(exceptions::add)
.withAwaitTerminationTimeout(1_000_000)
.asNanoseconds()
.build()) {

threadsCollider.collide();
}

// Then
then(exceptions).isEmpty();
then(list).hasSize(threadsCount).containsOnly("text");
}

@RepeatedTest(10)
void as_microseconds() {
// Given
List<String> list = Collections.synchronizedList(new ArrayList<>());
List<Exception> exceptions = new ArrayList<>();
int threadsCount = Runtime.getRuntime().availableProcessors();
int addThreadsCount = threadsCount / 2;
int removeThreadsCount = threadsCount / 2;

for (int i = 0; i < threadsCount; i++) {
list.add("text");
}

// When
try (MultiThreadsCollider threadsCollider =
multiThreadsCollider()
.withAction(() -> list.add("text"))
.times(addThreadsCount)
.withAction(() -> list.remove("text"))
.times(removeThreadsCount)
.withThreadsExceptionsConsumer(exceptions::add)
.withAwaitTerminationTimeout(1_000)
.asMicroseconds()
.build()) {

threadsCollider.collide();
}

// Then
then(exceptions).isEmpty();
then(list).hasSize(threadsCount).containsOnly("text");
}

@RepeatedTest(10)
void as_milliseconds() {
// Given
List<String> list = Collections.synchronizedList(new ArrayList<>());
List<Exception> exceptions = new ArrayList<>();
int threadsCount = Runtime.getRuntime().availableProcessors();
int addThreadsCount = threadsCount / 2;
int removeThreadsCount = threadsCount / 2;

for (int i = 0; i < threadsCount; i++) {
list.add("text");
}

// When
try (MultiThreadsCollider threadsCollider =
multiThreadsCollider()
.withAction(() -> list.add("text"))
.times(addThreadsCount)
.withAction(() -> list.remove("text"))
.times(removeThreadsCount)
.withThreadsExceptionsConsumer(exceptions::add)
.withAwaitTerminationTimeout(1)
.asMilliseconds()
.build()) {

threadsCollider.collide();
}

// Then
then(exceptions).isEmpty();
then(list).hasSize(threadsCount).containsOnly("text");
}

@RepeatedTest(10)
void as_seconds() {
// Given
List<String> list = Collections.synchronizedList(new ArrayList<>());
List<Exception> exceptions = new ArrayList<>();
int threadsCount = Runtime.getRuntime().availableProcessors();
int addThreadsCount = threadsCount / 2;
int removeThreadsCount = threadsCount / 2;

for (int i = 0; i < threadsCount; i++) {
list.add("text");
}

// When
try (MultiThreadsCollider threadsCollider =
multiThreadsCollider()
.withAction(() -> list.add("text"))
.times(addThreadsCount)
.withAction(() -> list.remove("text"))
.times(removeThreadsCount)
.withThreadsExceptionsConsumer(exceptions::add)
.withAwaitTerminationTimeout(1)
.asSeconds()
.build()) {

threadsCollider.collide();
}

// Then
then(exceptions).isEmpty();
then(list).hasSize(threadsCount).containsOnly("text");
}

@RepeatedTest(10)
void as_minutes() {
// Given
List<String> list = Collections.synchronizedList(new ArrayList<>());
List<Exception> exceptions = new ArrayList<>();
int threadsCount = Runtime.getRuntime().availableProcessors();
int addThreadsCount = threadsCount / 2;
int removeThreadsCount = threadsCount / 2;

for (int i = 0; i < threadsCount; i++) {
list.add("text");
}

// When
try (MultiThreadsCollider threadsCollider =
multiThreadsCollider()
.withAction(() -> list.add("text"))
.times(addThreadsCount)
.withAction(() -> list.remove("text"))
.times(removeThreadsCount)
.withThreadsExceptionsConsumer(exceptions::add)
.withAwaitTerminationTimeout(1)
.asMinutes()
.build()) {

threadsCollider.collide();
}

// Then
then(exceptions).isEmpty();
then(list).hasSize(threadsCount).containsOnly("text");
}

@RepeatedTest(10)
void as_hours() {
// Given
List<String> list = Collections.synchronizedList(new ArrayList<>());
List<Exception> exceptions = new ArrayList<>();
int threadsCount = Runtime.getRuntime().availableProcessors();
int addThreadsCount = threadsCount / 2;
int removeThreadsCount = threadsCount / 2;

for (int i = 0; i < threadsCount; i++) {
list.add("text");
}

// When
try (MultiThreadsCollider threadsCollider =
multiThreadsCollider()
.withAction(() -> list.add("text"))
.times(addThreadsCount)
.withAction(() -> list.remove("text"))
.times(removeThreadsCount)
.withThreadsExceptionsConsumer(exceptions::add)
.withAwaitTerminationTimeout(1)
.asHours()
.build()) {

threadsCollider.collide();
}

// Then
then(exceptions).isEmpty();
then(list).hasSize(threadsCount).containsOnly("text");
}

@RepeatedTest(10)
void as_days() {
// Given
List<String> list = Collections.synchronizedList(new ArrayList<>());
List<Exception> exceptions = new ArrayList<>();
int threadsCount = Runtime.getRuntime().availableProcessors();
int addThreadsCount = threadsCount / 2;
int removeThreadsCount = threadsCount / 2;

for (int i = 0; i < threadsCount; i++) {
list.add("text");
}

// When
try (MultiThreadsCollider threadsCollider =
multiThreadsCollider()
.withAction(() -> list.add("text"))
.times(addThreadsCount)
.withAction(() -> list.remove("text"))
.times(removeThreadsCount)
.withThreadsExceptionsConsumer(exceptions::add)
.withAwaitTerminationTimeout(1)
.asDays()
.build()) {

threadsCollider.collide();
}

// Then
then(exceptions).isEmpty();
then(list).hasSize(threadsCount).containsOnly("text");
}
}
}

0 comments on commit acb69d2

Please sign in to comment.