Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/main/java/io/roastedroot/proxywasm/ProxyWasm.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static io.roastedroot.proxywasm.Helpers.len;

import com.dylibso.chicory.runtime.ByteBufferMemory;
import com.dylibso.chicory.runtime.ByteArrayMemory;
import com.dylibso.chicory.runtime.HostFunction;
import com.dylibso.chicory.runtime.ImportMemory;
import com.dylibso.chicory.runtime.ImportValues;
Expand Down Expand Up @@ -327,6 +327,10 @@ public ProxyWasm build(Instance instance) throws StartException {
}

public ProxyWasm build(WasmModule module) throws StartException {
return this.build(Instance.builder(module));
}

public ProxyWasm build(Instance.Builder instanceBuilder) throws StartException {
var imports = ImportValues.builder();

imports.addMemory(Objects.requireNonNullElseGet(memory, this::defaultImportMemory));
Expand All @@ -351,7 +355,7 @@ public ProxyWasm build(WasmModule module) throws StartException {
imports.addFunction(Helpers.withModuleName(wasi.toHostFunctions(), "wasi_unstable"));

var instance =
Instance.builder(module)
instanceBuilder
.withStart(false) // we will start it manually
.withImportValues(imports.build())
.build();
Expand All @@ -363,7 +367,7 @@ ImportMemory defaultImportMemory() {
return new ImportMemory(
"env",
"memory",
new ByteBufferMemory(new MemoryLimits(2, MemoryLimits.MAX_PAGES)));
new ByteArrayMemory(new MemoryLimits(2, MemoryLimits.MAX_PAGES)));
}

WasiOptions defaultWasiOptions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.dylibso.chicory.experimental.aot.AotMachine;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.ProxyWasm;
import io.roastedroot.proxywasm.StartException;
import java.nio.file.Path;
Expand All @@ -13,17 +16,17 @@
* Java port of https://github.com/proxy-wasm/proxy-wasm-go-sdk/blob/ab4161dcf9246a828008b539a82a1556cf0f2e24/examples/dispatch_call_on_tick/main_test.go
*/
public class DispatchCallOnTickTest {
private static final WasmModule module =
Parser.parse(Path.of("./src/test/go-examples/dispatch_call_on_tick/main.wasm"));

static int tickMilliseconds = 100;

@Test
public void testOnTick() throws StartException {

var handler = new MockHandler();
var module =
Parser.parse(Path.of("./src/test/go-examples/dispatch_call_on_tick/main.wasm"));
ProxyWasm.Builder builder = ProxyWasm.builder().withPluginHandler(handler);
try (var host = builder.build(module)) {
var instanceBuilder = Instance.builder(module).withMachineFactory(AotMachine::new);
try (var host = builder.build(instanceBuilder)) {
assertEquals(tickMilliseconds, handler.getTickPeriodMilliseconds());

for (int i = 1; i <= 10; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.HttpContext;
import io.roastedroot.proxywasm.ProxyWasm;
Expand All @@ -20,6 +21,8 @@
* Java port of https://github.com/proxy-wasm/proxy-wasm-go-sdk/blob/ab4161dcf9246a828008b539a82a1556cf0f2e24/examples/http_body/main_test.go
*/
public class EchoHttpBodyTest {
private static final WasmModule module =
Parser.parse(Path.of("./src/test/go-examples/http_body/main.wasm"));

private MockHandler handler;
private ProxyWasm proxyWasm;
Expand All @@ -30,7 +33,6 @@ void setUp() throws StartException {
this.handler = new MockHandler();
ProxyWasm.Builder builder = ProxyWasm.builder();
builder.withPluginConfig("echo");
var module = Parser.parse(Path.of("./src/test/go-examples/http_body/main.wasm"));
this.proxyWasm = builder.build(module);
this.httpContext = proxyWasm.createHttpContext(handler);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.ProxyWasm;
import io.roastedroot.proxywasm.StartException;
import java.nio.file.Path;
Expand All @@ -12,14 +13,15 @@
* Java port of https://github.com/proxy-wasm/proxy-wasm-go-sdk/blob/ab4161dcf9246a828008b539a82a1556cf0f2e24/examples/foreign_call_on_tick/main_test.go
*/
public class ForeignCallOnTickTest {
private static final WasmModule module =
Parser.parse(Path.of("./src/test/go-examples/foreign_call_on_tick/main.wasm"));

static int tickMilliseconds = 1;

@Test
public void testOnTick() throws StartException {

var handler = new MockHandler();
var module = Parser.parse(Path.of("./src/test/go-examples/foreign_call_on_tick/main.wasm"));
ProxyWasm.Builder builder = ProxyWasm.builder().withPluginHandler(handler);
try (var host = builder.build(module)) {
assertEquals(tickMilliseconds, handler.getTickPeriodMilliseconds());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.junit.jupiter.api.Assertions.assertNull;

import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.ProxyWasm;
import io.roastedroot.proxywasm.StartException;
Expand All @@ -21,6 +22,8 @@
* Java port of https://github.com/proxy-wasm/proxy-wasm-go-sdk/blob/ab4161dcf9246a828008b539a82a1556cf0f2e24/examples/http_auth_random/main_test.go
*/
public class HttpAuthRandomTest {
private static WasmModule module =
Parser.parse(Path.of("./src/test/go-examples/http_auth_random/main.wasm"));

private static String clusterName = "httpbin";

Expand All @@ -31,7 +34,6 @@ public class HttpAuthRandomTest {
void setUp() throws StartException {
this.handler = new MockHandler();
ProxyWasm.Builder builder = ProxyWasm.builder();
var module = Parser.parse(Path.of("./src/test/go-examples/http_auth_random/main.wasm"));
this.host = builder.build(module);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.HttpContext;
import io.roastedroot.proxywasm.ProxyWasm;
Expand All @@ -20,6 +21,8 @@
* Java port of https://github.com/proxy-wasm/proxy-wasm-go-sdk/blob/ab4161dcf9246a828008b539a82a1556cf0f2e24/examples/http_body_chunk/main_test.go
*/
public class HttpBodyChunkTest {
private static final WasmModule module =
Parser.parse(Path.of("./src/test/go-examples/http_body_chunk/main.wasm"));

private MockHandler handler;
private ProxyWasm proxyWasm;
Expand All @@ -29,7 +32,6 @@ public class HttpBodyChunkTest {
void setUp() throws StartException {
this.handler = new MockHandler();
ProxyWasm.Builder builder = ProxyWasm.builder();
var module = Parser.parse(Path.of("./src/test/go-examples/http_body_chunk/main.wasm"));
this.proxyWasm = builder.build(module);
this.host = proxyWasm.createHttpContext(handler);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.HttpContext;
import io.roastedroot.proxywasm.ProxyWasm;
Expand All @@ -22,6 +23,8 @@
* Java port of https://github.com/proxy-wasm/proxy-wasm-go-sdk/blob/ab4161dcf9246a828008b539a82a1556cf0f2e24/examples/http_body/main_test.go
*/
public class HttpBodyTest {
private static final WasmModule module =
Parser.parse(Path.of("./src/test/go-examples/http_body/main.wasm"));

private MockHandler handler;
private ProxyWasm proxyWasm;
Expand All @@ -31,7 +34,6 @@ public class HttpBodyTest {
void setUp() throws StartException {
this.handler = new MockHandler();
ProxyWasm.Builder builder = ProxyWasm.builder();
var module = Parser.parse(Path.of("./src/test/go-examples/http_body/main.wasm"));
this.proxyWasm = builder.build(module);
this.httpContext = proxyWasm.createHttpContext(handler);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.ProxyWasm;
import io.roastedroot.proxywasm.StartException;
Expand All @@ -16,12 +17,13 @@
* Java port of https://github.com/proxy-wasm/proxy-wasm-go-sdk/blob/ab4161dcf9246a828008b539a82a1556cf0f2e24/examples/http_headers/main_test.go
*/
public class HttpHeadersTest {
private static final WasmModule module =
Parser.parse(Path.of("./src/test/go-examples/http_headers/main.wasm"));

private MockHandler handler = new MockHandler();

@Test
public void onHttpRequestHeaders() throws StartException {
var module = Parser.parse(Path.of("./src/test/go-examples/http_headers/main.wasm"));
try (var proxyWasm = ProxyWasm.builder().build(module)) {

int id = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.ProxyWasm;
import io.roastedroot.proxywasm.StartException;
Expand All @@ -14,11 +15,12 @@
* Java port of https://github.com/proxy-wasm/proxy-wasm-go-sdk/blob/ab4161dcf9246a828008b539a82a1556cf0f2e24/examples/http_routing/main_test.go
*/
public class HttpRoutingTest {
private static final WasmModule module =
Parser.parse(Path.of("./src/test/go-examples/http_routing/main.wasm"));

@Test
public void canary() throws StartException {
var handler = new MockHandler();
var module = Parser.parse(Path.of("./src/test/go-examples/http_routing/main.wasm"));
ProxyWasm.Builder builder =
ProxyWasm.builder().withPluginHandler(handler).withPluginConfig(new byte[] {2});
try (var host = builder.build(module)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.HttpContext;
import io.roastedroot.proxywasm.ProxyWasm;
Expand Down Expand Up @@ -97,6 +98,8 @@
*/
public class JsonValidationTest {
private final MockHandler handler = new MockHandler();
private static final WasmModule module =
Parser.parse(Path.of("./src/test/go-examples/json_validation/main.wasm"));

@Nested
class OnHttpRequestHeaders {
Expand All @@ -105,7 +108,6 @@ class OnHttpRequestHeaders {

@BeforeEach
void setUp() throws StartException {
var module = Parser.parse(Path.of("./src/test/go-examples/json_validation/main.wasm"));
this.host = ProxyWasm.builder().build(module);
this.context = host.createHttpContext(handler);
}
Expand Down Expand Up @@ -145,7 +147,6 @@ class OnHttpRequestBody {
@BeforeEach
void setUp() throws StartException {
var config = "{\"requiredKeys\": [\"my_key\"]}";
var module = Parser.parse(Path.of("./src/test/go-examples/json_validation/main.wasm"));
this.host = ProxyWasm.builder().withPluginConfig(config).build(module);
this.context = host.createHttpContext(handler);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.MetricType;
import io.roastedroot.proxywasm.ProxyWasm;
Expand All @@ -17,13 +18,13 @@
* Java port of https://github.com/proxy-wasm/proxy-wasm-go-sdk/blob/ab4161dcf9246a828008b539a82a1556cf0f2e24/examples/metrics/main_test.go
*/
public class MetricsTest {
private static final WasmModule module =
Parser.parse(Path.of("./src/test/go-examples/metrics/main.wasm"));

@Test
public void testMetric() throws StartException {
var handler = new MockHandler();
var module = Parser.parse(Path.of("./src/test/go-examples/metrics/main.wasm"));
ProxyWasm.Builder builder = ProxyWasm.builder().withPluginHandler(handler);

try (var host = builder.build(module)) {
try (var context = host.createHttpContext(handler)) {
// Create headers with custom header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.ProxyWasm;
import io.roastedroot.proxywasm.StartException;
Expand All @@ -15,13 +16,12 @@
* Java port of https://github.com/proxy-wasm/proxy-wasm-go-sdk/blob/master/examples/multiple_dispatches/main_test.go
*/
public class MultipleDispatchesTest {
private static final WasmModule module =
Parser.parse(Path.of("./src/test/go-examples/multiple_dispatches/main.wasm"));
private final MockHandler handler = new MockHandler();

@Test
public void testHttpContextOnHttpRequestHeaders() throws StartException {
// Load the WASM module
var module = Parser.parse(Path.of("./src/test/go-examples/multiple_dispatches/main.wasm"));

// Create and configure the ProxyWasm instance
try (var host = ProxyWasm.builder().withPluginHandler(handler).build(module)) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.MetricType;
import io.roastedroot.proxywasm.NetworkContext;
Expand All @@ -19,13 +20,14 @@
* Java port of https://github.com/proxy-wasm/proxy-wasm-go-sdk/blob/ab4161dcf9246a828008b539a82a1556cf0f2e24/examples/network/main_test.go
*/
public class NetworkTest {
private static final WasmModule module =
Parser.parse(Path.of("./src/test/go-examples/network/main.wasm"));
private final MockHandler handler = new MockHandler();
private ProxyWasm host;
private NetworkContext context;

@BeforeEach
void setUp() throws StartException {
var module = Parser.parse(Path.of("./src/test/go-examples/network/main.wasm"));
this.host = ProxyWasm.builder().withPluginHandler(handler).build(module);
this.context = host.createNetworkContext(handler);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.ProxyWasm;
import io.roastedroot.proxywasm.StartException;
import java.nio.file.Path;
Expand All @@ -14,12 +15,12 @@
* Java port of https://github.com/mosn/proxy-wasm-go-host/blob/25a9e133320ed52aee6ef87f6dcbed77f526550e/example/main_test.go
*/
public class OnRequestHeadersTest {
private static final WasmModule module =
Parser.parse(Path.of("./src/test/cc-examples/on_request_headers/http.wasm"));

@Test
public void test() throws StartException {

// This module uses the 0_1_0 ABI
var module = Parser.parse(Path.of("./src/test/cc-examples/on_request_headers/http.wasm"));
var handler = new MockHandler();
ProxyWasm.Builder builder =
ProxyWasm.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.ProxyWasm;
import io.roastedroot.proxywasm.StartException;
Expand All @@ -13,13 +14,14 @@
* Java port of https://github.com/proxy-wasm/proxy-wasm-go-sdk/blob/master/examples/postpone_requests/main_test.go
*/
public class PostponeRequestsTest {
private static final WasmModule module =
Parser.parse(Path.of("./src/test/go-examples/postpone_requests/main.wasm"));

@Test
public void testSetEffectiveContext() throws StartException {

var handler = new MockHandler();
// Load the WASM module
var module = Parser.parse(Path.of("./src/test/go-examples/postpone_requests/main.wasm"));

// Create and configure the ProxyWasm instance
try (var host = ProxyWasm.builder().withPluginHandler(handler).build(module)) {
Expand Down
Loading