Skip to content

Commit

Permalink
testing runtimes with _testdata
Browse files Browse the repository at this point in the history
  • Loading branch information
anthdm committed Jan 11, 2024
1 parent 574d562 commit b1a98bc
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ bin
config.toml
.db
.modcache
examples/*/*.wasm
examples/*/*.wasm
internal/_testdata/*.wasm
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ api: build
@./bin/api --seed

test:
@./internal/_testdata/build.sh
@go test ./internal/* -v

proto:
Expand Down
1 change: 1 addition & 0 deletions internal/_testdata/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GOOS=wasip1 GOARCH=wasm go build -o internal/_testdata/helloworld.wasm internal/_testdata/helloworld.go
16 changes: 16 additions & 0 deletions internal/_testdata/helloworld.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"net/http"

raptor "github.com/anthdm/raptor/sdk"
)

func handle(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello world!"))
}

func main() {
raptor.Handle(http.HandlerFunc(handle))
}
26 changes: 26 additions & 0 deletions internal/_testdata/helloworld.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// This should come from the official SDK.
// But there is no official SDK yet, so we keep it here.
function hexLog(s) {
for (let i = 0; i < s.length; i++) {
putstr(s.charCodeAt(i).toString(16).padStart(2, "0"))
}
putstr("0a")
}

console.log = hexLog

function respond(res, status) {
var buffer = new ArrayBuffer(8);
var view = new DataView(buffer);
view.setUint32(0, status, true);
view.setUint32(4, res.length, true);

for (let i = 0; i < res.length; i++) {
putstr(res.charCodeAt(i).toString(16).padStart(2, "0"))
}
for (let i = 0; i < view.buffer.byteLength; i++) {
putstr(view.getUint8(i).toString(16).padStart(2, "0"));
}
}

respond("Hello world!", 200)
10 changes: 6 additions & 4 deletions internal/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

func TestRuntimeInvokeJSCode(t *testing.T) {
b, err := os.ReadFile("../../examples/js/index.js")
b, err := os.ReadFile("../_testdata/helloworld.js")
require.Nil(t, err)

req := &proto.HTTPRequest{
Expand All @@ -42,14 +42,15 @@ func TestRuntimeInvokeJSCode(t *testing.T) {
scriptArgs := []string{"", "-e", string(b)}
require.Nil(t, r.Invoke(bytes.NewReader(breq), nil, scriptArgs...))

_, _, status, err := shared.ParseStdout("js", out)
_, res, status, err := shared.ParseStdout("js", out)
require.Nil(t, err)
require.Equal(t, http.StatusOK, status)
require.Equal(t, "Hello world!", string(res))
require.Nil(t, r.Close())
}

func TestRuntimeInvokeGoCode(t *testing.T) {
b, err := os.ReadFile("../../examples/go/app.wasm")
b, err := os.ReadFile("../_testdata/helloworld.wasm")
require.Nil(t, err)

req := &proto.HTTPRequest{
Expand All @@ -71,8 +72,9 @@ func TestRuntimeInvokeGoCode(t *testing.T) {
r, err := New(context.Background(), args)
require.Nil(t, err)
require.Nil(t, r.Invoke(bytes.NewReader(breq), nil))
_, _, status, err := shared.ParseStdout("go", out)
_, res, status, err := shared.ParseStdout("go", out)
require.Nil(t, err)
require.Equal(t, http.StatusOK, status)
require.Equal(t, "Hello world!", string(res))
require.Nil(t, r.Close())
}
1 change: 0 additions & 1 deletion internal/shared/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func ParseStdout(runtime string, stdout io.Reader) (logs []byte, resp []byte, st
magicStart := outLen - magicLen
status = int(binary.LittleEndian.Uint32(stdoutb[magicStart : magicStart+4]))
respLen := binary.LittleEndian.Uint32(stdoutb[magicStart+4:])
fmt.Println(status)
if int(respLen) > outLen-magicLen {
err = fmt.Errorf("response length exceeds available data")
return
Expand Down

0 comments on commit b1a98bc

Please sign in to comment.