diff --git a/src/test/java/pl/amazingcode/threadscollider/multi/MultiThreadsCollider_Scenarios.java b/src/test/java/pl/amazingcode/threadscollider/multi/MultiThreadsCollider_Scenarios.java index cc78e72..780c977 100644 --- a/src/test/java/pl/amazingcode/threadscollider/multi/MultiThreadsCollider_Scenarios.java +++ b/src/test/java/pl/amazingcode/threadscollider/multi/MultiThreadsCollider_Scenarios.java @@ -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) @@ -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 list = Collections.synchronizedList(new ArrayList<>()); + List 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 list = Collections.synchronizedList(new ArrayList<>()); + List 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 list = Collections.synchronizedList(new ArrayList<>()); + List 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 list = Collections.synchronizedList(new ArrayList<>()); + List 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 list = Collections.synchronizedList(new ArrayList<>()); + List 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 list = Collections.synchronizedList(new ArrayList<>()); + List 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 list = Collections.synchronizedList(new ArrayList<>()); + List 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"); + } + } }