-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_test.go
115 lines (92 loc) · 2.3 KB
/
python_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package pytasks
import (
"testing"
"github.com/DataDog/go-python3"
)
func BechmarkAll(b *testing.B) {
b.Run("Py", func(b *testing.B) {
py := GetPythonSingleton()
fooModule, err := py.ImportModule("foo")
if err != nil {
panic(err)
}
fooModule2, err := py.ImportModule("foo")
if err != nil {
panic(err)
}
fooModule2.DecRef()
b.ResetTimer()
for i := 0; i < b.N; i++ {
wg1, err := py.NewTask(func() {
odds := fooModule.GetAttrString("print_odds")
defer odds.DecRef()
odds.Call(python3.PyTuple_New(0), python3.PyDict_New())
})
if err != nil {
panic(err)
}
wg2, err := py.NewTask(func() {
even := fooModule.GetAttrString("print_even")
defer even.DecRef()
even.Call(python3.PyTuple_New(0), python3.PyDict_New())
})
if err != nil {
panic(err)
}
wg1.Wait()
wg2.Wait()
}
})
// At this point we know we won't need Python anymore in this
// program, we can restore the state and lock the GIL to perform
// the final operations before exiting.
err := GetPythonSingleton().Finalize()
if err != nil {
panic(err)
}
}
func TestAll(t *testing.T) {
t.Run("TestModulePointer", func(t *testing.T) {
py := GetPythonSingleton()
fooModule1, err := py.ImportModule("foo")
if err != nil {
panic(err)
}
fooModule2, err := py.ImportModule("foo")
if err != nil {
panic(err)
}
if fooModule1 != fooModule2 {
t.Fatalf("expected pointers to be the same. got: %p != %p", fooModule1, fooModule2)
}
})
t.Run("TestSingleton", func(t *testing.T) {
py := GetPythonSingleton()
py2 := GetPythonSingleton()
if py != py2 {
t.Fatalf("not a singleton - expected %p to equal %p", py, py2)
}
})
t.Run("TestSingletonFinalize", func(t *testing.T) {
py := GetPythonSingleton()
// At this point we know we won't need Python anymore in this
// program, we can restore the state and lock the GIL to perform
// the final operations before exiting.
err := py.Finalize()
if err != nil {
panic(err)
}
_, err = py.ImportModule("foo")
if err == nil {
t.Fatalf("expected to get an error for ImportModule")
}
_, err = py.NewTask(func() {})
if err == nil {
t.Fatalf("expected to get an error for NewTask")
}
err = py.Finalize()
if err == nil {
t.Fatalf("expected to get an error for Finalize")
}
})
}