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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jrt;
import org.junit.After;
import org.junit.Before;
import java.time.Duration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class InvokeAsyncTest {
Supervisor server;
Acceptor acceptor;
Supervisor client;
Target target;
Test.Barrier barrier;
SimpleRequestAccessFilter filter;
@Before
public void setUp() throws ListenFailedException {
server = new Supervisor(new Transport());
client = new Supervisor(new Transport());
acceptor = server.listen(new Spec(0));
target = client.connect(new Spec("localhost", acceptor.port()));
filter = new SimpleRequestAccessFilter();
server.addMethod(new Method("concat", "ss", "s", this::rpc_concat)
.methodDesc("Concatenate 2 strings")
.paramDesc(0, "str1", "a string")
.paramDesc(1, "str2", "another string")
.returnDesc(0, "ret", "str1 followed by str2")
.requestAccessFilter(filter));
barrier = new Test.Barrier();
}
@After
public void tearDown() {
target.close();
acceptor.shutdown().join();
client.transport().shutdown().join();
server.transport().shutdown().join();
}
private void rpc_concat(Request req) {
barrier.waitFor();
req.returnValues().add(new StringValue(req.parameters()
.get(0).asString() +
req.parameters()
.get(1).asString()));
}
@org.junit.Test
public void testAsync() {
Request req = new Request("concat");
req.parameters().add(new StringValue("abc"));
req.parameters().add(new StringValue("def"));
Test.Waiter w = new Test.Waiter();
target.invokeAsync(req, Duration.ofSeconds(5), w);
assertFalse(w.isDone());
barrier.breakIt();
w.waitDone();
assertTrue(w.isDone());
assertTrue(!req.isError());
assertEquals(1, req.returnValues().size());
assertEquals("abcdef", req.returnValues().get(0).asString());
}
@org.junit.Test
public void testFilterIsInvoked() {
Request req = new Request("concat");
req.parameters().add(new StringValue("abc"));
req.parameters().add(new StringValue("def"));
assertFalse(filter.invoked);
Test.Waiter w = new Test.Waiter();
target.invokeAsync(req, Duration.ofSeconds(10), w);
assertFalse(w.isDone());
barrier.breakIt();
w.waitDone();
assertTrue(w.isDone());
assertFalse(req.isError());
assertEquals("abcdef", req.returnValues().get(0).asString());
assertTrue(filter.invoked);
}
}
|