Get method string from stream (#1588)

This commit is contained in:
lyuxuan
2017-10-26 16:03:44 -07:00
committed by GitHub
parent fe0602d9d8
commit a4ff4e29c4
2 changed files with 30 additions and 0 deletions

View File

@ -663,3 +663,13 @@ func (ss *serverStream) RecvMsg(m interface{}) (err error) {
}
return nil
}
// MethodFromServerStream returns the method string for the input stream.
// The returned string is in the format of "/service/method".
func MethodFromServerStream(stream ServerStream) (string, bool) {
s, ok := transport.StreamFromContext(stream.Context())
if !ok {
return "", ok
}
return s.Method(), ok
}

View File

@ -5552,3 +5552,23 @@ func testServiceConfigMaxMsgSizeTD(t *testing.T, e env) {
t.Fatalf("%v.Send(%v) = %v, want _, error code: %s", stream, sreq, err, codes.ResourceExhausted)
}
}
func TestMethodFromServerStream(t *testing.T) {
defer leakcheck.Check(t)
const testMethod = "/package.service/method"
e := tcpClearRREnv
te := newTest(t, e)
var method string
var ok bool
te.unknownHandler = func(srv interface{}, stream grpc.ServerStream) error {
method, ok = grpc.MethodFromServerStream(stream)
return nil
}
te.startServer(nil)
defer te.tearDown()
_ = te.clientConn().Invoke(context.Background(), testMethod, nil, nil)
if !ok || method != testMethod {
t.Fatalf("Invoke with method %q, got %q, %v, want %q, true", testMethod, method, ok, testMethod)
}
}