Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@
<artifactId>jackson-core</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.25.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.25.1</version>
<scope>test</scope>
</dependency>
<!-- AI dependencies -->
<dependency>
<groupId>dev.langchain4j</groupId>
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/io/openliberty/tools/common/ai/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,21 @@ public static String getAbsolutePath(File p) {
}

public static LineReader getReader() {
return getReader(null);
}

public static LineReader getReader(Terminal overrideTerminal) {
if (reader == null) {
try {
terminal = TerminalBuilder.builder().system(true).build();
if (overrideTerminal == null) {
if (terminal == null) {
overrideTerminal = terminal = TerminalBuilder.builder().system(true).build();
} else {
overrideTerminal = terminal;
}
}
reader = LineReaderBuilder.builder()
.terminal(terminal)
.terminal(overrideTerminal)
.parser(new MultiLineParser())
.variable(LineReader.SECONDARY_PROMPT_PATTERN, "")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.jline.reader.LineReader;
import org.jline.terminal.TerminalBuilder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import io.openliberty.tools.common.ai.util.Utils;

public class DevUtilRunTestThreadTest extends BaseDevUtilTest {

private class RunTestThreadUtil extends DevTestUtil {
Expand Down Expand Up @@ -110,20 +115,27 @@ public void testRunHotTestThread() throws Exception {
assertEquals(1, util.counter);
}

//@Test
@Test
public void testRunHotkeyReaderThread() throws Exception {
RunTestThreadUtil util = new RunTestThreadUtil(false);
assertEquals(0, util.counter);

final ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1, true));

InputStream previousSystemIn = System.in;
LineReader previousReader = Utils.reader;
try {
// replace system input with a newline string
String enter = "\n";
System.setIn(new ByteArrayInputStream(enter.getBytes()));
// replace line reader using a terminal on piped input stream
PipedOutputStream out = new PipedOutputStream();
PipedInputStream in = new PipedInputStream(out);
Utils.reader = null;
Utils.getReader(
TerminalBuilder.builder()
.streams(in, OutputStream.nullOutputStream())
.build());

// run test on newline input
String enter = "\n";
out.write(enter.getBytes());
util.runHotkeyReaderThread(executor);

// wait for executor to pickup test job
Expand All @@ -146,7 +158,7 @@ public void testRunHotkeyReaderThread() throws Exception {
// verify that runTests() was called once
assertEquals(1, util.counter);
} finally {
System.setIn(previousSystemIn);
Utils.reader = previousReader;
}

}
Expand Down