go-libp2p/config/muxer_test.go

105 lines
2.3 KiB
Go
Raw Normal View History

package config
import (
"testing"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
2018-08-21 16:25:52 +00:00
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
yamux "github.com/libp2p/go-libp2p-yamux"
)
func TestMuxerSimple(t *testing.T) {
// single
_, err := MuxerConstructor(func(_ peer.ID) network.Multiplexer { return nil })
if err != nil {
t.Fatal(err)
}
}
func TestMuxerByValue(t *testing.T) {
_, err := MuxerConstructor(yamux.DefaultTransport)
if err != nil {
t.Fatal(err)
}
}
func TestMuxerDuplicate(t *testing.T) {
_, err := MuxerConstructor(func(_ peer.ID, _ peer.ID) network.Multiplexer { return nil })
if err != nil {
t.Fatal(err)
}
}
func TestMuxerError(t *testing.T) {
_, err := MuxerConstructor(func() (network.Multiplexer, error) { return nil, nil })
if err != nil {
t.Fatal(err)
}
}
func TestMuxerBadTypes(t *testing.T) {
for i, f := range []interface{}{
func() error { return nil },
func() string { return "" },
func() {},
func(string) network.Multiplexer { return nil },
func(string) (network.Multiplexer, error) { return nil, nil },
nil,
"testing",
} {
if _, err := MuxerConstructor(f); err == nil {
t.Fatalf("constructor %d with type %T should have failed", i, f)
}
}
}
2018-08-21 16:25:52 +00:00
func TestCatchDuplicateTransportsMuxer(t *testing.T) {
h, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
if err != nil {
t.Fatal(err)
}
2018-08-21 16:25:52 +00:00
yamuxMuxer, err := MuxerConstructor(yamux.DefaultTransport)
if err != nil {
t.Fatal(err)
}
var tests = map[string]struct {
h host.Host
transports []MsMuxC
expectedError string
}{
"no duplicate transports": {
h: h,
2021-04-29 21:35:37 +00:00
transports: []MsMuxC{{yamuxMuxer, "yamux"}},
2018-08-21 16:25:52 +00:00
expectedError: "",
},
"duplicate transports": {
h: h,
transports: []MsMuxC{
2021-04-29 21:35:37 +00:00
{yamuxMuxer, "yamux"},
{yamuxMuxer, "yamux"},
2018-08-21 16:25:52 +00:00
},
expectedError: "duplicate muxer transport: yamux",
},
}
for testName, test := range tests {
t.Run(testName, func(t *testing.T) {
_, err = makeMuxer(test.h, test.transports)
if err != nil {
if err.Error() != test.expectedError {
t.Errorf(
"\nexpected: [%v]\nactual: [%v]\n",
test.expectedError,
err,
)
}
}
})
}
}