Skip to content
This repository was archived by the owner on May 6, 2022. It is now read-only.

Commit d46570e

Browse files
committed
fix: thread cleanup in test suites
This is a more comprehensive fix to intermittent failures at Circle, which persisted after the change to maven-surefire forking. Tests that use the full speech pipeline and its accompanying background thread now automatically clean up that background thread after the test. Inside the speech pipeline itself, the background thread has been given a recognizable name and made responsive to interruption. While this should not be a problem at runtime under normal usage, the change dramatically reduces memory usage during tests.
1 parent 839d484 commit d46570e

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

pom.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,6 @@
260260
<useSystemClassLoader>false</useSystemClassLoader>
261261
<argLine>${argLine} -Djava.library.path=./target -XX:-OmitStackTraceInFastThrow</argLine>
262262
<trimStackTrace>false</trimStackTrace>
263-
<forkCount>4</forkCount>
264-
<reuseForks>false</reuseForks>
265263
</configuration>
266264
</plugin>
267265

src/main/java/io/spokestack/spokestack/SpeechPipeline.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ public final class SpeechPipeline implements AutoCloseable {
7272
private final List<String> stageClasses;
7373
private final SpeechConfig config;
7474
private final SpeechContext context;
75+
private volatile boolean running;
76+
private volatile boolean paused;
7577
private SpeechInput input;
7678
private List<SpeechProcessor> stages;
7779
private Thread thread;
78-
private boolean running;
79-
private boolean paused;
8080
private boolean managed;
8181

8282
/**
@@ -227,7 +227,7 @@ private void attachBuffer() throws Exception {
227227
}
228228

229229
private void startThread() throws Exception {
230-
this.thread = new Thread(this::run);
230+
this.thread = new Thread(this::run, "Spokestack-speech-pipeline");
231231
this.running = true;
232232
this.thread.start();
233233
}
@@ -300,6 +300,9 @@ private void step() {
300300
} else {
301301
dispatch();
302302
}
303+
if (Thread.currentThread().isInterrupted()) {
304+
this.running = false;
305+
}
303306
}
304307

305308
private void dispatch() {

src/test/java/io/spokestack/spokestack/SpeechPipelineTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import androidx.annotation.NonNull;
99
import io.spokestack.spokestack.android.AudioRecordError;
1010
import io.spokestack.spokestack.util.EventTracer;
11+
import org.junit.After;
1112
import org.junit.Before;
1213
import org.junit.Test;
1314
import org.junit.jupiter.api.function.Executable;
@@ -37,6 +38,16 @@ public void before() {
3738
this.events.clear();
3839
}
3940

41+
@After
42+
public void after() {
43+
// shut down any stray speech pipeline background threads
44+
for (Thread thread : Thread.getAllStackTraces().keySet()) {
45+
if (thread.getName().contains("Spokestack")) {
46+
thread.interrupt();
47+
}
48+
}
49+
}
50+
4051
@Test
4152
public void testBuilder() throws Exception {
4253
// default config

src/test/java/io/spokestack/spokestack/SpokestackTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
import io.spokestack.spokestack.tts.TTSTestUtils;
1212
import io.spokestack.spokestack.util.EventTracer;
1313
import org.jetbrains.annotations.NotNull;
14+
import org.junit.After;
1415
import org.junit.Test;
1516
import org.junit.runner.RunWith;
1617
import org.powermock.core.classloader.annotations.PrepareForTest;
1718
import org.powermock.modules.junit4.PowerMockRunner;
1819

19-
import java.nio.ByteBuffer;
2020
import java.util.ArrayList;
2121
import java.util.concurrent.LinkedBlockingQueue;
2222
import java.util.concurrent.TimeUnit;
@@ -30,6 +30,16 @@
3030
@PrepareForTest({SystemClock.class})
3131
public class SpokestackTest {
3232

33+
@After
34+
public void after() {
35+
// shut down any stray speech pipeline background threads
36+
for (Thread thread : Thread.getAllStackTraces().keySet()) {
37+
if (thread.getName().contains("Spokestack")) {
38+
thread.interrupt();
39+
}
40+
}
41+
}
42+
3343
@Test
3444
public void testBuild() throws Exception {
3545
// missing all required config

0 commit comments

Comments
 (0)