Unverified Commit a4c54821 authored by Lars Gierth's avatar Lars Gierth Committed by GitHub

Merge pull request #5 from libp2p/fix/fdcostly/prefixx

fix: fdcostly should take only the prefix into account
parents 4b89cdc9 b53615a3
......@@ -2,7 +2,6 @@ package addrutil
import (
ma "github.com/multiformats/go-multiaddr"
mafmt "github.com/whyrusleeping/mafmt"
)
// SubtractFilter returns a filter func that filters all of the given addresses
......@@ -20,7 +19,16 @@ func SubtractFilter(addrs ...ma.Multiaddr) func(ma.Multiaddr) bool {
// IsFDCostlyTransport returns true for transports that require a new file
// descriptor per connection created
func IsFDCostlyTransport(a ma.Multiaddr) bool {
return mafmt.TCP.Matches(a)
res := false
ma.ForEach(a, func(c ma.Component) bool {
if c.Protocol().Code == ma.P_TCP {
res = true
return false
}
return true
})
return res
}
// FilterNeg returns a negated version of the passed in filter
......
package addrutil
import (
ma "github.com/multiformats/go-multiaddr"
"testing"
ma "github.com/multiformats/go-multiaddr"
)
func TestSubtractAndNegFilter(t *testing.T) {
......@@ -35,3 +36,50 @@ func TestIsFDCostlyTransport(t *testing.T) {
t.Errorf("Expected address %s to not need a new file descriptor per new connection", udpMa.String())
}
}
func TestIsFDCostly(t *testing.T) {
good := []ma.Multiaddr{
newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234"),
newMultiaddr(t, "/ip4/0.0.0.0/tcp/1234"),
newMultiaddr(t, "/ip6/::1/tcp/1234"),
newMultiaddr(t, "/ip6/::/tcp/1234"),
newMultiaddr(t, "/ip6/fe80::1/tcp/1234"),
newMultiaddr(t, "/ip6/fe80::/tcp/1234"),
newMultiaddr(t, "/ip6/fe80::/tcp/1234/http"),
newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234/http"),
}
bad := []ma.Multiaddr{
newMultiaddr(t, "/ip4/127.0.0.1/udp/1234"),
newMultiaddr(t, "/ip4/0.0.0.0/udp/1234/utp"),
newMultiaddr(t, "/ip6/::1/udp/1234"),
newMultiaddr(t, "/ip6/::/udp/1234"),
}
for _, a := range bad {
if IsFDCostlyTransport(a) {
t.Errorf("addr %s should not be fd costly", a)
}
}
for _, a := range good {
if !IsFDCostlyTransport(a) {
t.Errorf("addr %s should be fd costly", a)
}
}
}
func TestIsFdCostlyMalformed(t *testing.T) {
bad := []ma.Multiaddr{
newMultiaddr(t, "/ip4/127.0.0.1/"),
newMultiaddr(t, "/ip4/0.0.0.0/"),
newMultiaddr(t, "/ip6/::1/"),
newMultiaddr(t, "/ip6/::/"),
}
for _, a := range bad {
if IsFDCostlyTransport(a) {
t.Errorf("addr %s should not be fd costly", a)
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment