Skip to content

Commit

Permalink
refactor: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
stawirej committed Jan 1, 2024
1 parent 0b83c83 commit a4ad048
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 116 deletions.
4 changes: 4 additions & 0 deletions src/main/java/pl/amazingcode/threadscollider/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ private Action(Runnable runnable, Optional<String> actionName, int times) {

static Action of(Runnable runnable, String actionName, int times) {

if (times < 1) {
throw new IllegalArgumentException("Repeat action at least once.");
}

return new Action(runnable, Optional.ofNullable(actionName), times);
}

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/pl/amazingcode/threadscollider/Processors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package pl.amazingcode.threadscollider;

/** Number of processors available to the Java virtual machine. */
public final class Processors {

/** Use all available processors. */
public static final int ALL = Runtime.getRuntime().availableProcessors();

/** Use half of available processors. */
public static final int HALF = ALL / 2;

/** Use one third of available processors. */
public static final int ONE_THIRD = ALL / 3;

/** Use one quarter of available processors. */
public static final int ONE_QUARTER = ALL / 4;
}
24 changes: 24 additions & 0 deletions src/test/java/pl/amazingcode/threadscollider/Action_Scenarios.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pl.amazingcode.threadscollider;

import static org.assertj.core.api.BDDAssertions.catchThrowable;
import static org.assertj.core.api.BDDAssertions.then;

import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
final class Action_Scenarios {

@Test
void Report_error_on_times_smaller_than_1() {

// When
Throwable throwable = catchThrowable(() -> Action.of(() -> {}, "action", 0));

// Then
then(throwable)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Repeat action at least once.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.TestInfo;
import pl.amazingcode.threadscollider.Processors;
import pl.amazingcode.threadscollider.ThreadsCollider;
import pl.amazingcode.threadscollider.fixtures.assertobject.CollisionsAssert;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
final class Deadlock_Negative_Scenarios {

private static final int ACTION_THREADS_COUNT = Runtime.getRuntime().availableProcessors() / 2;
private static final int TEST_REPETITIONS = 10;
private static final CollisionsAssert collisionsAssert =
CollisionsAssert.newInstance(TEST_REPETITIONS);
Expand Down Expand Up @@ -65,9 +65,9 @@ void Detect_deadlock(TestInfo testInfo) {
try (ThreadsCollider collider =
threadsCollider()
.withAction(() -> update1(list1, list2))
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAction(() -> update2(list2, list1))
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withThreadsExceptionsConsumer(exceptions::add)
.withAwaitTerminationTimeout(100)
.asMilliseconds()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.condition.EnabledIf;
import pl.amazingcode.threadscollider.Processors;
import pl.amazingcode.threadscollider.ThreadsCollider;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
final class Deadlock_Scenarios {

private static final int ACTION_THREADS_COUNT = Runtime.getRuntime().availableProcessors() / 2;
private List<Integer> list1;
private List<Integer> list2;
private Lock lock1;
Expand Down Expand Up @@ -100,9 +100,9 @@ void Detect_deadlock() {
try (ThreadsCollider collider =
threadsCollider()
.withAction(() -> update1(list1, list2), "update1")
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAction(() -> update2(list2, list1), "update2")
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withThreadsExceptionsConsumer(exceptions::add)
.withAwaitTerminationTimeout(100)
.asMilliseconds()
Expand All @@ -126,9 +126,9 @@ void Detect_deadlock_with_locks() {
try (ThreadsCollider collider =
threadsCollider()
.withAction(() -> update1WithLocks(list1, list2), "update1WithLocks")
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAction(() -> update2WithLocks(list2, list1), "update2WithLocks")
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withThreadsExceptionsConsumer(exceptions::add)
.withAwaitTerminationTimeout(100)
.asMilliseconds()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.TestInfo;
import pl.amazingcode.threadscollider.Processors;
import pl.amazingcode.threadscollider.ThreadsCollider;
import pl.amazingcode.threadscollider.fixtures.ThreadUnsafeCounter;
import pl.amazingcode.threadscollider.fixtures.assertobject.CollisionsAssert;
Expand All @@ -14,7 +15,6 @@
final class ThreadsCollider_Negative_Scenarios {

private static final int TEST_REPETITIONS = 200;
private static final int ACTION_THREADS_COUNT = Runtime.getRuntime().availableProcessors() / 2;

private static final CollisionsAssert collisionsAssert =
CollisionsAssert.newInstance(TEST_REPETITIONS);
Expand All @@ -35,9 +35,9 @@ void Build_multi_threads_collider(TestInfo testInfo) {
try (ThreadsCollider threadsCollider =
ThreadsCollider.ThreadsColliderBuilder.threadsCollider()
.withAction(counter::increment)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAction(counter::decrement)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.build()) {

threadsCollider.collide();
Expand All @@ -60,9 +60,9 @@ void as_nanoseconds(TestInfo testInfo) {
try (ThreadsCollider threadsCollider =
ThreadsCollider.ThreadsColliderBuilder.threadsCollider()
.withAction(counter::increment)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAction(counter::decrement)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAwaitTerminationTimeout(1_000_000)
.asNanoseconds()
.build()) {
Expand All @@ -84,9 +84,9 @@ void as_microseconds(TestInfo testInfo) {
try (ThreadsCollider threadsCollider =
ThreadsCollider.ThreadsColliderBuilder.threadsCollider()
.withAction(counter::increment)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAction(counter::decrement)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAwaitTerminationTimeout(1_000)
.asMicroseconds()
.build()) {
Expand All @@ -108,9 +108,9 @@ void as_milliseconds(TestInfo testInfo) {
try (ThreadsCollider threadsCollider =
ThreadsCollider.ThreadsColliderBuilder.threadsCollider()
.withAction(counter::increment)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAction(counter::decrement)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAwaitTerminationTimeout(1)
.asMilliseconds()
.build()) {
Expand All @@ -132,9 +132,9 @@ void as_seconds(TestInfo testInfo) {
try (ThreadsCollider threadsCollider =
ThreadsCollider.ThreadsColliderBuilder.threadsCollider()
.withAction(counter::increment)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAction(counter::decrement)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAwaitTerminationTimeout(1)
.asSeconds()
.build()) {
Expand All @@ -156,9 +156,9 @@ void as_minutes(TestInfo testInfo) {
try (ThreadsCollider threadsCollider =
ThreadsCollider.ThreadsColliderBuilder.threadsCollider()
.withAction(counter::increment)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAction(counter::decrement)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAwaitTerminationTimeout(1)
.asMinutes()
.build()) {
Expand All @@ -180,9 +180,9 @@ void as_hours(TestInfo testInfo) {
try (ThreadsCollider threadsCollider =
ThreadsCollider.ThreadsColliderBuilder.threadsCollider()
.withAction(counter::increment)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAction(counter::decrement)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAwaitTerminationTimeout(1)
.asHours()
.build()) {
Expand All @@ -204,9 +204,9 @@ void as_days(TestInfo testInfo) {
try (ThreadsCollider threadsCollider =
ThreadsCollider.ThreadsColliderBuilder.threadsCollider()
.withAction(counter::increment)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAction(counter::decrement)
.times(ACTION_THREADS_COUNT)
.times(Processors.HALF)
.withAwaitTerminationTimeout(1)
.asDays()
.build()) {
Expand Down
Loading

0 comments on commit a4ad048

Please sign in to comment.