-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support to init a ReadableStream from a io.Reader (#3740)
* Support to init a ReadableStream from a io.Reader * streams.NewReadableStreamForReader test * Fix linter complains * Apply suggestions from code review * Replace goja => sobek
- Loading branch information
Showing
2 changed files
with
112 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package streams | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/grafana/sobek" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.k6.io/k6/js/modulestest" | ||
) | ||
|
||
func TestNewReadableStreamForReader(t *testing.T) { | ||
t.Parallel() | ||
|
||
// The value to be streamed. | ||
exp := "Hello, World!" | ||
|
||
// We initialize the runtime, with the ReadableStream(rs) accessible in JS. | ||
r := modulestest.NewRuntime(t) | ||
rs := NewReadableStreamFromReader(r.VU, bytes.NewReader([]byte(exp))) | ||
require.NoError(t, r.VU.Runtime().Set("rs", rs)) | ||
|
||
// Then, we run some JS code that reads from the ReadableStream(rs). | ||
var ret sobek.Value | ||
err := r.EventLoop.Start(func() (err error) { | ||
ret, err = r.VU.Runtime().RunString(`(async () => { | ||
const reader = rs.getReader(); | ||
const {value} = await reader.read(); | ||
return value; | ||
})()`) | ||
return err | ||
}) | ||
assert.NoError(t, err) | ||
|
||
// Finally, we expect the returned promise to resolve | ||
// to the expected value (the one we streamed). | ||
p, ok := ret.Export().(*sobek.Promise) | ||
require.True(t, ok) | ||
assert.Equal(t, exp, p.Result().String()) | ||
} |