Skip to content

Commit d527338

Browse files
committed
Upgrade threadly to 6.0 and expand unit tests
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.
1 parent 07ea39e commit d527338

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed

.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
</attributes>
99
</classpathentry>
1010
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
11-
<classpathentry kind="lib" path="build/dependencies/threadly-5.42.jar"/>
11+
<classpathentry kind="lib" path="build/dependencies/threadly-6.0.jar"/>
1212
<classpathentry kind="output" path="build/classes/bin"/>
1313
</classpath>

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group = org.threadly
2-
version = 0.1
2+
version = 1.0-SNAPSHOT
33
org.gradle.parallel = false
44

5-
threadlyVersion = 5.42
5+
threadlyVersion = 6.0

src/test/java/org/threadly/ThreadlyTester.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ public enum TestLoad {
101101

102102
switch (TEST_PROFILE) {
103103
case Speedy:
104-
TEST_QTY = 2;
104+
TEST_QTY = 10;
105105
CYCLE_COUNT = 2;
106106
DELAY_TIME = 10;
107107
break;
108108
case Normal:
109-
TEST_QTY = 5;
109+
TEST_QTY = 20;
110110
CYCLE_COUNT = 10;
111111
DELAY_TIME = 10;
112112
break;

src/test/java/org/threadly/test/concurrent/TestableSchedulerTest.java

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import org.junit.Before;
1515
import org.junit.Test;
1616
import org.threadly.ThreadlyTester;
17+
import org.threadly.concurrent.CentralThreadlyPool;
1718
import org.threadly.concurrent.DoNothingRunnable;
19+
import org.threadly.concurrent.TaskPriority;
1820
import org.threadly.util.Clock;
1921
import org.threadly.util.ExceptionHandler;
2022
import org.threadly.util.StackSuppressedRuntimeException;
@@ -56,6 +58,66 @@ public void isShutdownTest() {
5658
assertFalse(scheduler.isShutdown());
5759
}
5860

61+
@Test
62+
public void getDefaultPriorityTest() {
63+
assertEquals(TaskPriority.High, scheduler.getDefaultPriority());
64+
assertEquals(TaskPriority.Low, new TestableScheduler(TaskPriority.Low, 100).getDefaultPriority());
65+
}
66+
67+
@Test
68+
public void getMaxWaitForLowPriorityTest() {
69+
assertEquals(100, new TestableScheduler(TaskPriority.Low, 100).getMaxWaitForLowPriority());
70+
}
71+
72+
@Test
73+
public void getQueuedTaskCountTest() {
74+
for (int i = 0; i < TEST_QTY; i++) {
75+
TaskPriority priority = i % 2 == 0 ? TaskPriority.High : TaskPriority.Low;
76+
77+
assertEquals(i, scheduler.getQueuedTaskCount());
78+
assertEquals(i / 2, scheduler.getQueuedTaskCount(priority));
79+
80+
if (i <= 5) {
81+
scheduler.schedule(DoNothingRunnable.instance(), 100, priority);
82+
} else {
83+
scheduler.submitScheduled(new TestCallable(), 100, priority);
84+
}
85+
}
86+
}
87+
88+
@Test
89+
public void getWaitingForExecutionTaskCountTest() {
90+
for (int i = 0; i < TEST_QTY; i++) {
91+
TaskPriority priority = i % 2 == 0 ? TaskPriority.High : TaskPriority.Low;
92+
93+
assertEquals(i, scheduler.getWaitingForExecutionTaskCount());
94+
assertEquals(i / 2, scheduler.getWaitingForExecutionTaskCount(priority));
95+
96+
if (i <= 5) {
97+
scheduler.submit(DoNothingRunnable.instance(), priority);
98+
} else {
99+
scheduler.submit(new TestCallable(), priority);
100+
}
101+
scheduler.submitScheduled(DoNothingRunnable.instance(), 10_000, priority); // task should be ignored
102+
}
103+
}
104+
105+
@Test
106+
public void getActiveTaskCountTest() {
107+
BlockingTestRunnable btr = new BlockingTestRunnable();
108+
try {
109+
scheduler.execute(btr);
110+
111+
assertEquals(0, scheduler.getActiveTaskCount());
112+
113+
CentralThreadlyPool.isolatedTaskPool().execute(() -> scheduler.tick());
114+
115+
new TestCondition(() -> scheduler.getActiveTaskCount() == 1).blockTillTrue();
116+
} finally {
117+
btr.unblock();
118+
}
119+
}
120+
59121
@Test
60122
public void lastTickTimeTest() {
61123
long now = Clock.lastKnownTimeMillis();
@@ -149,6 +211,7 @@ public void advanceThenTickTest() {
149211
public void executeTest() {
150212
List<TestRunnable> runnables = getRunnableList();
151213
Iterator<TestRunnable> it = runnables.iterator();
214+
scheduler.execute(it.next(), TaskPriority.Low); // even low priority should finish in tick
152215
while (it.hasNext()) {
153216
scheduler.execute(it.next());
154217
}
@@ -441,7 +504,7 @@ public void removeRunnableTest() {
441504

442505
assertFalse(scheduler.remove(immediateRun));
443506

444-
scheduler.scheduleWithFixedDelay(immediateRun, 0, delay);
507+
scheduler.scheduleWithFixedDelay(immediateRun, 0, delay, TaskPriority.High);
445508
assertTrue(scheduler.remove(immediateRun));
446509

447510
scheduler.scheduleWithFixedDelay(immediateRun, 0, delay);
@@ -508,7 +571,7 @@ public void handleRunStart() {
508571
}
509572
};
510573

511-
scheduler.scheduleWithFixedDelay(tr, 0, 0);
574+
scheduler.scheduleWithFixedDelay(tr, 0, 0, TaskPriority.High);
512575

513576
assertEquals(1, scheduler.tick());
514577

0 commit comments

Comments
 (0)