Skip to content

Commit a610c45

Browse files
committed
test #194
1 parent ceb96b7 commit a610c45

File tree

8 files changed

+266
-6
lines changed

8 files changed

+266
-6
lines changed

.github/workflows/custom.yml

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Custom Test
2+
3+
on:
4+
push:
5+
branches: [ "test-issue-194" ]
6+
workflow_dispatch:
7+
inputs:
8+
dummy:
9+
default: nothing
10+
11+
# 'gin-gonic/gin' ok
12+
# 'fatedier/frp' cannot pass even with go? need to verify
13+
#
14+
# NOTE: use serially job to avoid 'Job Cancelled' which could
15+
# possible be due to memory exhaust
16+
jobs:
17+
18+
test-with-xgo:
19+
runs-on: macos-latest
20+
timeout-minutes: 360
21+
steps:
22+
- uses: actions/checkout@v4
23+
with:
24+
path: xgo
25+
26+
- name: Prepare Utility Scripts
27+
run: |
28+
cat > util.sh <<'EOF'
29+
function record_exit_code {
30+
local exit_code=$?
31+
echo "exit code: $exit_code"
32+
echo "$exit_code" > test-exit-code.txt
33+
}
34+
trap 'record_exit_code' EXIT
35+
EOF
36+
37+
- name: Set up Go
38+
uses: actions/setup-go@v4
39+
with:
40+
go-version: '1.22'
41+
42+
- name: Install Xgo Locally
43+
run: |
44+
cd xgo
45+
go install ./cmd/xgo
46+
xgo exec --reset-instrument --log-debug go version
47+
xgo revision
48+
49+
- name: Dep check
50+
run: |
51+
go install github.com/xhd2015/go-inspect/cmd/depcheck@latest
52+
cd xgo/runtime/test/issue_194
53+
depcheck ./
54+
55+
- name: Test cusotm
56+
run: |
57+
source util.sh
58+
cd xgo/runtime/test/issue_194
59+
go test -v -run TestUserHandler_CreateUser || true
60+
xgo test -v -run TestUserHandler_CreateUser || true

cmd/xgo/runtime_gen/core/version.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
)
88

99
const VERSION = "1.0.39"
10-
const REVISION = "b2c3ae1e20700a8710d719b492b6db9e60fc4800+1"
11-
const NUMBER = 263
10+
const REVISION = "ceb96b726f89731a8f2bda8239af7c1810e44294+1"
11+
const NUMBER = 264
1212

1313
// these fields will be filled by compiler
1414
const XGO_VERSION = ""

cmd/xgo/version.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package main
33
import "fmt"
44

55
const VERSION = "1.0.39"
6-
const REVISION = "b2c3ae1e20700a8710d719b492b6db9e60fc4800+1"
7-
const NUMBER = 263
6+
const REVISION = "ceb96b726f89731a8f2bda8239af7c1810e44294+1"
7+
const NUMBER = 264
88

99
func getRevision() string {
1010
revSuffix := ""

runtime/core/version.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
)
88

99
const VERSION = "1.0.39"
10-
const REVISION = "b2c3ae1e20700a8710d719b492b6db9e60fc4800+1"
11-
const NUMBER = 263
10+
const REVISION = "ceb96b726f89731a8f2bda8239af7c1810e44294+1"
11+
const NUMBER = 264
1212

1313
// these fields will be filled by compiler
1414
const XGO_VERSION = ""

runtime/test/issue_194/go.mod

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module test
2+
3+
go 1.21.7
4+
5+
require (
6+
github.com/bytedance/sonic v1.11.8
7+
github.com/julienschmidt/httprouter v1.3.0
8+
github.com/stretchr/testify v1.9.0
9+
github.com/xhd2015/xgo/runtime v1.0.39
10+
gorm.io/driver/mysql v1.5.6
11+
gorm.io/gorm v1.25.10
12+
)
13+
14+
require (
15+
github.com/bytedance/sonic/loader v0.1.1 // indirect
16+
github.com/cloudwego/base64x v0.1.4 // indirect
17+
github.com/cloudwego/iasm v0.2.0 // indirect
18+
github.com/davecgh/go-spew v1.1.1 // indirect
19+
github.com/go-sql-driver/mysql v1.7.0 // indirect
20+
github.com/jinzhu/inflection v1.0.0 // indirect
21+
github.com/jinzhu/now v1.1.5 // indirect
22+
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
23+
github.com/pmezard/go-difflib v1.0.0 // indirect
24+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
25+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
26+
gopkg.in/yaml.v3 v3.0.1 // indirect
27+
)

