This repository was archived by the owner on Feb 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDNodeTest.java
More file actions
107 lines (89 loc) · 3.01 KB
/
DNodeTest.java
File metadata and controls
107 lines (89 loc) · 3.01 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package dnode;
import dnode.netty.NettyServer;
import junit.framework.AssertionFailedError;
import org.junit.After;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import static org.junit.Assert.assertEquals;
public class DNodeTest {
private DNode dNode;
private final Server server = new NettyServer(6060);
public class Mooer {
private final int moo;
public DNode dNode;
public Mooer(int moo) {
this.moo = moo;
}
public void moo(Callback cb) throws IOException {
cb.call(moo);
dNode.closeAllConnections();
server.shutdown();
}
public void boo(Callback cb) throws IOException {
cb.call(moo * 10);
dNode.closeAllConnections();
server.shutdown();
}
}
@After
public void shutdownServer() throws IOException {
server.shutdown();
}
@Test
public void shouldTalk() throws IOException, InterruptedException {
createDnode(100);
dNode.listen(server);
assertEquals("100\n", runClient("moo"));
}
@Test
public void shouldUseDataInInstance() throws IOException, InterruptedException {
createDnode(200);
dNode.listen(server);
assertEquals("200\n", runClient("moo"));
}
@Test
public void shouldCallRightMethod() throws IOException, InterruptedException {
createDnode(300);
dNode.listen(server);
assertEquals("3000\n", runClient("boo"));
}
public static interface SomeClient {
void hello();
}
@Test
public void shouldBeAbleToCallClient() {
Mooer mooer = new Mooer(345);
dNode = new DNode<SomeClient>(mooer, new ClientHandler<SomeClient>() {
@Override
public void onConnect(SomeClient client) {
System.out.println("client = " + client);
client.hello();
}
});
}
private void createDnode(int moo) {
Mooer instance = new Mooer(moo);
dNode = new DNode(instance);
instance.dNode = dNode;
}
private String runClient(String method) throws IOException, InterruptedException {
String node = System.getProperty("node", "/usr/local/bin/node");
String clientScript = System.getProperty("client", "client.js");
ProcessBuilder pb = new ProcessBuilder(node, clientScript, method);
pb.redirectErrorStream(true);
Process client = pb.start();
Reader clientStdOut = new InputStreamReader(client.getInputStream(), "UTF-8");
StringBuilder result = new StringBuilder();
int c;
while ((c = clientStdOut.read()) != -1) {
result.append((char) c);
}
int exit = client.waitFor();
if (exit != 0)
throw new AssertionFailedError("Exit value from external process was " + exit +
" (with stdout/stderr: " + result + ")");
return result.toString();
}
}