From af69301e528f244adebaf1eca5c7a9bcc39e9968 Mon Sep 17 00:00:00 2001 From: xl000 Date: Sun, 21 May 2023 20:24:25 +0800 Subject: [PATCH] Fix compile errors on macOS and add some tests --- Makefile | 10 ++++++- README.md | 2 +- ...socket_linux.go => udp_socket_uinxlike.go} | 4 +-- go.mod | 4 +++ go.sum | 1 + pkg/logger/logger_darwin.go | 20 ++++++++++++++ pkg/logger/logger_windows.go | 4 +-- tests/cmd_test.go | 27 +++++++++++++++++++ tests/logger_test.go | 19 +++++++++++++ 9 files changed, 85 insertions(+), 6 deletions(-) rename gate/kcp/{udp_socket_linux.go => udp_socket_uinxlike.go} (99%) create mode 100644 pkg/logger/logger_darwin.go create mode 100644 tests/cmd_test.go create mode 100644 tests/logger_test.go diff --git a/Makefile b/Makefile index 88ccc7cc..c67e2672 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ clean: # 构建服务器二进制文件 .PHONY: build build: - mkdir -p bin && CGO_ENABLED=0 go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/ ./cmd/... + mkdir -p bin && go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/ ./cmd/... # 清理镜像 .PHONY: docker_clean @@ -109,3 +109,11 @@ gen_client_proto: go test -count=1 -v -run TestClientProtoGen . && \ rm -rf proto/*.pb.go && \ find proto -name '*.proto' | xargs -n 1 protoc --proto_path=proto --go_out=proto + +.PHONY: test +test: + cd tests && go test -v + +.PHONY: fmt +fmt: + go fmt ./... diff --git a/README.md b/README.md index 09d53c26..b68c2f30 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ * Docker >= 20.10 * Docker Compose >= 1.29 -#### 本项目未使用CGO构建,理论上Windows、Linux、macOS系统都可以编译运行 +#### 本项目未使用CGO构建,理论上Windows、Linux系统都可以编译运行,macOS下没有`unix.Gettid`的实现,需要使用CGO构建 ## 快速启动 diff --git a/gate/kcp/udp_socket_linux.go b/gate/kcp/udp_socket_uinxlike.go similarity index 99% rename from gate/kcp/udp_socket_linux.go rename to gate/kcp/udp_socket_uinxlike.go index 856cc6eb..ac6dbf34 100644 --- a/gate/kcp/udp_socket_linux.go +++ b/gate/kcp/udp_socket_uinxlike.go @@ -1,5 +1,5 @@ -//go:build linux -// +build linux +//go:build linux || darwin +// +build linux darwin package kcp diff --git a/go.mod b/go.mod index 0ed6b171..038473e3 100644 --- a/go.mod +++ b/go.mod @@ -55,12 +55,14 @@ require github.com/FlourishingWorld/dpdk-go v0.0.0-20230213165129-6c5bc55b1f63 require ( github.com/pkg/errors v0.9.1 + github.com/stretchr/testify v1.7.0 golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec ) require ( github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.13.0 // indirect @@ -80,6 +82,7 @@ require ( github.com/nats-io/nats-server/v2 v2.9.7 // indirect github.com/nats-io/nkeys v0.3.0 // indirect github.com/nats-io/nuid v1.0.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/ugorji/go/codec v1.1.7 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect @@ -91,4 +94,5 @@ require ( golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect golang.org/x/text v0.3.6 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index ea0d6579..02a8a30b 100644 --- a/go.sum +++ b/go.sum @@ -99,6 +99,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= diff --git a/pkg/logger/logger_darwin.go b/pkg/logger/logger_darwin.go new file mode 100644 index 00000000..e65d80da --- /dev/null +++ b/pkg/logger/logger_darwin.go @@ -0,0 +1,20 @@ +//go:build darwin +// +build darwin + +package logger + +/* +#include + +static unsigned long long thread_id() { + unsigned long long tid; + pthread_threadid_np(NULL, &tid); + return tid; +} +*/ +import "C" +import "strconv" + +func (l *Logger) getThreadId() (threadId string) { + return strconv.FormatUint(uint64(C.thread_id()), 10) +} diff --git a/pkg/logger/logger_windows.go b/pkg/logger/logger_windows.go index 546bb39b..b06b4f0b 100644 --- a/pkg/logger/logger_windows.go +++ b/pkg/logger/logger_windows.go @@ -1,5 +1,5 @@ -//go:build !linux -// +build !linux +//go:build windows +// +build windows package logger diff --git a/tests/cmd_test.go b/tests/cmd_test.go new file mode 100644 index 00000000..e18bf271 --- /dev/null +++ b/tests/cmd_test.go @@ -0,0 +1,27 @@ +package tests + +import ( + "hk4e/protocol/cmd" + "hk4e/protocol/proto" + + "reflect" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCmd(t *testing.T) { + t.Log("TestCmd") + pbm := cmd.NewCmdProtoMap() + ping_req1 := pbm.GetProtoObjByCmdId(cmd.PingReq) + t1 := reflect.TypeOf(ping_req1) + t.Log(t1, t1.Elem(), t1.Elem().Name()) + ping_req2 := new(proto.PingReq) + t2 := reflect.TypeOf(ping_req2) + t.Log(t2, t2.Elem(), t2.Elem().Name()) + assert.Equal(t, t1.Elem().Name(), t2.Elem().Name()) + assert.Equal(t, pbm.GetCmdNameByCmdId(cmd.PingReq), "PingReq") + assert.Equal(t, int(pbm.GetCmdIdByCmdName("PingReq")), cmd.PingReq) + assert.Equal(t, int(pbm.GetCmdIdByProtoObj(ping_req1)), cmd.PingReq) + assert.Equal(t, int(pbm.GetCmdIdByProtoObj(ping_req2)), cmd.PingReq) +} diff --git a/tests/logger_test.go b/tests/logger_test.go new file mode 100644 index 00000000..cc49d7a5 --- /dev/null +++ b/tests/logger_test.go @@ -0,0 +1,19 @@ +package tests + +import ( + "hk4e/common/config" + "hk4e/pkg/logger" + + "testing" +) + +func TestLogger(t *testing.T) { + config.CONF = &config.Config{Logger: config.Logger{Level: "DEBUG", Mode: "CONSOLE", Track: true}} + + logger.InitLogger("logger_test") + defer logger.CloseLogger() + logger.Warn("logger test ...") + for i := 0; i < 100; i++ { + go func(x int) { logger.Info("%d", x) }(i) + } +}