Skip to content

Commit

Permalink
fix http test blocking main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Aug 4, 2020
1 parent 9405c89 commit 6471cd1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Improvements::

* Inject Maven properties as attributes in `process-asciidoc` mojo (#459)
* Make `auto-refresh` (and `http` by inheritance) only convert modified and created sources (#474)
* Make `auto-refresh` only copy modified and created resources + taking into consideration <resources> options (#478)

Bug Fixes::

Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/asciidoctor/maven/AsciidoctorHttpMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ public class AsciidoctorHttpMojo extends AsciidoctorRefreshMojo {
@Parameter(property = PREFIX + "reload-interval", defaultValue = "0")
protected int autoReloadInterval;


@Override
protected void doWork() throws MojoFailureException, MojoExecutionException {
public void execute() throws MojoExecutionException, MojoFailureException {
final AsciidoctorHttpServer server = new AsciidoctorHttpServer(getLog(), port, outputDirectory, home);
server.start();

super.doWork();
super.doWait();

doWork();
doWait();
server.stop();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void processSources(List<File> sourceFiles, ResourcesProcessor resourcesP
}

if (sourceDirectory == null) {
throw new MojoExecutionException("Required parameter 'asciidoctor.alternateSourceDir' not set.");
throw new MojoExecutionException("Required parameter 'asciidoctor.sourceDirectory' not set.");
}

Optional<File> sourceDirectoryCandidate = findSourceDirectory(sourceDirectory, project.getBasedir());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.asciidoctor.maven.test


import lombok.SneakyThrows
import org.asciidoctor.maven.AsciidoctorHttpMojo
import org.asciidoctor.maven.io.TestFilesHelper
import org.asciidoctor.maven.io.DoubleOutputStream
import org.asciidoctor.maven.io.PrefilledInputStream
import org.asciidoctor.maven.io.TestFilesHelper
import org.asciidoctor.maven.test.plexus.MockPlexusContainer
import spock.lang.Specification

Expand Down Expand Up @@ -73,6 +73,7 @@ class AsciidoctorHttpMojoTest extends Specification {
System.setOut(originalOut)
inputLatch.countDown()
System.setIn(originalIn)
awaitTermination(mojoThread)
}

def "default page"() {
Expand All @@ -88,7 +89,7 @@ class AsciidoctorHttpMojoTest extends Specification {
def originalIn = System.in

def newOut = new DoubleOutputStream(originalOut)
def newIn = new PrefilledInputStream('exit\r\n'.bytes, inputLatch)
def newIn = new PrefilledInputStream('exit\r\nexit\r\nexit\r\n'.bytes, inputLatch)

def httpPort = availablePort

Expand Down Expand Up @@ -131,6 +132,7 @@ class AsciidoctorHttpMojoTest extends Specification {
System.setOut(originalOut)
inputLatch.countDown()
System.setIn(originalIn)
awaitTermination(mojoThread)
}

private int getAvailablePort() {
Expand All @@ -139,4 +141,17 @@ class AsciidoctorHttpMojoTest extends Specification {
socket.close()
return port
}

@SneakyThrows
private void awaitTermination(Thread thread) {
int pollTime = 250;
int ticks = (30 * 1000 / pollTime);
while (thread.isAlive()) {
ticks--;
if (ticks == 0)
throw new InterruptedException("Max wait time reached");
else
Thread.sleep(pollTime);
}
}
}

0 comments on commit 6471cd1

Please sign in to comment.