Skip to content

Commit

Permalink
add testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
fho committed Oct 26, 2023
1 parent 8a58769 commit 28b2e40
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions test/gracefulstop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net"
"sync"
"testing"
"time"

"golang.org/x/net/http2"
"google.golang.org/grpc"
Expand Down Expand Up @@ -216,3 +217,55 @@ func (s) TestGracefulStopClosesConnAfterLastStream(t *testing.T) {
<-gracefulStopDone // Wait for GracefulStop to return.
})
}

func (s) TestGracefulStopBlocksUntilGRPCConnectionsTerminate(t *testing.T) {
unblockGRPCCall := make(chan struct{})
ss := &stubserver.StubServer{
UnaryCallF: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
t.Log("grpc server method called")
<-unblockGRPCCall
t.Log("grpc server method returned")
return &testpb.SimpleResponse{}, nil
},
}

s := grpc.NewServer()
testgrpc.RegisterTestServiceServer(s, ss)
err := ss.Start(nil)
if err != nil {
t.Fatalf("start failed: %s", err)
}
t.Cleanup(ss.Stop)

clt := ss.Client

grpcClientCallReturned := make(chan struct{})
go func() {
t.Log("UnaryCall")
_, err := clt.UnaryCall(context.Background(), &testpb.SimpleRequest{})
if err != nil {
t.Errorf("client got error from grpc method: %s", err)
}
close(grpcClientCallReturned)
}()

gracefulStopReturned := make(chan struct{})
go func() {
s.GracefulStop()
close(gracefulStopReturned)
}()
time.Sleep(time.Second)
select {
case <-gracefulStopReturned:
t.Log(ss.CC.GetState())
t.Error("GracefulStop returned before GRPC method call ended")
default:
}

unblockGRPCCall <- struct{}{}
<-grpcClientCallReturned
t.Log("grpc client call returned")

t.Log("waiting for GracefulStop() to return")
<-gracefulStopReturned
}

0 comments on commit 28b2e40

Please sign in to comment.