-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestUtils.java
More file actions
74 lines (63 loc) · 2.3 KB
/
TestUtils.java
File metadata and controls
74 lines (63 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.seam.api;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public final class TestUtils {
public static final String SEAM_TEST_API_KEY = "seam_apikey1_token";
public static final Queue<Integer> PORTS = new ArrayBlockingQueue<>(100);
private static final ExecutorService STDOUT_EXECUTOR_SERVICE = Executors.newSingleThreadExecutor();
static {
PORTS.add(9090);
PORTS.add(9091);
PORTS.add(9092);
PORTS.add(9093);
PORTS.add(9094);
PORTS.add(9095);
PORTS.add(9096);
PORTS.add(9097);
PORTS.add(9098);
PORTS.add(9099);
}
private TestUtils() {}
public static FakeSeamStartedResponse startFakeSeam() {
try {
int port = PORTS.poll();
ProcessBuilder process = new ProcessBuilder("fake-seam-connect", "--seed");
process.environment().put("PORT", Integer.toString(port));
Process p = process.start();
STDOUT_EXECUTOR_SERVICE.submit(() -> print(p));
Thread.sleep(5000);
Seam seam = Seam.builder()
.url("http://localhost:" + port)
.apiKey(TestUtils.SEAM_TEST_API_KEY)
.build();
return new FakeSeamStartedResponse(seam, p);
} catch (IOException | InterruptedException e) {
throw new RuntimeException("Failed to start fake seam", e);
}
}
public static class FakeSeamStartedResponse {
public final Seam seam;
public final Process process;
public FakeSeamStartedResponse(Seam seam, Process process) {
this.seam = seam;
this.process = process;
}
}
private static void print(Process p) {
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8));
String line = "";
try {
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}