Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add schema tests #64

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions wasm/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build wasm

package wasm

import (
Expand Down
82 changes: 82 additions & 0 deletions wasm/caller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright © 2024 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package wasm

import (
"testing"
"unsafe"

"github.com/conduitio/conduit-processor-sdk/pprocutils"
"github.com/matryer/is"
)

func TestHostCall(t *testing.T) {
tests := []struct {
name string
hostFunc HostFunc
buf []byte
expectedBuf []byte
expectedCmdSize uint32
expectedError error
}{
{
name: "buffer large enough on first call",
hostFunc: func(ptr unsafe.Pointer, size uint32) uint32 {
buf := unsafe.Slice((*byte)(ptr), size)
copy(buf, "command")
return uint32(len("command"))
},
buf: make([]byte, 10),
expectedBuf: []byte("command\x00\x00\x00"),
expectedCmdSize: 7,
expectedError: nil,
},
{
name: "buffer not large enough on first call, resized on second",
hostFunc: func(ptr unsafe.Pointer, size uint32) uint32 {
if size < 7 {
return 7
}
buf := unsafe.Slice((*byte)(ptr), size)
copy(buf, "command")
return uint32(len("command"))
},
buf: make([]byte, 3),
expectedBuf: []byte("command"),
expectedCmdSize: 7,
expectedError: nil,
},
{
name: "host returns error",
hostFunc: func(_ unsafe.Pointer, _ uint32) uint32 {
return pprocutils.ErrorCodeStart
},
buf: make([]byte, 10),
expectedBuf: nil,
expectedCmdSize: pprocutils.ErrorCodeStart,
expectedError: pprocutils.NewErrorFromCode(pprocutils.ErrorCodeStart),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
is := is.New(t)
buf, cmdSize, err := hostCall(tt.hostFunc, tt.buf)
is.Equal(tt.expectedError, err)
is.Equal(tt.expectedBuf, buf)
is.Equal(tt.expectedCmdSize, cmdSize)
})
}
}
Loading