Skip to content

Commit

Permalink
Add immediate shutdown tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TomaszTB committed Nov 8, 2024
1 parent f8b02bd commit d0e4b10
Showing 1 changed file with 119 additions and 1 deletion.
120 changes: 119 additions & 1 deletion src/test/java/us/ihmc/commons/thread/RepeatingTaskThreadTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package us.ihmc.commons.thread;

import org.junit.jupiter.api.Test;
import us.ihmc.commons.Conversions;
import us.ihmc.commons.RunnableThatThrows;
import us.ihmc.commons.time.FrequencyCalculator;
import us.ihmc.log.LogTools;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -201,7 +204,7 @@ public void testInterrupt()
RepeatingTaskThread thread = new RepeatingTaskThread(NAME)
{
@Override
protected void runTask() throws Throwable
protected void runTask()
{
ThreadTools.park(0.01);

Expand Down Expand Up @@ -249,6 +252,121 @@ protected void runTask() throws Throwable
thread.blockingKill();
}

@Test
public void testImmediateShutdown()
{
RunnableThatThrows wasteTime = () ->
{
try
{ // Sleep for half a second
Thread.sleep((long) Conversions.secondsToMilliseconds(0.5));
} catch (InterruptedException ignored) {}
};

LogTools.info("Test during free spin");
for (int millisToSleep = 0; millisToSleep < 500; millisToSleep += 100)
{
// Create a new thread
RepeatingTaskThread thread = new RepeatingTaskThread(wasteTime, NAME);

// Start free spin
thread.startRepeating();
ThreadTools.sleep(millisToSleep);

// Time the shutdown duration
long shutdownStart = System.nanoTime();
thread.kill();
thread.interrupt();
assertDoesNotThrow(() -> thread.join(500));
long shutdownComplete = System.nanoTime();

double shutdownDuration = Conversions.nanosecondsToSeconds(shutdownComplete - shutdownStart);
LogTools.info("Shutdown Duration: {}", shutdownDuration);
assertTrue(shutdownDuration < 0.01);
}

/*
* TODO: This fails because throttler keeps on throttling even when interrupted.
* It's not an issue immediately, but it'd be nice if it passed too
*/
// LogTools.info("Test during throttled looping");
// for (int millisToSleep = 0; millisToSleep < 1000; millisToSleep += 50)
// {
// // Create a new throttled thread
// RepeatingTaskThread thread = new RepeatingTaskThread(wasteTime, NAME).setFrequencyLimit(1.0);
//
// // Start throttled spin
// thread.startRepeating();
// ThreadTools.sleep(millisToSleep);
//
// // Time the shutdown duration
// long shutdownStart = System.nanoTime();
// thread.kill();
// thread.interrupt();
// assertDoesNotThrow(() -> thread.join(500));
// long shutdownComplete = System.nanoTime();
//
// double shutdownDuration = Conversions.nanosecondsToSeconds(shutdownComplete - shutdownStart);
// LogTools.info("Shutdown Duration: {}", shutdownDuration);
// assertTrue(shutdownDuration < 0.01);
// }

LogTools.info("Test during pause");
for (int millisToSleep = 0; millisToSleep < 500; millisToSleep += 100)
{
// Create a new throttled thread
RepeatingTaskThread thread = new RepeatingTaskThread(wasteTime, NAME);

// Start throttled spin
thread.start();
ThreadTools.sleep(millisToSleep);

// Time the shutdown duration
long shutdownStart = System.nanoTime();
thread.kill();
// No interrupted necessary in this case
assertDoesNotThrow(() -> thread.join(500));
long shutdownComplete = System.nanoTime();

double shutdownDuration = Conversions.nanosecondsToSeconds(shutdownComplete - shutdownStart);
LogTools.info("Shutdown Duration: {}", shutdownDuration);
assertTrue(shutdownDuration < 0.01);
}
}

@Test
public void testImmediateShutdownRace()
{
RunnableThatThrows wasteTime = () ->
{
try
{ // Sleep for 5 seconds
Thread.sleep(5000);
} catch (InterruptedException ignored) {}
};

for (int i = 0; i < 5000; ++i)
{
// Create a new thread
RepeatingTaskThread thread = new RepeatingTaskThread(wasteTime, NAME);

// Start free spin
thread.startRepeating();
LockSupport.parkNanos(5);

// Time the shutdown duration
long shutdownStart = System.nanoTime();
thread.kill();
thread.interrupt();
assertDoesNotThrow(() -> thread.join(500));
long shutdownComplete = System.nanoTime();

double shutdownDuration = Conversions.nanosecondsToSeconds(shutdownComplete - shutdownStart);
LogTools.info("Shutdown Duration: {}", shutdownDuration);
assertTrue(shutdownDuration < 0.01);
}
}

@Test
public void testOverride()
{
Expand Down

0 comments on commit d0e4b10

Please sign in to comment.