2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-09-20 02:55:46 +08:00

Add TestClose

Signed-off-by: Abhilash Gnan <abhilashgnan@gmail.com>
This commit is contained in:
Abhilash Gnan 2021-01-14 23:05:11 +01:00 committed by Soheil Hassas Yeganeh
parent ce11cfdf3a
commit cdd3331e3e
2 changed files with 33 additions and 5 deletions

View File

@ -128,7 +128,7 @@ func runTestHTTPServer(errCh chan<- error, l net.Listener) {
mu.Unlock() mu.Unlock()
}, },
} }
if err := s.Serve(l); err != ErrListenerClosed { if err := s.Serve(l); err != ErrListenerClosed && err != ErrServerClosed {
errCh <- err errCh <- err
} }
} }
@ -218,7 +218,7 @@ func runTestRPCServer(errCh chan<- error, l net.Listener) {
for { for {
c, err := l.Accept() c, err := l.Accept()
if err != nil { if err != nil {
if err != ErrListenerClosed { if err != ErrListenerClosed && err != ErrServerClosed {
errCh <- err errCh <- err
} }
return return
@ -651,7 +651,7 @@ func TestMultipleMatchers(t *testing.T) {
runTestHTTP1Client(t, l.Addr()) runTestHTTP1Client(t, l.Addr())
} }
func TestClose(t *testing.T) { func TestListenerClose(t *testing.T) {
defer leakCheck(t)() defer leakCheck(t)()
errCh := make(chan error) errCh := make(chan error)
defer func() { defer func() {
@ -685,7 +685,7 @@ func TestClose(t *testing.T) {
// Second connection either goes through or it is closed. // Second connection either goes through or it is closed.
if _, err := anyl.Accept(); err != nil { if _, err := anyl.Accept(); err != nil {
if err != ErrListenerClosed { if err != ErrListenerClosed && err != ErrServerClosed {
t.Fatal(err) t.Fatal(err)
} }
// The error is either io.ErrClosedPipe or net.OpError wrapping // The error is either io.ErrClosedPipe or net.OpError wrapping
@ -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. // Cribbed from google.golang.org/grpc/test/end2end_test.go.
// interestingGoroutines returns all goroutines we care about for the purpose // interestingGoroutines returns all goroutines we care about for the purpose

View File

@ -29,6 +29,7 @@ import (
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
"github.com/soheilhy/cmux" "github.com/soheilhy/cmux"
"google.golang.org/grpc/examples/helloworld/helloworld"
grpchello "google.golang.org/grpc/examples/helloworld/helloworld" grpchello "google.golang.org/grpc/examples/helloworld/helloworld"
) )
@ -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) ( func (s *grpcServer) SayHello(ctx context.Context, in *grpchello.HelloRequest) (
*grpchello.HelloReply, error) { *grpchello.HelloReply, error) {