Fix compile errors on macOS and add some tests

This commit is contained in:
xl000
2023-05-21 20:24:25 +08:00
parent 3d6d57d199
commit af69301e52
9 changed files with 85 additions and 6 deletions

View File

@@ -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 ./...

View File

@@ -30,7 +30,7 @@
* Docker >= 20.10
* Docker Compose >= 1.29
#### 本项目未使用CGO构建理论上Windows、Linux、macOS系统都可以编译运行
#### 本项目未使用CGO构建理论上Windows、Linux系统都可以编译运行macOS下没有`unix.Gettid`的实现需要使用CGO构建
## 快速启动

View File

@@ -1,5 +1,5 @@
//go:build linux
// +build linux
//go:build linux || darwin
// +build linux darwin
package kcp

4
go.mod
View File

@@ -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
)

1
go.sum
View File

@@ -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=

View File

@@ -0,0 +1,20 @@
//go:build darwin
// +build darwin
package logger
/*
#include <pthread.h>
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)
}

View File

@@ -1,5 +1,5 @@
//go:build !linux
// +build !linux
//go:build windows
// +build windows
package logger

27
tests/cmd_test.go Normal file
View File

@@ -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)
}

19
tests/logger_test.go Normal file
View File

@@ -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)
}
}