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

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