forked from roastedroot/proxy-wasm-java-host
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharedQueueTest.java
More file actions
136 lines (116 loc) · 5.43 KB
/
SharedQueueTest.java
File metadata and controls
136 lines (116 loc) · 5.43 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package io.roastedroot.proxywasm.examples;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import com.dylibso.chicory.wasm.Parser;
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.ProxyWasm;
import io.roastedroot.proxywasm.QueueName;
import io.roastedroot.proxywasm.StartException;
import io.roastedroot.proxywasm.WasmException;
import java.io.Closeable;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
/**
* Test case to verify src/test/go-examples/shared_queue example.
*/
public class SharedQueueTest {
ArrayList<Closeable> closeList = new ArrayList<>();
@AfterEach
void tearDown() {
Collections.reverse(closeList);
for (Closeable closeable : closeList) {
try {
closeable.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
<T extends Closeable> T deferClose(T x) {
closeList.add(x);
return x;
}
@Test
public void testOnPluginStart() throws StartException, WasmException {
// Load the WASM module
var receiverModule =
Parser.parse(Path.of("./src/test/go-examples/shared_queue/receiver/main.wasm"));
var sharedData = new MockSharedHandler();
var receiverVmId = "receiver";
// Create and configure the http_request_headers receiver instance
var receiverHandler1 = new MockHandler(sharedData);
var receiverHost1 =
deferClose(
ProxyWasm.builder()
.withPluginHandler(receiverHandler1)
.withProperties(Map.of("vm_id", receiverVmId))
.withPluginConfig("http_request_headers")
.build(receiverModule));
var requestHeadersQueueId =
sharedData.resolveSharedQueue(new QueueName(receiverVmId, "http_request_headers"));
receiverHandler1.assertLogsContain(
String.format(
"queue \"%s\" registered as queueID=%d by contextID=%d",
"http_request_headers", requestHeadersQueueId, receiverHost1.contextId()));
var requestHeadersQueue = sharedData.getSharedQueue(requestHeadersQueueId);
assertNotNull(requestHeadersQueue);
// Create and configure the http_response_headers receiver instance
var receiverHandler2 = new MockHandler(sharedData);
var receiverHost2 =
deferClose(
ProxyWasm.builder()
.withPluginHandler(receiverHandler2)
.withProperties(Map.of("vm_id", receiverVmId))
.withPluginConfig("http_response_headers")
.build(receiverModule));
var responseHeadersQueueId =
sharedData.resolveSharedQueue(new QueueName(receiverVmId, "http_response_headers"));
receiverHandler2.assertLogsContain(
String.format(
"queue \"%s\" registered as queueID=%d by contextID=%d",
"http_response_headers",
responseHeadersQueueId,
receiverHost2.contextId()));
var responseHeadersQueue = sharedData.getSharedQueue(responseHeadersQueueId);
assertNotNull(responseHeadersQueue);
// Load the WASM module
var senderModule =
Parser.parse(Path.of("./src/test/go-examples/shared_queue/sender/main.wasm"));
// Create and configure the sender instance
var senderHandler = new MockHandler(sharedData);
var senderVmId = "sender";
var senderHost =
deferClose(
ProxyWasm.builder()
.withPluginHandler(senderHandler)
.withProperties(Map.of("vm_id", senderVmId))
.withPluginConfig("http")
.build(senderModule));
senderHandler.assertLogsContain(
String.format("contextID=%d is configured for %s", senderHost.contextId(), "http"));
var senderContext = deferClose(senderHost.createHttpContext(senderHandler));
// queue is empty
assertEquals(0, requestHeadersQueue.data.size());
senderHandler.setHttpRequestHeaders(Map.of("hello", "world"));
Action action = senderContext.callOnRequestHeaders(false);
assertEquals(Action.CONTINUE, action);
String queuedMessage = "{\"key\": \"hello\",\"value\": \"world\"}";
senderHandler.assertLogsContain(String.format("enqueued data: %s", queuedMessage));
// queue now has 1 item
assertEquals(1, requestHeadersQueue.data.size());
// let the receiver know that the queue is ready
receiverHost1.sendOnQueueReady(requestHeadersQueueId);
receiverHandler1.assertLogsContain(
String.format(
"(contextID=%d) dequeued data from %s(queueID=%d): %s",
receiverHost1.contextId(),
"http_request_headers",
requestHeadersQueueId,
queuedMessage));
}
}