Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpc: update clientStreamAttempt context with transport context #7096

Merged
merged 4 commits into from May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions stream.go
Expand Up @@ -516,6 +516,7 @@ func (a *csAttempt) newStream() error {
return toRPCErr(nse.Err)
}
a.s = s
a.ctx = s.Context()
a.p = &parser{r: s, recvBufferPool: a.cs.cc.dopts.recvBufferPool}
return nil
}
Expand Down
122 changes: 122 additions & 0 deletions test/stats_test.go
@@ -0,0 +1,122 @@
/*
*
* Copyright 2024 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package test

import (
"context"
"net"
"sync"
"testing"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/interop"
testgrpc "google.golang.org/grpc/interop/grpc_testing"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/stats"
)

// TestPeerForClientStatsHandler tests the scenario where stats handler
// (having peer as part of its struct) has peer enriched as part of
// stream context.
zasweq marked this conversation as resolved.
Show resolved Hide resolved
func (s) TestPeerForClientStatsHandler(t *testing.T) {
spy := &handlerSpy{}
zasweq marked this conversation as resolved.
Show resolved Hide resolved

// Start server.
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal(err)
}
grpcServer := grpc.NewServer()
zasweq marked this conversation as resolved.
Show resolved Hide resolved
testgrpc.RegisterTestServiceServer(grpcServer, interop.NewTestServer())
errCh := make(chan error)
go func() {
errCh <- grpcServer.Serve(l)
}()
t.Cleanup(func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I don't think we want cleanup, maybe switch to a defer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you switch one below too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ping on this.

grpcServer.Stop()
if err := <-errCh; err != nil {
t.Error(err)
}
})

// Create client with stats handler and do some calls.
conn, err := grpc.NewClient(
zasweq marked this conversation as resolved.
Show resolved Hide resolved
l.Addr().String(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithStatsHandler(spy))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := conn.Close(); err != nil {
t.Error(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional: This t.Error is not really in the essence of the test. I think maybe we can get rid of it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
})
arvindbr8 marked this conversation as resolved.
Show resolved Hide resolved

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
client := testgrpc.NewTestServiceClient(conn)
interop.DoEmptyUnaryCall(ctx, client)
interop.DoLargeUnaryCall(ctx, client)
interop.DoClientStreaming(ctx, client)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be useful to have a subtest for each RPC type (unary, bidi, etc)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesnt seem to be resolved?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zach suggested here to have single RPC type.

interop.DoServerStreaming(ctx, client)
interop.DoPingPong(ctx, client)
zasweq marked this conversation as resolved.
Show resolved Hide resolved

// Assert if peer is populated for each stats type.
zasweq marked this conversation as resolved.
Show resolved Hide resolved
for _, callbackArgs := range spy.Args {
if callbackArgs.Peer == nil {
switch callbackArgs.RPCStats.(type) {
case *stats.Begin:
continue
case *stats.PickerUpdated:
continue
default:
}
t.Errorf("peer not populated for: %T", callbackArgs.RPCStats)
}
}
}
zasweq marked this conversation as resolved.
Show resolved Hide resolved

type peerStats struct {
RPCStats stats.RPCStats
zasweq marked this conversation as resolved.
Show resolved Hide resolved
Peer *peer.Peer
}

type handlerSpy struct {
zasweq marked this conversation as resolved.
Show resolved Hide resolved
Args []peerStats
zasweq marked this conversation as resolved.
Show resolved Hide resolved
mu sync.Mutex
}

func (h *handlerSpy) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
return ctx
}

func (h *handlerSpy) HandleRPC(ctx context.Context, rs stats.RPCStats) {
h.mu.Lock()
zasweq marked this conversation as resolved.
Show resolved Hide resolved
p, _ := peer.FromContext(ctx)
h.Args = append(h.Args, peerStats{rs, p})
h.mu.Unlock()
}

func (h *handlerSpy) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context {
return ctx
}

func (h *handlerSpy) HandleConn(context.Context, stats.ConnStats) {}