|
7 | 7 | import java.io.PipedInputStream;
|
8 | 8 | import java.io.PipedOutputStream;
|
9 | 9 | import java.io.PrintStream;
|
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
10 | 13 | import java.util.concurrent.atomic.AtomicInteger;
|
11 | 14 | import org.junit.AfterClass;
|
| 15 | +import org.junit.Assert; |
12 | 16 | import org.junit.Test;
|
13 | 17 | import org.junit.runner.RunWith;
|
14 | 18 | import org.junit.runners.JUnit4;
|
15 | 19 |
|
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | + |
16 | 22 | @RunWith(JUnit4.class)
|
17 | 23 | public class WorkerTest {
|
18 | 24 |
|
@@ -89,6 +95,42 @@ public void work(String[] args) throws Exception {
|
89 | 95 | }
|
90 | 96 | }
|
91 | 97 |
|
| 98 | + @Test |
| 99 | + public void testPersistentWorkerArgsfile() throws Exception { |
| 100 | + Path tmpFile = Files.createTempFile("testPersistentWorkerArgsfiles-args", ".txt"); |
| 101 | + |
| 102 | + try (PersistentWorkerHelper helper = new PersistentWorkerHelper()) { |
| 103 | + Worker.Interface worker = |
| 104 | + new Worker.Interface() { |
| 105 | + @Override |
| 106 | + public void work(String[] args) { |
| 107 | + for (String arg : args) { |
| 108 | + System.out.println(arg); |
| 109 | + } |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + String contents = "line 1\n--flag_1\nsome arg\n"; |
| 114 | + |
| 115 | + Files.write(tmpFile, contents.getBytes(StandardCharsets.UTF_8)); |
| 116 | + |
| 117 | + WorkerProtocol.WorkRequest.newBuilder() |
| 118 | + .addArguments("@" + tmpFile) |
| 119 | + .build() |
| 120 | + .writeDelimitedTo(helper.requestOut); |
| 121 | + |
| 122 | + helper.runWorker(worker); |
| 123 | + |
| 124 | + WorkerProtocol.WorkResponse response = |
| 125 | + WorkerProtocol.WorkResponse.parseDelimitedFrom(helper.responseIn); |
| 126 | + |
| 127 | + assertEquals(response.getExitCode(), 0); |
| 128 | + assertEquals(response.getOutput(), contents); |
| 129 | + } finally { |
| 130 | + Files.deleteIfExists(tmpFile); |
| 131 | + } |
| 132 | + } |
| 133 | + |
92 | 134 | /** A helper to manage IO when testing a persistent worker. */
|
93 | 135 | private final class PersistentWorkerHelper implements AutoCloseable {
|
94 | 136 |
|
|
0 commit comments