Skip to content

Commit

Permalink
Upgrade threadly to 6.0 and expand unit tests
Browse files Browse the repository at this point in the history
There was some additional unit test coverage we got just from the use of these classes in the testing of the threadly project.
This adds a couple test cases to make sure we are still having great coverage in this project in isolation too.
  • Loading branch information
jentfoo committed Feb 16, 2020
1 parent 07ea39e commit d527338
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="build/dependencies/threadly-5.42.jar"/>
<classpathentry kind="lib" path="build/dependencies/threadly-6.0.jar"/>
<classpathentry kind="output" path="build/classes/bin"/>
</classpath>
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = org.threadly
version = 0.1
version = 1.0-SNAPSHOT
org.gradle.parallel = false

threadlyVersion = 5.42
threadlyVersion = 6.0
4 changes: 2 additions & 2 deletions src/test/java/org/threadly/ThreadlyTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ public enum TestLoad {

switch (TEST_PROFILE) {
case Speedy:
TEST_QTY = 2;
TEST_QTY = 10;
CYCLE_COUNT = 2;
DELAY_TIME = 10;
break;
case Normal:
TEST_QTY = 5;
TEST_QTY = 20;
CYCLE_COUNT = 10;
DELAY_TIME = 10;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import org.junit.Before;
import org.junit.Test;
import org.threadly.ThreadlyTester;
import org.threadly.concurrent.CentralThreadlyPool;
import org.threadly.concurrent.DoNothingRunnable;
import org.threadly.concurrent.TaskPriority;
import org.threadly.util.Clock;
import org.threadly.util.ExceptionHandler;
import org.threadly.util.StackSuppressedRuntimeException;
Expand Down Expand Up @@ -56,6 +58,66 @@ public void isShutdownTest() {
assertFalse(scheduler.isShutdown());
}

@Test
public void getDefaultPriorityTest() {
assertEquals(TaskPriority.High, scheduler.getDefaultPriority());
assertEquals(TaskPriority.Low, new TestableScheduler(TaskPriority.Low, 100).getDefaultPriority());
}

@Test
public void getMaxWaitForLowPriorityTest() {
assertEquals(100, new TestableScheduler(TaskPriority.Low, 100).getMaxWaitForLowPriority());
}

@Test
public void getQueuedTaskCountTest() {
for (int i = 0; i < TEST_QTY; i++) {
TaskPriority priority = i % 2 == 0 ? TaskPriority.High : TaskPriority.Low;

assertEquals(i, scheduler.getQueuedTaskCount());
assertEquals(i / 2, scheduler.getQueuedTaskCount(priority));

if (i <= 5) {
scheduler.schedule(DoNothingRunnable.instance(), 100, priority);
} else {
scheduler.submitScheduled(new TestCallable(), 100, priority);
}
}
}

@Test
public void getWaitingForExecutionTaskCountTest() {
for (int i = 0; i < TEST_QTY; i++) {
TaskPriority priority = i % 2 == 0 ? TaskPriority.High : TaskPriority.Low;

assertEquals(i, scheduler.getWaitingForExecutionTaskCount());
assertEquals(i / 2, scheduler.getWaitingForExecutionTaskCount(priority));

if (i <= 5) {
scheduler.submit(DoNothingRunnable.instance(), priority);
} else {
scheduler.submit(new TestCallable(), priority);
}
scheduler.submitScheduled(DoNothingRunnable.instance(), 10_000, priority); // task should be ignored
}
}

@Test
public void getActiveTaskCountTest() {
BlockingTestRunnable btr = new BlockingTestRunnable();
try {
scheduler.execute(btr);

assertEquals(0, scheduler.getActiveTaskCount());

CentralThreadlyPool.isolatedTaskPool().execute(() -> scheduler.tick());

new TestCondition(() -> scheduler.getActiveTaskCount() == 1).blockTillTrue();
} finally {
btr.unblock();
}
}

@Test
public void lastTickTimeTest() {
long now = Clock.lastKnownTimeMillis();
Expand Down Expand Up @@ -149,6 +211,7 @@ public void advanceThenTickTest() {
public void executeTest() {
List<TestRunnable> runnables = getRunnableList();
Iterator<TestRunnable> it = runnables.iterator();
scheduler.execute(it.next(), TaskPriority.Low); // even low priority should finish in tick
while (it.hasNext()) {
scheduler.execute(it.next());
}
Expand Down Expand Up @@ -441,7 +504,7 @@ public void removeRunnableTest() {

assertFalse(scheduler.remove(immediateRun));

scheduler.scheduleWithFixedDelay(immediateRun, 0, delay);
scheduler.scheduleWithFixedDelay(immediateRun, 0, delay, TaskPriority.High);
assertTrue(scheduler.remove(immediateRun));

scheduler.scheduleWithFixedDelay(immediateRun, 0, delay);
Expand Down Expand Up @@ -508,7 +571,7 @@ public void handleRunStart() {
}
};

scheduler.scheduleWithFixedDelay(tr, 0, 0);
scheduler.scheduleWithFixedDelay(tr, 0, 0, TaskPriority.High);

assertEquals(1, scheduler.tick());

Expand Down

0 comments on commit d527338

Please sign in to comment.