Skip to content

Commit

Permalink
fix(libp2phttp): Return ErrServerClosed on Close (#3050)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo authored Nov 21, 2024
1 parent e13d4de commit 288868c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions p2p/http/libp2phttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ func (h *Host) Serve() error {
expectedErrCount := len(h.httpTransport.listeners)
select {
case <-h.httpTransport.closeListeners:
err = http.ErrServerClosed
case err = <-errCh:
expectedErrCount--
}
Expand Down
18 changes: 18 additions & 0 deletions p2p/http/libp2phttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
httpping "github.com/libp2p/go-libp2p/p2p/http/ping"
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -996,3 +997,20 @@ func TestImpliedHostIsSet(t *testing.T) {
}

}

func TestErrServerClosed(t *testing.T) {
server := libp2phttp.Host{
InsecureAllowHTTP: true,
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/0/http")},
}

done := make(chan struct{})
go func() {
err := server.Serve()
assert.Equal(t, http.ErrServerClosed, err)
close(done)
}()

server.Close()
<-done
}

0 comments on commit 288868c

Please sign in to comment.