Skip to content

Commit

Permalink
feat: add GetCallee to kitexutil
Browse files Browse the repository at this point in the history
  • Loading branch information
ppzqh committed Aug 15, 2024
1 parent 1f8ccf4 commit d4aac5a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/utils/kitexutil/kitexutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ func GetCaller(ctx context.Context) (string, bool) {
return ri.From().ServiceName(), true
}

// GetCallee is used to get the Service Name of the callee.
// Return false if failed to get the information.
func GetCallee(ctx context.Context) (string, bool) {
defer func() { recover() }()

ri := rpcinfo.GetRPCInfo(ctx)
if ri == nil {
return "", false
}
return ri.To().ServiceName(), true
}

// GetMethod is used to get the current RPC Method name.
// Return false if failed to get the information.
func GetMethod(ctx context.Context) (string, bool) {
Expand Down
27 changes: 27 additions & 0 deletions pkg/utils/kitexutil/kitexutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,33 @@ func TestGetCaller(t *testing.T) {
}
}

func TestGetCallee(t *testing.T) {
type args struct {
ctx context.Context
}
tests := []struct {
name string
args args
want string
want1 bool
}{
{name: "Success", args: args{testCtx}, want: callee, want1: true},
{name: "Failure", args: args{context.Background()}, want: "", want1: false},
{name: "Panic recovered", args: args{panicCtx}, want: "", want1: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := GetCallee(tt.args.ctx)
if got != tt.want {
t.Errorf("GetCallee() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("GetCallee() got1 = %v, want %v", got1, tt.want1)
}
})
}
}

func TestGetCallerAddr(t *testing.T) {
type args struct {
ctx context.Context
Expand Down

0 comments on commit d4aac5a

Please sign in to comment.