trpc-group / trpc-go

A pluggable, high-performance RPC framework written in golang
Other
742 stars 85 forks source link

How to use or fix problem when I test unit-testing below trpc-go #185

Open wuye251 opened 3 days ago

wuye251 commented 3 days ago

Preliminary Research

Question

Additional Information

My unit-testing code

package service

import (
    "context"
    "flag"
    "testing"

    pb "code.teamsun.com/global_pb/intelligent_assistance.git/intelligent_assistance"
    "teamsun.com/intelligent_assistance/intelligent_assistance/internal/config"
    "teamsun.com/intelligent_assistance/intelligent_assistance/internal/dao"
    "teamsun.com/intelligent_assistance/intelligent_assistance/internal/model"
    "trpc.group/trpc-go/trpc-go"

    _ "trpc.group/trpc-go/trpc-filter/debuglog"
    _ "trpc.group/trpc-go/trpc-filter/recovery"
)

func init() {
    flag.String("config", "../../config/trpc_go.yaml", "")
    flag.String("test.run", "", "")
    s := trpc.NewServer()
    config.InitServerConfig()
    dao.New()
    pb.RegisterIntelligentAssistanceService(s.Service("intelligent_assistance.trpc"),
        &IntelligentAssistanceImpl{})
    go s.Serve()

    print("init server config success")
}

func TestEndCall(t *testing.T) {
    endCall(context.Background(), "1009", &model.CallInfo{
        Staff:        "staff",
        EnterpriseId: "1658884c2f644cdfbb647cfdb1689041",
    })
}

got:

init server config success/root/code/intelligent_assistance/internal/service/__debug_bin1147665774 flag redefined: test.run
panic: /root/code/intelligent_assistance/internal/service/__debug_bin1147665774 flag redefined: test.run

goroutine 1 [running]:
flag.(*FlagSet).Var(0xc0001d8310, {0x29f67f8, 0xc000134aa0}, {0x281aff5, 0x8}, {0x2858598, 0x2d})
    /usr/local/go/src/flag/flag.go:1028 +0x5aa
flag.(*FlagSet).StringVar(0xc0001d8310, 0xc000134aa0, {0x281aff5, 0x8}, {0x0, 0x0}, {0x2858598, 0x2d})
    /usr/local/go/src/flag/flag.go:879 +0x8e
flag.(*FlagSet).String(0xc0001d8310, {0x281aff5, 0x8}, {0x0, 0x0}, {0x2858598, 0x2d})
    /usr/local/go/src/flag/flag.go:892 +0x8f
flag.String({0x281aff5, 0x8}, {0x0, 0x0}, {0x2858598, 0x2d})
    /usr/local/go/src/flag/flag.go:899 +0x65
testing.Init()
    /usr/local/go/src/testing/testing.go:428 +0x28b

When I delete extra code. Like this:

func init() {
    trpc.NewServer()
    config.InitServerConfig()
    dao.New()

    print("init server config success")
}

got:

2024/07/01 16:23:10 maxprocs: Leaving GOMAXPROCS=4: CPU quota undefined
flag provided but not defined: -test.run
Usage of /root/code/intelligent_assistance/internal/service/__debug_bin862289461:
  -conf string
        server config path (default "./trpc_go.yaml")

Or how to use unit tests below trpc-go? Thank you in advance for your help

WineChord commented 3 days ago

You may refer to the project's own testcase: https://github.com/trpc-group/trpc-go/blob/1c5c0fbfa33268a4fe00828642d8e586f048eb85/trpc_test.go#L166