Skip to content

Commit

Permalink
fix test case
Browse files Browse the repository at this point in the history
Signed-off-by: zhangchao <[email protected]>
  • Loading branch information
Taction committed Aug 14, 2023
1 parent 77056c7 commit 836feae
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions bindings/wasm/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,41 +185,6 @@ func Test_Invoke(t *testing.T) {
}
}

func TestEnsureConcurrency(t *testing.T) {
l := logger.NewLogger(t.Name())
var buf bytes.Buffer
l.SetOutput(&buf)

meta := metadata.Base{Properties: map[string]string{"url": urlArgsFile}}

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() {
request := &bindings.InvokeRequest{
Metadata: map[string]string{"args": fmt.Sprintf("1,%d", i)},
Operation: ExecuteOperation,
}
expectedResp := fmt.Sprintf("main\n1\n%d", i)
resp, err := output.Invoke(ctx, request)
require.NoError(t, err)
require.Equal(t, expectedResp, string(resp.Data))
wg.Done()
}()
}
wg.Wait()
}

type handler struct{}

func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -305,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 < 5; 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()
}

0 comments on commit 836feae

Please sign in to comment.