-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: start worker process when starting
WebApp
in embedded mode
- Loading branch information
Showing
6 changed files
with
183 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
datashare-app/src/test/java/org/icij/datashare/WebAppTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.icij.datashare; | ||
|
||
import static org.fest.assertions.Assertions.assertThat; | ||
import static org.icij.datashare.WebApp.startNlpWorkers; | ||
import static org.icij.datashare.json.JsonObjectMapper.MAPPER; | ||
import static org.icij.datashare.utils.ProcessHandler.dumpPid; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.Test; | ||
|
||
public class WebAppTest { | ||
private static final String extDir = Objects.requireNonNull( | ||
WebAppTest.class.getClassLoader().getResource("extensions")).getPath(); | ||
private static final Map<String, Object> properties = Map.of( | ||
"mode", "EMBEDDED", "nlpParallelism", "6", "extensionsDir", extDir | ||
); | ||
|
||
@Test | ||
public void test_nlp_workers_process() throws IOException, InterruptedException { | ||
// Given | ||
String extensionPattern = "^datashare-spacy-worker-[\\d\\.]+$"; | ||
ExtensionService extensionService = new ExtensionService(Path.of(extDir)); | ||
PropertiesProvider propertiesProvider = new PropertiesProvider(properties); | ||
ExecutableExtensionHelper extensionHelper = | ||
new ExecutableExtensionHelper(propertiesProvider, extensionService, extensionPattern); | ||
// When | ||
Process p = startNlpWorkers(extensionHelper, 6, false); | ||
// Then | ||
int timeout = 2; | ||
TimeUnit unit = TimeUnit.SECONDS; | ||
if (!p.waitFor(timeout, unit)) { | ||
throw new AssertionError( | ||
"failed to get process output in less than " + timeout + unit.name().toLowerCase()); | ||
} | ||
HashMap<String, Object> output = (HashMap<String, Object>) MAPPER.readValue( | ||
p.getInputStream().readAllBytes(), Map.class); | ||
assertThat(output.get("n_workers")).isEqualTo(6); | ||
assertThat((String) output.get("config_file")).contains("datashare-spacy-worker-config-"); | ||
} | ||
|
||
@Test(timeout = 20000) | ||
public void test_nlp_workers_process_should_throw_when_worker_pool_is_running() throws IOException { | ||
// Given | ||
String extensionPattern = "^datashare-spacy-worker-[\\d\\.]+$"; | ||
ExtensionService extensionService = new ExtensionService(Path.of(extDir)); | ||
PropertiesProvider propertiesProvider = new PropertiesProvider(properties); | ||
ExecutableExtensionHelper extensionHelper = | ||
new ExecutableExtensionHelper(propertiesProvider, extensionService, extensionPattern); | ||
Process p = null; | ||
try { | ||
p = new ProcessBuilder("sleep", "100000").start(); | ||
Path pidPath = Files.createTempFile("datashare-spacy-worker-1.9.0", ".pid"); | ||
dumpPid(pidPath.toFile(), p.pid()); | ||
// When/Then | ||
assertThat( | ||
assertThrows(RuntimeException.class, () -> startNlpWorkers(extensionHelper, 1, false)).getMessage()) | ||
.matches("found phantom worker running in process.*"); | ||
} finally { | ||
if (p != null) { | ||
p.destroyForcibly(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
datashare-app/src/test/resources/extensions/datashare-spacy-worker-1.9.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env bash | ||
config_file=$2 | ||
n_workers=$4 | ||
echo { \"n_workers\": $n_workers, \"config_file\": \"$config_file\" } |