diff --git a/rpc/rpc.go b/rpc/rpc.go index 304e88c..f239bd1 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -6,6 +6,7 @@ import ( "encoding/gob" "errors" "fmt" + "path" "reflect" "runtime" "strings" @@ -148,15 +149,16 @@ func (c callContext) call(args ...any) error { } pc := make([]uintptr, 1) - n := runtime.Callers(2, pc[:]) + n := runtime.Callers(3, pc[:]) if n < 1 { return errNoReceiver } frame, _ := runtime.CallersFrames(pc).Next() - prf := strings.Split(frame.Function, ".") - rname := prf[1] - funcname := prf[2] + prf := path.Base(frame.Function) + lastdot := strings.LastIndex(prf, ".") + rname := prf[:lastdot] + funcname := prf[lastdot+1:] serialized, err := encode(c.t, rname, funcname, args...) if err != nil { diff --git a/rpc/rpc_test.go b/rpc/rpc_test.go index 941db5a..dc8f521 100644 --- a/rpc/rpc_test.go +++ b/rpc/rpc_test.go @@ -1,7 +1,42 @@ package rpc -import "testing" +import ( + "context" + "testing" + + "go.mongodb.org/mongo-driver/bson/primitive" + "repositories.action2quare.com/ayo/gocommon" + "repositories.action2quare.com/ayo/gocommon/logger" +) + +type testReceiver struct { +} + +func (tr *testReceiver) TargetExists(tid primitive.ObjectID) bool { + return tid[0] >= 10 +} + +func (tr *testReceiver) TestFunc(a string, b string) { + logger.Println("TestFunc :", a, b) + + target := primitive.NewObjectID() + target[0] = 0 + if CallOrGo(tr, target, a, b) != ErrCanExecuteHere { + return + } + + logger.Println(a, b) +} func TestRpc(t *testing.T) { + var tr testReceiver + RegistReceiver(&tr) + myctx, cancel := context.WithCancel(context.Background()) + redisClient, _ := gocommon.NewRedisClient("redis://192.168.8.94:6379", 0) + Start(myctx, redisClient) + + tr.TestFunc("aaa", "bb") + <-myctx.Done() + cancel() }