Skip to content

Commit de9e162

Browse files
zZHorizonZzvietj
authored andcommitted
Refactor gRPC test to use Jash for process execution
Signed-off-by: Daniel Fiala <danfiala23@gmail.com>
1 parent 5d3b50e commit de9e162

3 files changed

Lines changed: 23 additions & 43 deletions

File tree

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@
108108
<version>1.70</version>
109109
<scope>test</scope>
110110
</dependency>
111+
<dependency>
112+
<groupId>dev.jbang</groupId>
113+
<artifactId>jash</artifactId>
114+
<version>0.0.3</version>
115+
<scope>test</scope>
116+
</dependency>
111117
<dependency>
112118
<groupId>io.grpc</groupId>
113119
<artifactId>grpc-netty-shaded</artifactId>
@@ -274,4 +280,4 @@
274280
</plugins>
275281
</build>
276282

277-
</project>
283+
</project>

vertx-grpc-it/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@
7979
<artifactId>bcpkix-jdk15on</artifactId>
8080
<scope>test</scope>
8181
</dependency>
82+
<dependency>
83+
<groupId>dev.jbang</groupId>
84+
<artifactId>jash</artifactId>
85+
<scope>test</scope>
86+
</dependency>
8287
</dependencies>
8388

8489
<build>

vertx-grpc-it/src/test/java/io/vertx/grpc/it/ReflectionTest.java

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
package io.vertx.grpc.it;
1212

13+
import dev.jbang.jash.Jash;
1314
import io.grpc.examples.helloworld.GreeterGrpcService;
1415
import io.grpc.examples.helloworld.HelloReply;
1516
import io.grpc.examples.helloworld.HelloWorldProto;
@@ -23,10 +24,11 @@
2324
import io.vertx.tests.common.GrpcTestBase;
2425
import org.junit.Test;
2526

26-
import java.io.BufferedReader;
27-
import java.io.IOException;
28-
import java.io.InputStreamReader;
29-
import java.util.concurrent.TimeUnit;
27+
import java.time.Duration;
28+
import java.time.temporal.ChronoUnit;
29+
import java.util.stream.Collectors;
30+
31+
import static dev.jbang.jash.Jash.$;
3032

3133
/**
3234
* This test requires grpcurl to be installed on the system.
@@ -118,46 +120,13 @@ public void testReflectionDescribeMessage(TestContext should) {
118120
private Future<String> executeGrpcurl(String args) {
119121
return vertx.executeBlocking(() -> {
120122
try {
121-
// Build the command - use an array to avoid shell escaping issues
122-
String[] cmdArray = {
123-
"sh", "-c", "grpcurl -plaintext localhost:" + port + " " + args
124-
};
125-
126-
System.out.println("[grpcurl] Executing command: " + String.join(" ", cmdArray));
127-
128-
// Create the process
129-
Process process = Runtime.getRuntime().exec(cmdArray);
130-
131-
// Read the output
132-
StringBuilder output = new StringBuilder();
133-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
134-
String line;
135-
while ((line = reader.readLine()) != null) {
136-
output.append(line).append("\n");
137-
}
138-
}
123+
String command = "grpcurl -plaintext localhost:" + port + " " + args;
124+
System.out.println("[grpcurl] Executing command: " + command);
139125

140-
// Wait for the process to complete
141-
if (!process.waitFor(10, TimeUnit.SECONDS)) {
142-
process.destroyForcibly();
143-
throw new RuntimeException("grpcurl command timed out: " + String.join(" ", cmdArray));
126+
try (Jash process = $(command).withTimeout(Duration.of(10, ChronoUnit.SECONDS))) {
127+
return process.stream().collect(Collectors.joining("\n"));
144128
}
145-
146-
// Check the exit code
147-
int exitCode = process.exitValue();
148-
if (exitCode != 0) {
149-
StringBuilder errorOutput = new StringBuilder();
150-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
151-
String line;
152-
while ((line = reader.readLine()) != null) {
153-
errorOutput.append(line).append("\n");
154-
}
155-
}
156-
throw new RuntimeException("grpcurl command failed with exit code " + exitCode + ": " + errorOutput);
157-
}
158-
159-
return output.toString();
160-
} catch (IOException | InterruptedException e) {
129+
} catch (Exception e) {
161130
throw new RuntimeException("Failed to execute grpcurl command", e);
162131
}
163132
});

0 commit comments

Comments
 (0)