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

test/gracefulstop: use stubserver instead of testservice implementation #7907

Merged
merged 18 commits into from
Feb 7, 2025
Merged
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 17 additions & 26 deletions test/gracefulstop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"context"
"fmt"
"net"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -110,62 +109,54 @@ func (s) TestGracefulStop(t *testing.T) {
closeCalled: make(chan struct{}),
allowCloseCh: make(chan struct{}),
}
d := func(ctx context.Context, _ string) (net.Conn, error) { return dlis.Dial(ctx) }
purnesh42H marked this conversation as resolved.
Show resolved Hide resolved

ss := &stubserver.StubServer{
Listener: dlis,
FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error {
_, err := stream.Recv()
if err != nil {
if _, err := stream.Recv(); err != nil {
return err
}
return stream.Send(&testpb.StreamingOutputCallResponse{})
},
S: grpc.NewServer(),
}
s := grpc.NewServer()
testgrpc.RegisterTestServiceServer(s, ss)
// 1.Start Server and start serving by calling Serve().
Copy link
Contributor

Choose a reason for hiding this comment

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

@pvsravani. You missed space here. Please recheck all comments

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

stubserver.StartTestService(t, ss)
purnesh42H marked this conversation as resolved.
Show resolved Hide resolved

// 1. Start Server
Copy link
Contributor

Choose a reason for hiding this comment

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

don't revert the step number

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

wg := sync.WaitGroup{}
wg.Add(1)
go func() {
s.Serve(dlis)
wg.Done()
}()

// 2. GracefulStop() Server after listener's Accept is called, but don't
Copy link
Contributor

Choose a reason for hiding this comment

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

don't revert the step number

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

// allow Accept() to exit when Close() is called on it.
//2. Call GracefulStop from a goroutine. It will trigger Close on the listener
Copy link
Contributor

Choose a reason for hiding this comment

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

Please leave a space after the // and before the first character of the comment.

Also, please ensure that comment lines are wrapped at 80-cols wide. See: https://google.github.io/styleguide/go/decisions#comment-line-length

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

//but the listener won’t close immediately—it waits until a connection is accepted.
gracefulStopDone := make(chan struct{})
<-dlis.acceptCalled
wg.Add(1)
go func() {
s.GracefulStop()
wg.Done()
ss.S.GracefulStop()
close(gracefulStopDone)
}()

// 3. Create a new connection to the server after listener.Close() is called.
Copy link
Contributor

Choose a reason for hiding this comment

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

don't revert the step number

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

// Server should close this connection immediately, before handshaking.
// 3.Create a new connection to the server after listener.Close() is called.
purnesh42H marked this conversation as resolved.
Show resolved Hide resolved
// Server should close this connection immediately, before handshaking.

<-dlis.closeCalled // Block until GracefulStop calls dlis.Close()

// Now dial. The listener's Accept method will return a valid connection,
// even though GracefulStop has closed the listener.
//Dial the server. This will cause a connection to be accepted. This will also unblock the Close method.
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here. Ensure space after the // and ensure wrapping at 80-cols.

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

ctx, dialCancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer dialCancel()
cc, err := grpc.DialContext(ctx, "", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(d))
dialer := func(ctx context.Context, _ string) (net.Conn, error) { return dlis.Dial(ctx) }
cc, err := grpc.DialContext(ctx, "", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(dialer))
if err != nil {
t.Fatalf("grpc.DialContext(_, %q, _) = %v", lis.Addr().String(), err)
}
client := testgrpc.NewTestServiceClient(cc)
defer cc.Close()

// 4. Send an RPC on the new connection.
// 4. Make an RPC.
// The server would send a GOAWAY first, but we are delaying the server's
// writes for now until the client writes more than the preface.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
if _, err = client.FullDuplexCall(ctx); err == nil || status.Code(err) != codes.Unavailable {
t.Fatalf("FullDuplexCall= _, %v; want _, <status code Unavailable>", err)
}
cancel()
wg.Wait()
<-gracefulStopDone
}

// TestGracefulStopClosesConnAfterLastStream ensures that a server closes the
Expand Down
Loading