forked from zoncoen/scenarigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_test.go
71 lines (62 loc) · 1.42 KB
/
plugin_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//go:build !race
// +build !race
package plugin
import (
"os"
"path/filepath"
"testing"
)
func TestOpen(t *testing.T) {
t.Run("success", func(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get current directory: %s", err)
}
tests := map[string]struct {
path string
}{
"absolute path": {
path: "../test/e2e/testdata/gen/plugins/simple.so",
},
"relative path": {
path: filepath.Join(wd, "../test/e2e/testdata/gen/plugins/simple.so"),
},
}
for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
resetCache()
// open plugin
p, err := Open(test.path)
if err != nil {
t.Fatalf("failed to open plugin: %s", err)
}
// use plugin
v, err := p.Lookup("Function")
if err != nil {
t.Fatalf("failed to lookup: %s", err)
}
f, ok := v.(func() string)
if !ok {
t.Fatalf("expect func() but got %T", v)
}
if got, expect := f(), "function"; got != expect {
t.Fatalf("expect %s but got %s", expect, got)
}
// get from cache
pp, err := Open(filepath.Clean(filepath.Join(wd, "../test/e2e/testdata/gen/plugins/simple.so")))
if err != nil {
t.Fatalf("failed to open plugin: %s", err)
}
if pp != p {
t.Fatalf("failed to get from cache: got->%p cache->%p", pp, p)
}
})
}
})
}
func resetCache() {
m.Lock()
defer m.Unlock()
cache = map[string]Plugin{}
}