fix: fix connection gater in transport constructor

This commit is contained in:
Steven Allen 2020-05-20 10:37:51 -07:00
parent 3bf5baf905
commit 46b58740de
3 changed files with 32 additions and 5 deletions

View File

@ -28,12 +28,12 @@ var (
privKeyType = reflect.TypeOf((*crypto.PrivKey)(nil)).Elem()
pubKeyType = reflect.TypeOf((*crypto.PubKey)(nil)).Elem()
pstoreType = reflect.TypeOf((*peerstore.Peerstore)(nil)).Elem()
connGaterType = reflect.TypeOf((*connmgr.ConnectionGater)(nil)).Elem()
// concrete types
peerIDType = reflect.TypeOf((peer.ID)(""))
upgraderType = reflect.TypeOf((*tptu.Upgrader)(nil))
pskType = reflect.TypeOf((pnet.PSK)(nil))
connGaterType = reflect.TypeOf((*connmgr.ConnectionGater)(nil))
peerIDType = reflect.TypeOf((peer.ID)(""))
upgraderType = reflect.TypeOf((*tptu.Upgrader)(nil))
pskType = reflect.TypeOf((pnet.PSK)(nil))
)
var argTypes = map[reflect.Type]constructor{

View File

@ -121,7 +121,14 @@ func makeConstructor(
return func(h host.Host, u *tptu.Upgrader, cg connmgr.ConnectionGater) (interface{}, error) {
arguments := make([]reflect.Value, len(argConstructors))
for i, makeArg := range argConstructors {
arguments[i] = reflect.ValueOf(makeArg(h, u, cg))
if arg := makeArg(h, u, cg); arg != nil {
arguments[i] = reflect.ValueOf(arg)
} else {
// ValueOf an un-typed nil yields a zero reflect
// value. However, we _want_ the zero value of
// the _type_.
arguments[i] = reflect.Zero(t.In(i))
}
}
return callConstructor(v, arguments)
}, nil

View File

@ -7,10 +7,14 @@ import (
"strings"
"testing"
"github.com/libp2p/go-libp2p-core/connmgr"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/transport"
"github.com/libp2p/go-tcp-transport"
tptu "github.com/libp2p/go-libp2p-transport-upgrader"
)
func TestNewHost(t *testing.T) {
@ -33,6 +37,22 @@ func TestBadTransportConstructor(t *testing.T) {
}
}
func TestTransportConstructor(t *testing.T) {
ctx := context.Background()
ctor := func(
h host.Host,
_ connmgr.ConnectionGater,
upgrader *tptu.Upgrader,
) transport.Transport {
return tcp.NewTCPTransport(upgrader)
}
h, err := New(ctx, Transport(ctor))
if err != nil {
t.Fatal(err)
}
h.Close()
}
func TestNoListenAddrs(t *testing.T) {
ctx := context.Background()
h, err := New(ctx, NoListenAddrs)