From 616da76ccae7985df2fc773684468a1315425eae Mon Sep 17 00:00:00 2001 From: Taction Date: Tue, 19 Sep 2023 00:07:53 +0800 Subject: [PATCH] Fix wasm binding wasi http (#3077) Signed-off-by: zhangchao --- bindings/wasm/output.go | 2 +- bindings/wasm/output_test.go | 40 ++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/bindings/wasm/output.go b/bindings/wasm/output.go index 29fb6ed937..89606ef494 100644 --- a/bindings/wasm/output.go +++ b/bindings/wasm/output.go @@ -93,7 +93,7 @@ func (out *outputBinding) Init(ctx context.Context, metadata bindings.Metadata) _ = out.runtime.Close(context.Background()) return fmt.Errorf("can not instantiate wasi-http with strict sandbox") } - err = wasi_http.Instantiate(ctx, out.runtime) + err = wasi_http.MakeWasiHTTP().Instantiate(ctx, out.runtime) } if err != nil { _ = out.runtime.Close(context.Background()) diff --git a/bindings/wasm/output_test.go b/bindings/wasm/output_test.go index f065df863f..b5e765c0b1 100644 --- a/bindings/wasm/output_test.go +++ b/bindings/wasm/output_test.go @@ -17,9 +17,11 @@ import ( "bytes" "context" _ "embed" + "fmt" "io" "net/http" "net/http/httptest" + "sync" "testing" "github.com/stretchr/testify/require" @@ -268,3 +270,41 @@ func Test_InvokeHttp(t *testing.T) { }) } } + +func TestEnsureConcurrency(t *testing.T) { + l := logger.NewLogger(t.Name()) + var buf bytes.Buffer + l.SetOutput(&buf) + + s := httptest.NewServer(&handler{}) + defer s.Close() + + meta := metadata.Base{Properties: map[string]string{"url": urlHTTPFile}} + + output := NewWasmOutput(l) + defer output.(io.Closer).Close() + + ctx := context.Background() + + err := output.Init(ctx, bindings.Metadata{Base: meta}) + require.NoError(t, err) + + // Wasm is running in goroutine, use wait group to ensure all goroutines are finished + wg := sync.WaitGroup{} + // 100 is enough to trigger concurrency, and wasm should be executed run fast enough to not consuming too much time + for i := 0; i < 100; i++ { + wg.Add(1) + go func(i int) { + request := &bindings.InvokeRequest{ + Metadata: map[string]string{"args": fmt.Sprintf("%s/%d", s.URL, i)}, + Operation: ExecuteOperation, + } + expectedResp := fmt.Sprintf("Status: 200\nBody: \n/%d\n", i) + resp, err := output.Invoke(ctx, request) + require.NoError(t, err) + require.Equal(t, expectedResp, string(resp.Data)) + wg.Done() + }(i) + } + wg.Wait() +} diff --git a/go.mod b/go.mod index c5d7ecf2c8..7bba3ffc92 100644 --- a/go.mod +++ b/go.mod @@ -96,7 +96,7 @@ require ( github.com/sendgrid/sendgrid-go v3.13.0+incompatible github.com/sijms/go-ora/v2 v2.7.17 github.com/spf13/cast v1.5.1 - github.com/stealthrocket/wasi-go v0.7.6-0.20230718231108-c3d30af59057 + github.com/stealthrocket/wasi-go v0.8.1-0.20230912180546-8efbab50fb58 github.com/stretchr/testify v1.8.4 github.com/supplyon/gremcos v0.1.40 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.732 diff --git a/go.sum b/go.sum index b7cde83b92..ced4636d94 100644 --- a/go.sum +++ b/go.sum @@ -1852,8 +1852,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= github.com/spf13/viper v1.15.0 h1:js3yy885G8xwJa6iOISGFwd+qlUo5AvyXb7CiihdtiU= -github.com/stealthrocket/wasi-go v0.7.6-0.20230718231108-c3d30af59057 h1:BaBBX206PM1+qF5WQx7Ug7mbKqzizBONDMv4ST5EVNg= -github.com/stealthrocket/wasi-go v0.7.6-0.20230718231108-c3d30af59057/go.mod h1:PJ5oVs2E1ciOJnsTnav4nvTtEcJ4D1jUZAewS9pzuZg= +github.com/stealthrocket/wasi-go v0.8.1-0.20230912180546-8efbab50fb58 h1:mTC4gyv3lcJ1XpzZMAckqkvWUqeT5Bva4RAT1IoHAAA= +github.com/stealthrocket/wasi-go v0.8.1-0.20230912180546-8efbab50fb58/go.mod h1:ZAYCOqLJkc9P6fcq14TV4cf+gJ2fHthp9kCGxBViagE= github.com/stealthrocket/wazergo v0.19.1 h1:BPrITETPgSFwiytwmToO0MbUC/+RGC39JScz1JmmG6c= github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8= github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=