Skip to content

Commit

Permalink
Add TestClose
Browse files Browse the repository at this point in the history
Signed-off-by: Abhilash Gnan <abhilashgnan@gmail.com>
  • Loading branch information
jan25 authored and soheilhy committed Jan 14, 2021
1 parent ce11cfd commit cdd3331
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
33 changes: 29 additions & 4 deletions cmux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func runTestHTTPServer(errCh chan<- error, l net.Listener) {
mu.Unlock()
},
}
if err := s.Serve(l); err != ErrListenerClosed {
if err := s.Serve(l); err != ErrListenerClosed && err != ErrServerClosed {
errCh <- err
}
}
Expand Down Expand Up @@ -218,7 +218,7 @@ func runTestRPCServer(errCh chan<- error, l net.Listener) {
for {
c, err := l.Accept()
if err != nil {
if err != ErrListenerClosed {
if err != ErrListenerClosed && err != ErrServerClosed {
errCh <- err
}
return
Expand Down Expand Up @@ -651,7 +651,7 @@ func TestMultipleMatchers(t *testing.T) {
runTestHTTP1Client(t, l.Addr())
}

func TestClose(t *testing.T) {
func TestListenerClose(t *testing.T) {
defer leakCheck(t)()
errCh := make(chan error)
defer func() {
Expand Down Expand Up @@ -685,7 +685,7 @@ func TestClose(t *testing.T) {

// Second connection either goes through or it is closed.
if _, err := anyl.Accept(); err != nil {
if err != ErrListenerClosed {
if err != ErrListenerClosed && err != ErrServerClosed {
t.Fatal(err)
}
// The error is either io.ErrClosedPipe or net.OpError wrapping
Expand All @@ -696,6 +696,31 @@ func TestClose(t *testing.T) {
}
}

func TestClose(t *testing.T) {
defer leakCheck(t)()
errCh := make(chan error)
defer func() {
select {
case err := <-errCh:
t.Fatal(err)
default:
}
}()
l, cleanup := testListener(t)
defer cleanup()

muxl := New(l)
anyl := muxl.Match(Any())

go safeServe(errCh, muxl)

muxl.Close()

if _, err := anyl.Accept(); err != ErrServerClosed {
t.Fatal(err)
}
}

// Cribbed from google.golang.org/grpc/test/end2end_test.go.

// interestingGoroutines returns all goroutines we care about for the purpose
Expand Down
5 changes: 4 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"golang.org/x/net/websocket"

"github.com/soheilhy/cmux"
"google.golang.org/grpc/examples/helloworld/helloworld"
grpchello "google.golang.org/grpc/examples/helloworld/helloworld"
)

Expand Down Expand Up @@ -86,7 +87,9 @@ func serveRPC(l net.Listener) {
}
}

type grpcServer struct{}
type grpcServer struct {
helloworld.UnimplementedGreeterServer
}

func (s *grpcServer) SayHello(ctx context.Context, in *grpchello.HelloRequest) (
*grpchello.HelloReply, error) {
Expand Down

0 comments on commit cdd3331

Please sign in to comment.