diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index 289af5e2..7cf510e0 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -31,7 +31,10 @@ func Test_dispatch_call_on_tick(t *testing.T) { defer kill() var count int = 1 require.Eventually(t, func() bool { - if strings.Contains(stdErr.String(), fmt.Sprintf("called! %d", count)) { + if checkMessage(stdErr.String(), []string{ + fmt.Sprintf("called %d for contextID=1", count), + fmt.Sprintf("called %d for contextID=2", count), + }, nil) { count++ } return count == 6 diff --git a/examples/dispatch_call_on_tick/envoy.yaml b/examples/dispatch_call_on_tick/envoy.yaml index 6178aa3d..eb1505da 100644 --- a/examples/dispatch_call_on_tick/envoy.yaml +++ b/examples/dispatch_call_on_tick/envoy.yaml @@ -30,6 +30,23 @@ static_resources: type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm value: config: + configuration: + "@type": type.googleapis.com/google.protobuf.StringValue + value: "plugin1" + vm_config: + runtime: "envoy.wasm.runtime.v8" + code: + local: + filename: "./examples/dispatch_call_on_tick/main.go.wasm" + - name: envoy.filters.http.wasm + typed_config: + "@type": type.googleapis.com/udpa.type.v1.TypedStruct + type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm + value: + config: + configuration: + "@type": type.googleapis.com/google.protobuf.StringValue + value: "plugin2" vm_config: runtime: "envoy.wasm.runtime.v8" code: diff --git a/examples/dispatch_call_on_tick/main.go b/examples/dispatch_call_on_tick/main.go index d14c6d7c..483ea36b 100644 --- a/examples/dispatch_call_on_tick/main.go +++ b/examples/dispatch_call_on_tick/main.go @@ -19,7 +19,7 @@ import ( "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" ) -const tickMilliseconds uint32 = 1 +const tickMilliseconds uint32 = 100 func main() { proxywasm.SetVMContext(&vmContext{}) @@ -41,6 +41,8 @@ type pluginContext struct { // so that we don't need to reimplement all the methods. types.DefaultPluginContext contextID uint32 + callBack func(numHeaders, bodySize, numTrailers int) + cnt int } // Override types.DefaultPluginContext. @@ -50,6 +52,10 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu return types.OnPluginStartStatusFailed } proxywasm.LogInfof("set tick period milliseconds: %d", tickMilliseconds) + ctx.callBack = func(numHeaders, bodySize, numTrailers int) { + ctx.cnt++ + proxywasm.LogInfof("called %d for contextID=%d", ctx.cnt, ctx.contextID) + } return types.OnPluginStartStatusOK } @@ -58,15 +64,7 @@ func (ctx *pluginContext) OnTick() { hs := [][2]string{ {":method", "GET"}, {":authority", "some_authority"}, {":path", "/path/to/service"}, {"accept", "*/*"}, } - if _, err := proxywasm.DispatchHttpCall("web_service", hs, nil, nil, - 5000, callback); err != nil { + if _, err := proxywasm.DispatchHttpCall("web_service", hs, nil, nil, 5000, ctx.callBack); err != nil { proxywasm.LogCriticalf("dispatch httpcall failed: %v", err) } } - -var cnt int - -func callback(numHeaders, bodySize, numTrailers int) { - cnt++ - proxywasm.LogInfof("called! %d", cnt) -} diff --git a/examples/dispatch_call_on_tick/main_test.go b/examples/dispatch_call_on_tick/main_test.go index ce0db889..e1935baf 100644 --- a/examples/dispatch_call_on_tick/main_test.go +++ b/examples/dispatch_call_on_tick/main_test.go @@ -35,7 +35,7 @@ func TestPluginContext_OnTick(t *testing.T) { host.CallOnHttpCallResponse(attrs[0].CalloutID, nil, nil, nil) // Check Envoy logs. logs := host.GetInfoLogs() - require.Contains(t, logs, fmt.Sprintf("called! %d", i)) + require.Contains(t, logs, fmt.Sprintf("called %d for contextID=%d", i, proxytest.PluginContextID)) } } diff --git a/proxywasm/internal/abi_callback_timers.go b/proxywasm/internal/abi_callback_timers.go index bbe47358..454dcce2 100644 --- a/proxywasm/internal/abi_callback_timers.go +++ b/proxywasm/internal/abi_callback_timers.go @@ -20,5 +20,6 @@ func proxyOnTick(pluginContextID uint32) { if !ok { panic("invalid root_context_id") } + currentState.setActiveContextID(pluginContextID) ctx.context.OnTick() }