feat: support creating and calling custom functions with go plugin

This commit is contained in:
debugtalk
2021-12-28 21:14:15 +08:00
parent 49829a6fc7
commit 2da666a4e6
9 changed files with 274 additions and 68 deletions

View File

@@ -1,9 +1,66 @@
package hrp
import (
"fmt"
"os"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
fmt.Println("[TestMain] build go plugin")
cmd := exec.Command("go", "build", "-buildmode=plugin", "-o=examples/debugtalk.so", "examples/plugin/debugtalk.go")
if err := cmd.Run(); err != nil {
panic(err)
}
os.Exit(m.Run())
}
func TestLocatePlugin(t *testing.T) {
cwd, _ := os.Getwd()
_, err := locatePlugin(cwd)
if !assert.Error(t, err) {
t.Fail()
}
_, err = locatePlugin("")
if !assert.Error(t, err) {
t.Fail()
}
startPath := "examples/debugtalk.so"
_, err = locatePlugin(startPath)
if !assert.Nil(t, err) {
t.Fail()
}
startPath = "examples/demo.json"
_, err = locatePlugin(startPath)
if !assert.Nil(t, err) {
t.Fail()
}
startPath = "examples/"
_, err = locatePlugin(startPath)
if !assert.Nil(t, err) {
t.Fail()
}
startPath = "examples/plugin/debugtalk.go"
_, err = locatePlugin(startPath)
if !assert.Nil(t, err) {
t.Fail()
}
startPath = "/abc"
_, err = locatePlugin(startPath)
if !assert.Error(t, err) {
t.Fail()
}
}
func TestHttpRunner(t *testing.T) {
testcase1 := &TestCase{
Config: NewConfig("TestCase1").