|
10 | 10 | */ |
11 | 11 | package io.vertx.grpc.it; |
12 | 12 |
|
| 13 | +import dev.jbang.jash.Jash; |
13 | 14 | import io.grpc.examples.helloworld.GreeterGrpcService; |
14 | 15 | import io.grpc.examples.helloworld.HelloReply; |
15 | 16 | import io.grpc.examples.helloworld.HelloWorldProto; |
|
23 | 24 | import io.vertx.tests.common.GrpcTestBase; |
24 | 25 | import org.junit.Test; |
25 | 26 |
|
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.$; |
30 | 32 |
|
31 | 33 | /** |
32 | 34 | * This test requires grpcurl to be installed on the system. |
@@ -118,46 +120,13 @@ public void testReflectionDescribeMessage(TestContext should) { |
118 | 120 | private Future<String> executeGrpcurl(String args) { |
119 | 121 | return vertx.executeBlocking(() -> { |
120 | 122 | 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); |
139 | 125 |
|
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")); |
144 | 128 | } |
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) { |
161 | 130 | throw new RuntimeException("Failed to execute grpcurl command", e); |
162 | 131 | } |
163 | 132 | }); |
|
0 commit comments