runtime/test/issue_194/go.sum

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
github.com/bytedance/sonic v1.11.8 h1:Zw/j1KfiS+OYTi9lyB3bb0CFxPJVkM17k1wyDG32LRA=
2+
github.com/bytedance/sonic v1.11.8/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
3+
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
4+
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
5+
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
6+
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
7+
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
8+
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
9+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
11+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12+
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
13+
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
14+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
15+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
16+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
17+
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
18+
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
19+
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
20+
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
21+
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
22+
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
23+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
24+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
25+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
26+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
27+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
28+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
29+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
30+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
31+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
32+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
33+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
34+
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
35+
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
36+
github.com/xhd2015/xgo/runtime v1.0.39 h1:Uudjr5VGjlvD4gDIOznRR+pteDVNlr6qOHHcZMzYZbA=
37+
github.com/xhd2015/xgo/runtime v1.0.39/go.mod h1:9GBQ2SzJCzpD3T+HRN+2C0TUOGv7qIz4s0mad1xJ8Jo=
38+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
39+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
40+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
41+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
42+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
43+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
44+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
45+
gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8=
46+
gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
47+
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
48+
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
49+
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
50+
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
51+
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

runtime/test/issue_194/main.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/bytedance/sonic"
6+
"io"
7+
"net/http"
8+
"strconv"
9+
10+
"github.com/julienschmidt/httprouter"
11+
"gorm.io/driver/mysql"
12+
"gorm.io/gorm"
13+
)
14+
15+
type User struct {
16+
ID int
17+
Name string
18+
}
19+
20+
func NewMySQLDB(host, port, user, pass, dbname string) (*gorm.DB, error) {
21+
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
22+
user, pass, host, port, dbname)
23+
return gorm.Open(mysql.Open(dsn), &gorm.Config{})
24+
}
25+
26+
func NewUserHandler(store *gorm.DB) *UserHandler {
27+
return &UserHandler{store: store}
28+
}
29+
30+
type UserHandler struct {
31+
store *gorm.DB
32+
}
33+
34+
func (h *UserHandler) CreateUser(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
35+
w.Header().Set("Content-Type", "application/json")
36+
37+
body, err := io.ReadAll(r.Body)
38+
if err != nil {
39+
w.WriteHeader(http.StatusBadRequest)
40+
_, _ = fmt.Fprintf(w, `{"msg":"%s"}`, err.Error())
41+
return
42+
}
43+
defer func() { _ = r.Body.Close() }()
44+
45+
u := User{}
46+
if err := sonic.Unmarshal(body, &u); err != nil {
47+
w.WriteHeader(http.StatusBadRequest)
48+
_, _ = fmt.Fprintf(w, `{"msg":"%s"}`, err.Error())
49+
return
50+
}
51+
52+
if err := h.store.Create(&u).Error; err != nil {
53+
w.WriteHeader(http.StatusInternalServerError)
54+
_, _ = fmt.Fprintf(w, `{"msg":"%s"}`, err.Error())
55+
return
56+
}
57+
w.WriteHeader(http.StatusCreated)
58+
}
59+
60+
func (h *UserHandler) GetUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
61+
id := ps[0].Value
62+
uid, _ := strconv.Atoi(id)
63+
64+
w.Header().Set("Content-Type", "application/json")
65+
var u User
66+
if err := h.store.First(&u, uid).Error; err != nil {
67+
w.WriteHeader(http.StatusInternalServerError)
68+
_, _ = fmt.Fprintf(w, `{"msg":"%s"}`, err.Error())
69+
return
70+
}
71+
_, _ = fmt.Fprintf(w, `{"id":%d,"name":"%s"}`, u.ID, u.Name)
72+
}
73+
74+
func setupRouter(handler *UserHandler) *httprouter.Router {
75+
router := httprouter.New()
76+
router.POST("/users", handler.CreateUser)
77+
router.GET("/users/:id", handler.GetUser)
78+
return router
79+
}
80+
81+
func main() {
82+
mysqlDB, _ := NewMySQLDB("localhost", "3306", "user", "password", "test")
83+
handler := NewUserHandler(mysqlDB)
84+
router := setupRouter(handler)
85+
_ = http.ListenAndServe(":8000", router)
86+
}

runtime/test/issue_194/main_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"net/http/httptest"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/xhd2015/xgo/runtime/mock"
10+
"gorm.io/gorm"
11+
)
12+
13+
func TestUserHandler_CreateUser(t *testing.T) {
14+
mysqlDB := &gorm.DB{}
15+
handler := NewUserHandler(mysqlDB)
16+
router := setupRouter(handler)
17+
18+
// 为 mysqlDB 打上猴子补丁,替换其 Create 方法
19+
mock.Patch(mysqlDB.Create, func(value interface{}) (tx *gorm.DB) {
20+
expected := &User{
21+
Name: "user1",
22+
}
23+
actual := value.(*User)
24+
assert.Equal(t, expected, actual)
25+
return mysqlDB
26+
})
27+
28+
w := httptest.NewRecorder()
29+
req := httptest.NewRequest("POST", "/users", strings.NewReader(`{"name": "user1"}`))
30+
router.ServeHTTP(w, req)
31+
32+
// 断言成功响应
33+
assert.Equal(t, 201, w.Code)
34+
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
35+
assert.Equal(t, "", w.Body.String())
36+
}

0 commit comments

Comments
 (0)