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 9 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
27 changes: 10 additions & 17 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,36 +109,29 @@ 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 {
easwars marked this conversation as resolved.
Show resolved Hide resolved
return err
}
return stream.Send(&testpb.StreamingOutputCallResponse{})
},
S: grpc.NewServer(),
}
s := grpc.NewServer()
testgrpc.RegisterTestServiceServer(s, ss)
stubserver.StartTestService(t, ss)
purnesh42H marked this conversation as resolved.
Show resolved Hide resolved

// 1. Start Server
easwars marked this conversation as resolved.
Show resolved Hide resolved
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)
gracefulStopDone := make(chan struct{})
<-dlis.acceptCalled
go func() {
s.Serve(dlis)
wg.Done()
ss.S.GracefulStop()
close(gracefulStopDone)
}()
easwars marked this conversation as resolved.
Show resolved Hide resolved

// 2. GracefulStop() Server after listener's Accept is called, but don't
easwars marked this conversation as resolved.
Show resolved Hide resolved
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.
<-dlis.acceptCalled
wg.Add(1)
go func() {
s.GracefulStop()
wg.Done()
}()

// 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.
Expand All @@ -150,7 +142,8 @@ func (s) TestGracefulStop(t *testing.T) {
// even though GracefulStop has closed the listener.
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)
}
Expand All @@ -165,7 +158,7 @@ func (s) TestGracefulStop(t *testing.T) {
t.Fatalf("FullDuplexCall= _, %v; want _, <status code Unavailable>", err)
}
cancel()
wg.Wait()
<-gracefulStopDone
}

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