Skip to content

Commit f4ea274

Browse files
TactionItalyPaleAle
authored andcommitted
Fix wasm binding wasi http (#3077)
Signed-off-by: zhangchao <[email protected]>
1 parent e83b79e commit f4ea274

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

bindings/wasm/output.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (out *outputBinding) Init(ctx context.Context, metadata bindings.Metadata)
9393
_ = out.runtime.Close(context.Background())
9494
return fmt.Errorf("can not instantiate wasi-http with strict sandbox")
9595
}
96-
err = wasi_http.Instantiate(ctx, out.runtime)
96+
err = wasi_http.MakeWasiHTTP().Instantiate(ctx, out.runtime)
9797
}
9898
if err != nil {
9999
_ = out.runtime.Close(context.Background())

bindings/wasm/output_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import (
1717
"bytes"
1818
"context"
1919
_ "embed"
20+
"fmt"
2021
"io"
2122
"net/http"
2223
"net/http/httptest"
24+
"sync"
2325
"testing"
2426

2527
"github.com/stretchr/testify/require"
@@ -268,3 +270,41 @@ func Test_InvokeHttp(t *testing.T) {
268270
})
269271
}
270272
}
273+
274+
func TestEnsureConcurrency(t *testing.T) {
275+
l := logger.NewLogger(t.Name())
276+
var buf bytes.Buffer
277+
l.SetOutput(&buf)
278+
279+
s := httptest.NewServer(&handler{})
280+
defer s.Close()
281+
282+
meta := metadata.Base{Properties: map[string]string{"url": urlHTTPFile}}
283+
284+
output := NewWasmOutput(l)
285+
defer output.(io.Closer).Close()
286+
287+
ctx := context.Background()
288+
289+
err := output.Init(ctx, bindings.Metadata{Base: meta})
290+
require.NoError(t, err)
291+
292+
// Wasm is running in goroutine, use wait group to ensure all goroutines are finished
293+
wg := sync.WaitGroup{}
294+
// 100 is enough to trigger concurrency, and wasm should be executed run fast enough to not consuming too much time
295+
for i := 0; i < 100; i++ {
296+
wg.Add(1)
297+
go func(i int) {
298+
request := &bindings.InvokeRequest{
299+
Metadata: map[string]string{"args": fmt.Sprintf("%s/%d", s.URL, i)},
300+
Operation: ExecuteOperation,
301+
}
302+
expectedResp := fmt.Sprintf("Status: 200\nBody: \n/%d\n", i)
303+
resp, err := output.Invoke(ctx, request)
304+
require.NoError(t, err)
305+
require.Equal(t, expectedResp, string(resp.Data))
306+
wg.Done()
307+
}(i)
308+
}
309+
wg.Wait()
310+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ require (
9696
github.com/sendgrid/sendgrid-go v3.13.0+incompatible
9797
github.com/sijms/go-ora/v2 v2.7.17
9898
github.com/spf13/cast v1.5.1
99-
github.com/stealthrocket/wasi-go v0.7.6-0.20230718231108-c3d30af59057
99+
github.com/stealthrocket/wasi-go v0.8.1-0.20230912180546-8efbab50fb58
100100
github.com/stretchr/testify v1.8.4
101101
github.com/supplyon/gremcos v0.1.40
102102
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.732

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,8 +1852,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
18521852
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
18531853
github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
18541854
github.com/spf13/viper v1.15.0 h1:js3yy885G8xwJa6iOISGFwd+qlUo5AvyXb7CiihdtiU=
1855-
github.com/stealthrocket/wasi-go v0.7.6-0.20230718231108-c3d30af59057 h1:BaBBX206PM1+qF5WQx7Ug7mbKqzizBONDMv4ST5EVNg=
1856-
github.com/stealthrocket/wasi-go v0.7.6-0.20230718231108-c3d30af59057/go.mod h1:PJ5oVs2E1ciOJnsTnav4nvTtEcJ4D1jUZAewS9pzuZg=
1855+
github.com/stealthrocket/wasi-go v0.8.1-0.20230912180546-8efbab50fb58 h1:mTC4gyv3lcJ1XpzZMAckqkvWUqeT5Bva4RAT1IoHAAA=
1856+
github.com/stealthrocket/wasi-go v0.8.1-0.20230912180546-8efbab50fb58/go.mod h1:ZAYCOqLJkc9P6fcq14TV4cf+gJ2fHthp9kCGxBViagE=
18571857
github.com/stealthrocket/wazergo v0.19.1 h1:BPrITETPgSFwiytwmToO0MbUC/+RGC39JScz1JmmG6c=
18581858
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
18591859
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=

0 commit comments

Comments
 (0)